Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 200692.3913 Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly.

Similar presentations


Presentation on theme: "March 200692.3913 Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly."— Presentation transcript:

1 March 200692.3913 Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly one object is needed to coordinate actions across the system. Singleton pattern is implemented by creating a class with a method that creates a new instance of the object if one does not exist.

2 March 200692.3913 Ron McFadyen2 Singleton pattern If an instance already exists, it simply returns a reference to that object. To make sure that the object cannot be instantiated any other way, the constructor is made either private or protected. eager instantiation lazy instantiation - no resources until needed The singleton pattern must be carefully constructed in multi- threaded applications. getInstance() Singleton() Singleton Public Private

3 March 200692.3913 Ron McFadyen3 Singleton pattern Structure? Behaviour? How would singleton combine with observer if there should only be one subject? Could singleton and decorator combine? Could the same decorated object be decorated differently for different clients?

4 March 200692.3913 Ron McFadyen4 Factory method pattern We have a situation where objects (called products) are created. Products are organized into subclasses referred to as concrete products. A parallel hierarchy exists with a creator superclass organized with concrete creator subclasses. The concrete creators are responsible for instantiating concrete products. In general we say the Factory Method defers instantiation to subclasses. Factory Method defines an interface for creating an object, but let subclasses decide which class to instantiate.

5 March 200692.3913 Ron McFadyen5 Factory method – generic class diagram product = factoryMethod() return product Creator Concrete Creator1 createMethod() factoryMethod() Product Concrete Product1 Concrete Product2 Concrete Creator2 factoryMethod() Client

6 March 200692.3913 Ron McFadyen6 Factory method – generic class diagram Pizza = createPizza() PizzaStore NyPizza Store orderPizza() createPizza() Pizza NyCheese Pizza Chicago CheesePizza Chicago PizzaStore createPizza() main In the text example, the creator (specific pizza store) subclass is instantiated by testdrive, and then a number of products (specific pizzas) are created. The model works for whatever pizza store gets instantiated and its easy to incorporate new stores....... return new ChicagoCheesePizza()

7 March 200692.3913 Ron McFadyen7 Factory method – generic class diagram Pizza = createPizza() PizzaStore NyPizza Store orderPizza() createPizza() Pizza NyCheese Pizza Chicago CheesePizza Chicago PizzaStore createPizza() main...... FactoryMethod Abstract factory Concrete factory Concrete product Abstract product

8 March 200692.3913 Ron McFadyen8 Factory method pattern What is the behaviour for when main orders a Cheese Pizza when the store is NyPizzaStore?

9 March 200692.3913 Ron McFadyen9 Command The command pattern encapsulates a request or unit of work into an object. An invoker will ask a concrete command to perform its function and the concrete command will in turn ask some receiver to perform a pre-determined action.

10 March 200692.3913 Ron McFadyen10 Command – generic class diagram > Command Concrete Command Receiver Invoker Command Concrete commandreceiver command action1() action2() execute() setCommand() invoker

11 March 200692.3913 Ron McFadyen11 Command – text example > Command lightOn Command Light Remote control Command Concrete command receiver command on() off() execute() setCommand() invoker lightOff Command execute() Concrete command null Command execute() Concrete command { }

12 March 200692.3913 Ron McFadyen12 Command - behaviour :Light:RemoteControl execute() lightOn() execute() lightOff() :LightOn Command :LightOff Command :null Command execute()


Download ppt "March 200692.3913 Ron McFadyen1 Singleton pattern Singleton is designed to restrict instantiation of a class to one (or a few) objects. Useful when exactly."

Similar presentations


Ads by Google