Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation.

Similar presentations


Presentation on theme: "Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation."— Presentation transcript:

1 Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation

2 Creational Patterns ► Design patterns of this nature are an abstraction of the instantiation process. ► These encapsulate knowledge about which concrete classes the system uses. ► These patterns also hides how instances of these classes are created and put together. ► This adds up to huge flexibility on what, who, how and when “stuff” gets created.

3 What is a Prototype Design Pattern? ► This type of pattern uses classes and other objects as a prototypical instances, and then create objects by copying these prototypical instances – the prototypes – and using them to create the instantiated instances.

4 So how does Prototype fit in? ► If a function is parameterized by various prototypical classes, which it then copies and adds to the program, then you can change the program’s running composition by replacing the prototypical objects with different ones. You’re using prototypes! ► Lost because of words? Well, here come the pictures! ^_^

5 Photoshop Possible Programming ► This is one of the toolbars in Photoshop. Let’s say each button is an object of class “GraphicTool” and each of the jobs that these objects do can be described of “DrawingStuff”

6 Tool manipulate() RotateTool manipulate() GraphicTool manipulate() createGraphic() g = createGraphic() while (user drags mouse) { g->draw(new position) } insert g into drawing Pencil createGraphic() Erase createGraphic() DrawingStuff manipulate() createGraphic() Prototype …… UML Diagram …… Returns copy of self

7 Prototype Class Diagram Client operation() Prototype clone() ConcreteProto1 clone() ConcreteProto2 clone() prototype return copy of self p = prototype->clone() Graphic from http://vik.ktu.lt/moduliai/T120M021/Creational_DP_files/frame.htm Client – creates a new object by asking a prototype to clone itself. Prototype – declares an interface for cloning itself! Concrete Prototypes – implements an operation for cloning itself These describe the Participants

8 Consequences of Prototype ► Overall – this hides the concrete product classes from the client, thereby reducing the number of names clients know about. Moreover, this DP allows the client work with application-specific classes without modification. ► This SCREAMS of code re-usage – you could even use the “Tool” class (from Photoshop) for different types of programs – Like music writing software – and drawing stuff would then be “Notes”, “Clefs” or another concrete class!

9 Additional Benefits ► Adding and removing products at Run Time ► Specifying new objects by varying values ► Specifying new objects by varying structure ► Reduced Subclassing ► Configuring an application with classes dynamically

10 There’s a catch, right? ► Unfortunately, yes. ► The Clone() operation can be VERY complicated. This brings us to implementation…

11 Implementation of Prototype ► Using a prototype manager  Basically, using a registry for the prototypes. ► Implementing the Clone operation…  This is REALLY tricky when you have objects with Circular references.  Most languages provide shallow copies – they share some information from the original. Usually, you NEED a Deep copy – nothing shared! If your object has Save & Load operations, this is a lot easier to implement.

12 Implementation (cont) ► Initialize clones  Are we seeing a recurrence of the “Clones aren’t easy” idea – yep  You may have to create an Initialize function for your clones because clients may want to initialize some, if not all of the internal state values, and you typically can’t pass values into the “Clone” operation because Clone varies from object to object, and is inherited from the prototype class.

13 So…when should you use Prototype? ► Should use this when a system should be independent of how its products are created, composed and represented and one or more of the following:  When the classes to instantiate are specified at Run-Time  To avoid building a class hierarchy that’s really deep  When instances of a class have few differences

14 How does Prototype relate? ► Closely related to the other Creational Design Patterns. Many times, you can use these patterns together. ► Of the creational DP’s, it’s most competitive/similar to Abstract Factory. ► Also, designs that use the Composite and Decorator Structural Design Patterns often can really benefit from using Prototype.

15 Overview ► Prototype is used by making copies of concrete classes for objects, using clone(). ► Clone can be hard to code, but allows for huge amounts of flexibility. ► This is similar in idea to Templates, except rather than algorithms, it just creates default types of concrete classes. ► Once you understand Clone, this can save you a lot of work.

16 Any Questions? "I've been on courses about this: you stand at the front, you maintain eye contact with the students, and you get this wonderful flow of communication with the audience. Bollocks. Those people have never taught a group of Computer Science students." "Um, what was the question?" Tim Hopkins and a random CS2 student


Download ppt "Prototype Creational Design Pattern By Brian Cavanaugh September 22, 2003 Software, Design and Documentation."

Similar presentations


Ads by Google