The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1.

Slides:



Advertisements
Similar presentations
Lecture 5: Interfaces.
Advertisements

ACM/JETT Workshop - August 4-5, Classes, Objects, Equality and Cloning.
CSC 243 – Java Programming, Spring 2013 March 26, 2013 Week 8, java.lang.Clonable & java.io.Serializable Interfaces.
Effective Java, Chapter 3: Methods Common to All Objects.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Java Exceptions, Cloning, Serialization Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Prototype Pattern Creational Pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copy this prototype.
Tirgul 1 Today’s subjects: –First programming exercise. –Java reminder & completions : reference data types, cloning, inner classes, packages. –Short reminder.
1 More on Inheritance Overview l Object: The father of all classes l Casting and Classes l Object Cloning l Importance of Cloning.
Mutable, Immutable, and Cloneable Objects Chapter 15.
Fall 2007CS 2251 Inheritance and Class Hierarchies Chapter 3.
Interfaces. In this class, we will cover: What an interface is Why you would use an interface Creating an interface Using an interface Cloning an object.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
1 Java Object Model Part 2: the Object class. 2 Object class Superclass for all Java classes Any class without explicit extends clause is a direct subclass.
Prototype Pattern Intent:
Mutable, Immutable, and Cloneable Objects Chapter 15 Slides by Steve Armstrong LeTourneau University Longview, TX  2007,  Prentice Hall.
Lecture 18 Review the difference between abstract classes and interfaces The Cloneable interface Shallow and deep copies The ActionListener interface,
Chapter 10 Classes Continued
Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.
Interfaces and Inner Classes. What is an Interface?  What is “presented to the user”?  The public part of a class?  What is the substance of an interface?
GETTING INPUT Simple I/O. Simple Input Scanner scan = new Scanner(System.in); System.out.println("Enter your name"); String name = scan.nextLine(); System.out.println("Enter.
Appendix A.2: Review of Java and Object-Oriented Programming: Part 2 “For the object-oriented project, remember that the primary unit of decomposition.
Chapter 6 Class Inheritance F Superclasses and Subclasses F Keywords: super F Overriding methods F The Object Class F Modifiers: protected, final and abstract.
1 Java Inheritance. 2 Inheritance On the surface, inheritance is a code re-use issue. –we can extend code that is already written in a manageable manner.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
Modern Software Development Using C#.NET Chapter 5: More Advanced Class Construction.
Refactoring Deciding what to make a superclass or interface is difficult. Some of these refactorings are helpful. Some research items include Inheritance.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
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.
Object Oriented Programming
Billy Bennett June 22,  Intent Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.
©SoftMoore ConsultingSlide 1 Serialization. ©SoftMoore ConsultingSlide 2 Serialization Allows objects to be written to a stream Can be used for persistence.
Java Type System and Object Model Horstmann ch , 7.7.
Chapter 8 Class Inheritance and Interfaces F Superclasses and Subclasses  Keywords: super F Overriding methods  The Object Class  Modifiers: protected,
The State Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Interfaces F What is an Interface? F Creating an Interface F Implementing an Interface F What is Marker Interface?
The Facade Pattern (Structural) ©SoftMoore ConsultingSlide 1.
CS 116 OBJECT ORIENTED PROGRAMMING II LECTURE 12 GEORGE KOUTSOGIANNAKIS Copyright: 2015 Illinois Institute of Technology/ George Koutsogiannakis 1.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
The Strategy Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Inheritance and Class Hierarchies Chapter 3. Chapter Objectives  To understand inheritance and how it facilitates code reuse  To understand how Java.
The Decorator Pattern (Structural) ©SoftMoore ConsultingSlide 1.
The Flyweight Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Introduction to Collections. Collections Collections provide a way of organizing related data in a model Different types of collections have different.
The Singleton Pattern (Creational)
Quick Review of OOP Constructs Classes:  Data types for structured data and behavior  fields and methods Objects:  Variables whose data type is a class.
CIS3023: Programming Fundamentals for CIS Majors II Summer 2010 Ganesh Viswanathan Interfaces (Part II) Course Lecture Slides 28 June 2010 “A picture is.
Interfaces, Abstract Classes, and Polymorphism. What Is an Interface? An interface is the set of public methods in a class Java provides the syntax for.
The Memento Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
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.
COMPOSITE PATTERN NOTES. The Composite pattern l Intent Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients.
Chapter 7: Cloning and RTTI
Polymorphism 2nd Lecture
Inheritance and Polymorphism
Creational Pattern: Prototype
Intent (Thanks to Jim Fawcett for the slides)
CSE 331 Cloning objects slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia
Lecture 4: Interface Design
Java Programming Language
Prototype Pattern 1.
Lesson 5: More on Creational Patterns
Interfaces.
CS 112 Programming 2 Lecture 02 Abstract Classes & Interfaces (2)
Presentation transcript:

The Prototype Pattern (Creational) ©SoftMoore ConsultingSlide 1

Motivational Example Suppose you have a list of Shape objects, which could be circles, rectangles, etc. How would you write the code to duplicate that list? –Note: You are required to duplicate every shape on the list (deep copy), not simply duplicate the references (shallow copy). How can you create a duplicate list without knowing the exact class of each shape in the original list? –multiway switch based on class? (not maintainable) –reflection? (complicated and messy) –prototype pattern? (simple and maintainable) ©SoftMoore ConsultingSlide 2 for (Shape s : list1) list2.add((Shape)s.clone()); cast required since return type of clone() is Object

Summary of Prototype Pattern The prototype pattern allows creation of objects without knowing their exact type. New objects are created by asking existing objects to make copies of themselves. Java supports the prototype pattern via the clone() method, which is declared in class Object, the mother of all classes. ©SoftMoore ConsultingSlide 3

Shallow Copy versus Deep Copy Shallow copy –field-by-field copy –copies references, not values –fast and memory efficient, but the two copies are not independent – changes to one are reflected in the other Deep copy –recursively copy values, not simply references to values –slower and consumes more memory, but the two copies are independent – changes to one have no affect on the other ©SoftMoore ConsultingSlide 4

Shallow Copy Illustrated ©SoftMoore ConsultingSlide 5 :Circle:Rectangle:Circle:Pentagon list1 list2 What happens when you change the radius of one of the circles?

Deep Copy Illustrated ©SoftMoore ConsultingSlide 6 :Circle:Rectangle:Circle:Pentagon list1 :Circle:Rectangle:Circle:Pentagon list2

Prototype Pattern Intent: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Collaborations: A client asks a prototype object to clone itself. Consequences: –Client can create objects without knowing their exact class or creation details –Easy to add and remove objects at run-time. –Reduced number of class and reduced subclassing when compared to other creational patterns. ©SoftMoore ConsultingSlide 7

Prototype Pattern (continued) Structure: ©SoftMoore ConsultingSlide 8 Client operation() Prototype clone() ConcretePrototype1 clone() ConcretePrototype2 clone() p = prototype.clone() return copy of self prototype

Prototype Pattern (continued) Example Application: User-customizable palette of drawing symbols for a graphics application. Implementation Issues: –using a prototype manager (registry of available prototypes) –shallow copy versus deep copy (what can be shared) Support for the Prototype pattern in Java: –the clone() method defined in class Object –use of Cloneable interface to grant permission for cloning ©SoftMoore ConsultingSlide 9

Using a Prototype Manager When the prototypes in a system can be created and destroyed dynamically, consider using a prototype manager, a registry of available prototypes. Clients won’t manage prototypes themselves but will store and retrieve them from the registry using keys. A prototype manager is an associative store that returns the prototype matching a given key. ©SoftMoore ConsultingSlide 10

©SoftMoore ConsultingSlide 11 The clone() method is used to create a copy of an existing object. Shallow copy versus deep copy (field-by-field copy versus recursive copy) Shallow copy is acceptable for primitive types and immutable fields (e.g., int, String, Double, etc.) The clone() Method in Java e1 :Employee e2 e1 :Employee e2 :Employee

Expectations for the clone() Method For any object x Different objects x.clone() != x Same contents x.clone().equals(x) Same class x.clone().getClass() == x.getClass() Slide 12©SoftMoore Consulting

Slide 13 Implementing the clone() Method To support cloning correctly for subclasses Implement the Cloneable interface. –tagging (a.k.a. marker) interface –no methods; only purpose is to allow calls to clone() method Redefine the clone() method as public. –declared as private in class Object –required even if calling super.clone() is sufficient for implementation Enclose method body in a try/catch block that catches and ignores CloneNotSupportedException (can’t happen since we are implementing Cloneable, but we must still catch it anyway)

©SoftMoore ConsultingSlide 14 Implementing the clone() Method (continued) Call super.clone() to obtain an initial object –checks to see if this object implements the Cloneable interface (if not, throws a CloneNotSupportedException ) –creates a new instance of the class of this object –method clone() in class Object initializes all fields using a shallow copy Perform a deep copy of each mutable field –usually implemented by calling the clone() method for that field reference For primitive types and immutable fields, calling super.clone() is sufficient; i.e., no additional work to implement a deep copy is required.

Calling super.clone() The clone() method of a class usually calls super.clone() to copy fields inherited from the parent class and then does any custom copying procedures for mutable fields declared in the class. Similarly the clone() method of the super class would usually call super.clone(). Eventually the clone() method for class Object is called, which creates a new instance of the original class and performs a field-by field copy (“shallow copy”) to the new instance. ©SoftMoore ConsultingSlide 15

©SoftMoore ConsultingSlide 16 Overriding the clone() Method // In class Manager: Assume that Manager has several // primitive and immutable fields plus a mutable field // named "dept" of class Department. public Object clone() { try { Manager result = (Manager) super.clone(); // must handle mutable field dept result.dept = (Department) dept.clone(); return result; } catch (CloneNotSupportedException ex) { return null; // will never happen } clone() method for Department used to implement clone() method for Manager

Java Serialization and Cloning Java serialization is also a form of cloning. –performs a deep copy Serialization: write object as a sequence of bytes to a stream Deserialization: recreate brand new object on the other end with the original object’s data Serialization can be used for persistence (writing to a file stream) or for socket communication. ©SoftMoore ConsultingSlide 17 Note: Deserialization does not call the default constructor. It simply creates a blank object and fills in the fields with values retrieved via deserialization.

©SoftMoore ConsultingSlide 18 Serialization (continued) Object Serialization Java Application Class A instance int x = 4 B b = Class B instance Serialized Object(s) in Arbitrary File Java Application Class A instance int x = 4 B b = Class B instance DeserializationSerialization JVM External Storage

Using Serializaiton to Implement clone()... ObjectOutputStream out = new ObjectOutputStream(new ByteArrayOutputStream()); out.writeObject(this); out.flush(); ByteArrayInputStream bais = new ByteArrayInputStream(out.toByteArray()); ObjectInputStream in = new ObjectInputStream(bais); return in.readObject(); ©SoftMoore ConsultingSlide 19

When to Use the Prototype Pattern When classes to instantiate are specified at run-time. When you want to create another object at runtime that is a true copy of the object you are cloning. When object is complex and easier to clone. When object is time consuming to instantiate from scratch. When you don’t have access to a constructor for that object. When you have objects that differ in state in small ways. Slide 20©SoftMoore Consulting

References Prototype pattern (Wikipedia) Prototype Pattern (Object-Oriented Design) Prototype Design Pattern (SourceMaking) ©SoftMoore ConsultingSlide 21