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

Slides:



Advertisements
Similar presentations
Creational Patterns, Abstract Factory, Builder Billy Bennett June 11, 2009.
Advertisements

Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
ITEC200 – Week03 Inheritance and Class Hierarchies.
 Consists of Creational patterns  Each generator pattern has a Client, Product, and Generator.  The Generator needs at least one operation that creates.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Inheritance Review CSC 171 FALL 2004 LECTURE 19. READING Read Horstmann, Chapter 11 Look at Chapter 12 – Will not be on MT or Final – One lab Begin Reading.
Factory Method Joey Richey Kevin Gorski. Definition Allows a class developer define the interface for creating an object while retaining control of which.
Prototype Pattern Intent:
Inheritance and Polymorphism Recitation – 10/(16,17)/2008 CS 180 Department of Computer Science, Purdue University.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Design Patterns in Java Chapter 18 Prototype Summary prepared by Kirk Scott 1.
Type Laundering & Prototype Pattern Kunal Chaudhary.
Design Patterns.
(C) 2010 Pearson Education, Inc. All rights reserved. Java™ How to Program, 8/e.
CS 4240: Bridge and Abstract Factory Readings:  Chap. 10 and 11 Readings:  Chap. 10 and 11.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns II.
Unit 4 Prototype Summary prepared by Kirk Scott 1.
Unit 4 Prototype Summary prepared by Kirk Scott 1.
Software Components Creational Patterns.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Programming in Java CSCI-2220 Object Oriented Programming.
Define an interface for creating an object, but let subclasses decide which class to instantiate Factory Method Pattern.
CDP-1 9. Creational Pattern. CDP-2 Creational Patterns Abstracts instantiation process Makes system independent of how its objects are –created –composed.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.
Chapter 14 Abstract Classes and Interfaces. Abstract Classes An abstract class extracts common features and functionality of a family of objects An abstract.
Review Class Inheritance, Abstract, Interfaces, Polymorphism, GUI (MVC)
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
1 The finalize, clone, and getClass Methods  The finalize method is invoked by the garbage collector on an object when the object becomes garbage.  The.
1 Prototype Design Pattern Nitin Prabhu Adapted from presentations of Mike Fortozo,John Lin
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Singleton Pattern Presented By:- Navaneet Kumar ise
1 More OO Design Patterns CSC 335: Object-Oriented Programming and Design.
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
(c) University of Washington06-1 CSC 143 Java Inheritance Tidbits.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
Singleton Pattern. Problem Want to ensure a single instance of a class, shared by all uses throughout a program Context Need to address initialization.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
1 Creational Design Patterns CSC 335: Object-Oriented Programming and Design.
PROTOTYPE. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract factory.
Sep 181 Example Program DemoTranslateEnglishGUI.java.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
Notices Assn 2 is due tomorrow, 7pm. Moodle quiz next week – written in the lab as before. Everything up to and including today’s lecture: Big Topics are.
Java Programming: Guided Learning with Early Objects Chapter 9 Inheritance and Polymorphism.
Generator Design Patterns: Singleton and Prototype
Design Patterns: MORE Examples
Abstract Factory Pattern
Week 2, Day 1: The Factory Method Pattern
Factory Patterns 1.
Inheritance and Polymorphism
Creational Pattern: Prototype
object oriented Principles of software design
Abstract Factory Pattern
Intent (Thanks to Jim Fawcett for the slides)
Object Oriented Design Patterns - Creational Patterns
Prototype Pattern 1.
Lesson 5: More on Creational Patterns
Constructors, GUI’s(Using Swing) and ActionListner
Creational Patterns.
Decorator Pattern.
Software Design Lecture : 28.
Presentation transcript:

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!