12
Prototype Pattern Prototype Pattern Yiqiang Song Yiqiang Song 2/2/09 2/2/09

Prototype Pattern

Embed Size (px)

DESCRIPTION

Prototype Pattern. Yiqiang Song 2/2/09. Prototype Pattern. A creational pattern Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Problem. Prototype solution. Prototype Pattern UML. Participants: • Prototype - PowerPoint PPT Presentation

Citation preview

Page 1: Prototype Pattern

Prototype PatternPrototype Pattern

Yiqiang Song Yiqiang Song 2/2/092/2/09

Page 2: Prototype Pattern

Prototype PatternPrototype Pattern

► A creational patternA creational pattern► Specify the kinds of objects to create using a Specify the kinds of objects to create using a

prototypical instance, and create new objects prototypical instance, and create new objects by copying this prototypeby copying this prototype

Page 3: Prototype Pattern

ProblemProblem

Page 4: Prototype Pattern

Prototype solutionPrototype solution

Page 5: Prototype Pattern

Prototype Pattern UMLPrototype Pattern UML

Participants:• Prototypeo declares an interface for cloning itself.• ConcretePrototype o implements an operation for cloning itself.• Cliento creates a new object by asking a prototype to clone itself.

Page 6: Prototype Pattern

ExamplExamplee

Animal farmAnimal farm

Page 7: Prototype Pattern

Prototype Pattern Prototype Pattern Example code Example code

Page 8: Prototype Pattern

Prototype Pattern Prototype Pattern Example codeExample code

Page 9: Prototype Pattern

Prototype Pattern Prototype Pattern Example codeExample code

Page 10: Prototype Pattern

Prototype PatternPrototype Pattern

When to UseWhen to Use►When product creation should be

decoupled from system behavior►When to avoid subclasses of an object

creator in the client application ►When creating an instance of a class is

time-consuming or complex in some way.

Page 11: Prototype Pattern

Consequences of Prototype Consequences of Prototype PatternPattern

► Hides the concrete product classes Hides the concrete product classes from the clientfrom the client

► Adding/removing of prototypes at run-Adding/removing of prototypes at run-timetime

► Allows specifying new objects by Allows specifying new objects by varying values or structurevarying values or structure

►Reducing the need for sub-classingReducing the need for sub-classing

Page 12: Prototype Pattern

Drawbacks of Prototype Drawbacks of Prototype Pattern Pattern

►It is built on the method .clone(), which could be complicated sometimes in terms of shallow copy and deep copy. Moreover, classes that have circular references to other classes cannot really be cloned.