Prototype Pattern

Post on 30-Dec-2015

45 views 2 download

Tags:

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

Transcript of Prototype Pattern

Prototype PatternPrototype Pattern

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

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

ProblemProblem

Prototype solutionPrototype solution

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.

ExamplExamplee

Animal farmAnimal farm

Prototype Pattern Prototype Pattern Example code Example code

Prototype Pattern Prototype Pattern Example codeExample code

Prototype Pattern Prototype Pattern Example codeExample code

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.

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

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.