Presentation is loading. Please wait.

Presentation is loading. Please wait.

Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation.

Similar presentations


Presentation on theme: "Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation."— Presentation transcript:

1 Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation for cloning itself Client (GraphicTool) – creates a new object by asking a prototype to clone itself

2 Prototype Motivation

3

4 Prototype pattern Motivation Tool Manipulate() RotateTool Manipulate() p = prototype->Clone() while (user drags mouse) { p->Draw(new position) } insert p into drawing prototype GraphicTool Manipulate() Graphic Draw(position) Clone() EditBox Draw(position) Clone() Slider Draw(position) Clone() return copy of self

5 Prototype Structure Specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype.

6 Prototype pattern Structure p = prototype->Clone() Prototype Clone() prototype Client Operation() ConcretePrototype1 Clone() ConcretePrototype Clone() return copy of self

7 Prototype pattern Applicability Use the Prototype pattern when a system should be independent of how its products are created, composed, and represented; Where the system should be highly dynamic, and – when the classes to instantiate are specified at run-time; or – to avoid building a class hierarchy of factories that parallels the class hierarchy of products; or – when instances of a class can have one of only a few different combinations of state. It may be convenient to install a corresponding number of prototypes and clone them rather than instatiating the class manually, each time with an appropriate state

8 Consequences Adding and removing prototypes at run-time Specifying new prototypes by varying values – define new behavior through object composition - by specifying values for an objects variables - and not by defining a new class – Specifying new prototypes by varying the structure – Example: editors for circuit design; build a circuit out of sub- circuit Reduced subclassing – Prototype pattern can be used instead of Factory Method pattern to avoid building parallel hierarchies of creators and products – no need of creator hierarchy at all – Benefits for C++ realization Configuring application with subclasses dynamically

9 Implementation Using a prototype manager – when number of prototypes in the system is not fixed; – to keep a registry of available prototypes; – prototype manager is an associative store that returns the prototype matching the given key Implementing the Clone operation – “shallow copy versus deep copy” Initializing clones – need to initialize cloned object to values specified by client – can’t pass this values to Clone(), because their number will vary between classes of prototypes – define separate Initialize(…) operation to set clone’s internal state for each prototype class accordingly


Download ppt "Prototype pattern Participants Prototype (Graphic) – declared an interface for cloning itself ConcretePrototype (EditBox, Slider) – implements an operation."

Similar presentations


Ads by Google