170 likes | 178 Views
Midterm Review, Assignments 3,4. Mon: Midterm. Wed: Guest Speaker Michael Pieck, VP Goldman, Sachs & Co. Review for Midterm. Remember IBooleanCriteria?. Interface used to build up boolean statements public boolean satisfies( Object o ) Functor Use this to filter
E N D
Mon: Midterm Wed: Guest Speaker Michael Pieck, VP Goldman, Sachs & Co
Remember IBooleanCriteria? • Interface used to build up boolean statements • public boolean satisfies( Object o ) • Functor • Use this to filter • FilteredIterator goes with it List l = … new FilteredIterator( l.iteratior(), booleanCriteria() )
What does this do myClass.newInstance() • Creates new instance of Class called myClass • You have to have no arg constructor) • Ctor needs to be public
Remember Factory Method? • Class that instantiates objects of different types based on keys.
Remember is Singleton? • Class that allows only one instance • Private constructor • Either public static accessor method or • Public static instance
Remember Observable/Observable? • Its pattern which is applicable to cases when objects need to be aware of the state change in other objects • Observable always maintains a list of observers and notifies them when changes occur by either specific method or via an event
Remember Cache? • Class or collaboration or pattern which allows faster access to a subset of data, which is typically most often used. • Speed of search • What to evict? • Evict the oldest ones • Random • Least frequently accessed
What does this do? List l = new ArrayList(); for ( int i = 0; i < 10; I++ ) { l.add( createRandomShape() ); } Iterator iter = new FilteredIterator( l.iterator(), new IBooleanCriteria() { public boolean satisfies( Object o ) { return ( o instanceof Circle ); } } ); while ( iter.hasNext() ) { System.out.println( o.next() ); }
Remember Object Pool? • Pattern where N objects are typically allocated instead of 1 and then reused. • Object get() • Release( Object o )
Remember Uses, Contains, Extends? Public class MyClass extends YourClass { // uses Public Foo add( Bar b ) { A a = new A(); b.doSomethig(); } // contains private XYZ xyz; }
Remember Comparator and Comparable? • Comparable: compareTo( Object o ); • Comparator: int compare( Object o1, Object o2 ) • Comparable is interface implemented by objects with natural ordering • Comparator is an interface used to compare any two objects
Pulic class StudentFirstNameComp implements Comparator { Public int compare( Object o1, Object o2 ) { Return ((Student)o1).getFirstName().compareTo( ((Student)o2).getFirstName() ); } List l = createListOfStudents(); Collections.sort( l, new StudentFirstNameComp () ); Collections.sort( l );
Remember Flyweight? • Only one instance of every flyweight exists by is created dynamically. Flyweights are immutable. • ColorFactory { • public Color getColor( int r, int g, int b ); • }
<eworld class=EWorld> <property name=mutation value=0.0001/> <site class=Desert> <property name=bg value=red/> </site> </eworld> public class Eworld { public void addSite( ISite site ) public void setMutation(); public double getMutation(); } public class Desert impl ISite { public void setBg(); public Color getBg(); }
// build for site tag • Public void build( ITag parent ) { • super.build( parent ); Eworld ew = (Eworld)parent.getObject(); Isite site = (Isite)getObject(); ew.addSite( site ); }