Prototype Pattern
-
Upload
stockton-gavyn -
Category
Documents
-
view
45 -
download
2
description
Transcript of Prototype Pattern
![Page 1: Prototype Pattern](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/1.jpg)
Prototype PatternPrototype Pattern
Yiqiang Song Yiqiang Song 2/2/092/2/09
![Page 2: Prototype Pattern](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/2.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/3.jpg)
ProblemProblem
![Page 4: Prototype Pattern](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/4.jpg)
Prototype solutionPrototype solution
![Page 5: Prototype Pattern](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/5.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/6.jpg)
ExamplExamplee
Animal farmAnimal farm
![Page 7: Prototype Pattern](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/7.jpg)
Prototype Pattern Prototype Pattern Example code Example code
![Page 8: Prototype Pattern](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/8.jpg)
Prototype Pattern Prototype Pattern Example codeExample code
![Page 9: Prototype Pattern](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/9.jpg)
Prototype Pattern Prototype Pattern Example codeExample code
![Page 10: Prototype Pattern](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/10.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/11.jpg)
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](https://reader031.fdocuments.in/reader031/viewer/2022032006/56812c2a550346895d90a30a/html5/thumbnails/12.jpg)
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.