Prototype8-1 Prototype CS490 Design Patterns Alex Lo, Rose-Hulman Institute May 13, 2003.

22
Prototype 8-1 Prototype CS490 Design Patterns Alex Lo, Rose-Hulman Institute May 13, 2003
  • date post

    19-Dec-2015
  • Category

    Documents

  • view

    222
  • download

    0

Transcript of Prototype8-1 Prototype CS490 Design Patterns Alex Lo, Rose-Hulman Institute May 13, 2003.

Prototype 8-1

Prototype

CS490 Design PatternsAlex Lo, Rose-Hulman Institute

May 13, 2003

Prototype 8-2

Outline

Definitions Example Exercises Cloning

Prototype 8-3

What is Prototype? (short)

Metsker: “To provide new objects by copying an example”

GoF: “Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.”

Prototype 8-4

Prototype Applicability from GoF

Use the Prototype Pattern when a system should be independent of how it's products are created, composed and represented; and when the classes to instantiate are specified at run-

time, for example, by dynamic loading; or to avoid building a class hierarchy of factories that

parallels the class hierarchy of products; or 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.

Prototype 8-5

Benefits and Liabilities from GoF

Adding and removing products at run time - lets you incorporate a new class into a system by registering a prototypical instance with the client.

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

Specifying new objects by varying structure Reduced sub-classing - lets you clone rather

than to make a new object Configuring an application with classes

dynamically - some run-time environments let you load classes into an application dynamically

Prototype 8-6

Metsker Example: Replace Abstract Factory

Remember Abstract Factory? UI tool kits

Prototype 8-7

Challenge 18.1

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.

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.

Prototype 8-8

Challenge 18.1 Solution

Prototype 8-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);

Prototype 8-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(),

Prototype 8-11

Challenge Solutionpublic 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;

}

Prototype 8-12

Cartoon of the Day

Prototype 8-13

Cloning in Java

The Object class in Java has a clone() function.

Classes that wish to have this functionality must implement the Cloneable interface

Prototype 8-14

Challenge 18.3

How does clone() work?

Prototype 8-15

Solution

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.

Prototype 8-16

Using clone()

OzTextArea has more than 100 instance variables.

Creating new instantiation safer than using clone()

Why?

Prototype 8-17

Challenge 18.4

Write OzTextArea.clone() so that it copies a text area without relying on the superclass implementation of clone()

Prototype 8-18

Solution

public Object clone() {

OzTextArea ta = new OzTextArea(); ta.setFont(textArea().getFont()); ta.setCursor(getCursor()); return ta;

}

Prototype 8-19

Metsker Example Con’t

When our UI components can properly clone themselves, then we can entirely replace the abstract UI factory we had before.

Prototype 8-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"); }}

Prototype 8-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) ); }}

Prototype 8-22

QUIZ!