Prototype8-1 Prototype CS490 Design Patterns Alex Lo, Rose-Hulman Institute May 13, 2003
Prototype8-2 Outline r Definitions r Example r Exercises r Cloning
Prototype8-3 What is Prototype? (short) r Metsker: “To provide new objects by copying an example” r GoF: “Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.”
Prototype8-4 Prototype Applicability from GoF r Use the Prototype Pattern when a system should be independent of how it's products are created, composed and represented; and m when the classes to instantiate are specified at run-time, for example, by dynamic loading; or m to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or m when instances of a class can have one of only a few different combinations of state. It may be more convenient to install a corresponding number of prototypes and clone them rather than instantiating the class manually, each time with the appropriate state.
Prototype8-5 Benefits and Liabilities from GoF r Adding and removing products at run time - lets you incorporate a new class into a system by registering a prototypical instance with the client. r Specifying new objects by varying values - effectively define new kinds of objects by instantiating existing classes and registering the instances as prototypes of client objects r Specifying new objects by varying structure r Reduced sub-classing - lets you clone rather than to make a new object r Configuring an application with classes dynamically - some run-time environments let you load classes into an application dynamically
Prototype8-6 Metsker Example: Replace Abstract Factory r Remember Abstract Factory? r UI tool kits
Prototype8-7 Challenge 18.1 r Rather than have several abstract factories, we’d like to have a UIKit class that can be prototyped to substitute for the Kits from the last diagram. r Draw a diagram of a UIKit class, showing instance variables for prototypical button and text area objects and showing the creation methods that will return copies of these objects.
Prototype8-8 Challenge 18.1 Solution
Prototype8-9 Example con’t In UIKit, you initialize the fields like so: protected OzButton button = new OzButton(); protected OzTextArea textArea = new OzTextArea(); //... The UI factories just differ in font and cursor, for FullScreenKit Font f = new Font("Dialog", Font.ITALIC, 18);
Prototype8-10 Challenge 18.2 For the HandheldKit: Cursor c = new Cursor(Cursor.HAND_CURSOR); Font f = new Font("Dialog", Font.PLAIN, 8); To create a factory for full-screen or handheld components, you create a UIKit object and set the cursor and font of this object's instance variables. A convenient place to do this work is in UIKit static methods that return instances of the appropriately tuned factories. Write UIKit.handheld(),
Prototype8-11 Challenge Solution public static UIKit handheld() { UIKit k = new UIKit(); Font f = new Font("Dialog", Font.PLAIN, 8); k.button.setFont(f); k.textArea.setFont(f); Cursor c = new Cursor(Cursor.HAND_CURSOR); k.textArea.setCursor(c); return k; }
Prototype8-12 Cartoon of the Day
Prototype8-13 Cloning in Java r The Object class in Java has a clone() function. r Classes that wish to have this functionality must implement the Cloneable interface
Prototype8-14 Challenge 18.3 r How does clone() work?
Prototype8-15 Solution r creates a new instance of the class of this object and initializes all its fields with exactly the contents of the corresponding fields of this object, as if by assignment; the contents of the fields are not themselves cloned. Thus, this method performs a 'shallow copy' of this object, not a 'deep copy' operation.
Prototype8-16 Using clone() r OzTextArea has more than 100 instance variables. r Creating new instantiation safer than using clone() r Why?
Prototype8-17 Challenge 18.4 r Write OzTextArea.clone() so that it copies a text area without relying on the superclass implementation of clone()
Prototype8-18 Solution public Object clone() { OzTextArea ta = new OzTextArea(); ta.setFont(textArea().getFont()); ta.setCursor(getCursor()); return ta; }
Prototype8-19 Metsker Example Con’t r When our UI components can properly clone themselves, then we can entirely replace the abstract UI factory we had before.
Prototype8-20 public class ShowKit { public static JPanel crossSales(UIKit k) { JPanel p = new JPanel(); p.setLayout(new BorderLayout()); p.add(k.createButton("Clear"), "South"); OzTextArea t = k.createTextArea(); t.append(" 1) Consult the recommendation list.\n"); … p.add(t, "Center"); return p; } public static void main(String[] args) { UIKit k = UIKit.handheld(); JPanel p = ShowKit.crossSales(k); SwingFacade.launch(p, " Oozinoz Cross Sales"); }
Prototype8-21 Cloning Gone Wrong public class ShowCloningProblem { public static void main(String[] args) { Integer x = new Integer(10); myClass m1 = new myClass(x); myClass m2 = m1.clone(); m2.x.set( new Integer(12) ); }
Prototype8-22 QUIZ!