Presentation is loading. Please wait.

Presentation is loading. Please wait.

Head-First Design Patterns. Similar to a ReminderEntry.

Similar presentations


Presentation on theme: "Head-First Design Patterns. Similar to a ReminderEntry."— Presentation transcript:

1 Head-First Design Patterns

2 Similar to a ReminderEntry

3 Similar to Reminders But no iterator. Just the raw data structure, i.e., ArrayList. To use this, outsider will have to keep index, increment it, etc. And they can change it!

4 How to use raw Get raw here. Set up loop here.

5 Use Iterator instead Much cleaner. Don’t need to know what data structure is being used. Just need to know I can use next and hasNext. And no access to raw! What work do we have to do to make this so easy in above code? 1.define createIterator method in BreakfastMenu class. 2.Figure out how to define that Iterator class that’s doing all the work.

6 The hard way: define your own Iterator class Would be similar to defining a RemindersIterator class. I *think* this code is poorly designed! Why? Because is does not clone the list from what I can see.

7 Code to show the strangeness DinerMenu dmenu = new DinerMenu(); DinerMenuIterator dit1 = dmenu.createIterator(); boolean b1 = dit1.hasNext();//should be false dmenu.add(“some new entry”); DinerMenuIterator dit2 = dmenu.creatIterator(); boolean b2 = dit2.hasNext();//should be true //Big question b1 = dit1.hasNext();//I predict will be true now Solution? public DinerMenuIterator createIterator(){ return new DinerMenuIterator( menuItems.clone() ); } Takes a “snapshot” of the data. If changes made later, snapshot is unchanged.

8 The easy way: look at data structure API! Data StructureMethod to return iterator ArrayList.iterator() LinkedList.iterator() Vector.iterator() arrayClass ArrayIterator …… Note that these methods do not clone either. If you want a snapshot, you will have to do something like: return my_linkedlist.clone().iterator();

9 Another Chapter

10 How to use it: Singleton s = Singleton.getInstance(); s.someMethod(); public void someMethod(){…} Access Levels ModifierClassPackageSubclassWorld publicYYYY protectedYYYN no modifier YYNN privateYNNN

11 Singleton Brings Up Concurrency Issues Many modern programs use multiple threads of control. This gives lots of power, e.g., multi-core, time- sharing in single-core. But it also brings up problems. Like to introduce you to one of them: concurrency bugs. Can use the Singleton pattern as example.

12 public class ThreadedClass extends Thread public void run(int i){ //this method runs when start() is invoked while( true ){ System.out.println( i ); } public static void main( String[] args ){ ThreadedClass t1 = new ThreadedClass(); t1.start(1); ThreadedClass t2 = new ThreadedClass(); t2.start(2); } What is printed? First Look at Thread class

13 public class ThreadedClass extends Thread static int data; public ThreadedClass(){ data = 0; } public void run(){ //this method runs when start() is invoked while( true ){ int k = data; k++; data = k; System.out.println( data ); } public static void main( String[] args ){ ThreadedClass t1 = new ThreadedClass(); t1.start(); ThreadedClass t2 = new ThreadedClass(); t2.start(); } What is printed? Gets more interesting. Opens the door for concurrency bugs. This will be shared by all threads.

14 while( true ){ t1 int k = data; k++; data = k; System.out.println( data ); } while( true ){ t2 int k = data; k++; data = k; System.out.println( data ); } t1: k 1 = 0 t1: k 1 = 1 t2: k 2 = 0 t2: k 2 = 1 t1: data = 1 t1: SOP 1 t2: data = 1 t2: SOP 1 t1: … t1: data = 1 t2: … t2: data = 2 t1: SOP 1 t2: SOP 2 ti: SOP 3 ti: SOP4 etc. What do we expect printed with two threads running? data

15 t1 t2 Who Cares? data t3 Where could this bite us in our project?

16

17 You can synchronize methods! Means only one thread at a time can use. Can’t break up into smaller chunks. Solves the concurrency bug we saw. Why is it expensive?

18 View as an annotation that says multiple threads might share this variable. Did we squash this concurrency bug?

19 One Other Idea

20 Serialization Issues public class Singleton implements java.io.Serializable { public final static Singleton INSTANCE = new Singleton(); private Singleton() { } } /////////////////////////////////////////////////////////////////////////////////////////// public class SingletonTest { public static void main(String [] args){ writeSingleton(); Singleton s1 = readSingleton(); Singleton s2 = readSingleton(); Assert.assertEquals(true, s1 == s2); } private static void writeSingleton() { FileOutputStream fos = new FileOutputStream("serializedSingleton"); ObjectOutputStream oos = new ObjectOutputStream(fos); oos.writeObject(Singleton.INSTANCE); oos.flush(); } private static Singleton readSingleton() { Singleton s = null; FileInputStream fis = new FileInputStream("serializedSingleton"); ObjectInputStream ois = new ObjectInputStream(fis); s = (Singleton)ois.readObject(); return s; } true or false?

21 Work Around public class Singleton implements java.io.Serializable { public static Singleton INSTANCE = new Singleton(); protected Singleton() {} private Object readResolve() { return INSTANCE; } } The readResolve method is called when ObjectInputStream has read an object from the stream and is preparing to return it to the caller. ObjectInputStream checks whether the class of the object defines the readResolve method. If the method is defined, the readResolve method is called to allow the object in the stream to designate the object to be returned. The object returned should be of a type that is compatible with all uses. If it is not compatible, a ClassCastException will be thrown when the type mismatch is discovered.


Download ppt "Head-First Design Patterns. Similar to a ReminderEntry."

Similar presentations


Ads by Google