Presentation is loading. Please wait.

Presentation is loading. Please wait.

March 200692.3913 Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem.

Similar presentations


Presentation on theme: "March 200692.3913 Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem."— Presentation transcript:

1 March 200692.3913 Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem in software design. A design pattern isn't a finished design that can be transformed directly into code; it is a description or template for how to solve a problem that can be used in many different situations.

2 March 200692.3913 Ron McFadyen2 Design Patterns Patterns originated as an architectural concept by Christopher Alexander. 1977: A Pattern Language: Towns, Buildings, Construction is a book on architecture - by Christopher Alexander, Sara Ishikawa and Murray Silverstein In 1994: Design Patterns: Elements of Reusable Object-Oriented Software -GoF First Pattern Languages of Programs conference Some online resourses: en.wikipedia.org/wiki/Design_pattern_(computer_science) www.dofactory.com

3 March 200692.3913 Ron McFadyen3 Design Patterns Design patterns are composed of several sections Format used by the Gang of Four. Pattern Name and Classification Intent Also Known As Motivation Applicability Structure Participants Collaboration Consequences Implementation Sample Code Known Uses Related Pattern Of particular interest are the Structure, Participants, and Collaboration sections.

4 March 200692.3913 Ron McFadyen4 Also known as publish subscribe The essence of this pattern is that one or more objects (called observers or listeners) are registered (or register themselves) to observe an event which may be raised by the observed object (the subject). The object which may raise an event maintains a collection of the observers. Observer Pattern

5 March 200692.3913 Ron McFadyen5 Observer Pattern The object that is responsible for the event is given the responsibility of monitoring for the event – this object is the subject. Objects that are interested in the event must register with the subject as interested parties – as observers. The subject will notify its observers when the event occurs. Two interfaces are used: a subscriber interface and a subject interface.

6 March 200692.3913 Ron McFadyen6 Observer Pattern Observer: objects that want to be notified of a certain event. An observer must have an update method whereby it is notified of an event. Subject: the object that triggers the event. It must implement: attach (observer) - add an observer to its list of observers detach (observer) - remove an observer from … notify () - goes through its list of observers calling each observer’s update method As needed - additional methods to allow an observer to get additional information

7 March 200692.3913 Ron McFadyen7 Interfaces «interface» Subject attach() detach() notify() «interface» Observer update()

8 March 200692.3913 Ron McFadyen8 Generic pattern «interface» Subject attach() detach() notify() «interface» Observer update() ConcreteSubject attach() detach() notify() … ConcreteObserver update() … *

9 March 200692.3913 Ron McFadyen9 Weather Station Example «interface» Subject attach() detach() notify() «interface» Observer update() WeatherData attach() detach() notify() CurrentConditionsDisplay update() display() * StatisticsDisplay update() display() ForecastDisplay update() display() «interface» DisplayElement display()

10 March 200692.3913 Ron McFadyen10 Weather Station Example «interface» Subject attach() detach() notify() «interface» Observer update() WeatherData attach() detach() notify() CurrentConditionsDisplay update() display() * StatisticsDisplay update() display() ForecastDisplay update() display() «interface» DisplayElement display() Observer Concrete observer Concrete subject observersubject Concrete observer

11 March 200692.3913 Ron McFadyen11 Object diagram What collection of objects exist? How are they related?

12 March 200692.3913 Ron McFadyen12 Behaviour What is the typical behaviour expressed with sequence diagrams?

13 March 200692.3913 Ron McFadyen13 Decorator pattern This design pattern allows new/additional behavior to be added to an existing method of an object dynamically Decorator works by wrapping a new "decorator" object around the original object, which is typically achieved by passing the original object as a parameter to the constructor of the decorator, with the decorator implementing the new functionality. The interface of the original object needs to be maintained by the decorator. Decorators are alternatives to subclassing. Subclassing adds behaviour at compile time whereas decorators provide new behaviour at runtime.

14 March 200692.3913 Ron McFadyen14 Decorator 1 component decorator Concrete component Concrete decoratorA Concrete decoratorB operation() Client

15 March 200692.3913 Ron McFadyen15 Decorator textbook example 1 component Beverage Condiment Decorator HouseBlend cost() getDescription getDescription() DarkRoast Decaf cost() Espresso cost() MilkMochaSoyWhip cost() getDescription() cost() getDescription() cost() getDescription() cost() getDescription()

16 March 200692.3913 Ron McFadyen16 Decorator textbook example Beverage Condiment Decorator HouseBlend DarkRoast Decaf Espresso MilkMochaSoyWhip Decorator Concrete decorator Concrete component Component Concrete decorator Concrete component

17 March 200692.3913 Ron McFadyen17 Object diagram What collection of objects exist? How are they related?

18 March 200692.3913 Ron McFadyen18 Behaviour What is the typical behaviour expressed with sequence diagrams?

19 March 200692.3913 Ron McFadyen19 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.

20 March 200692.3913 Ron McFadyen20 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.

21 March 200692.3913 Ron McFadyen21 Singleton pattern Structure? Behaviour?

22 March 200692.3913 Ron McFadyen22 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.

23 March 200692.3913 Ron McFadyen23 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

24 March 200692.3913 Ron McFadyen24 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?

25 March 200692.3913 Ron McFadyen25 Singleton pattern Example: Suppose we have a system of classes that need to use random numbers for some simulation purpose. A random number generator is typically used to generate a sequence of pseudo-random numbers; you provide a seed value that is used to begin the sequence. seed = (seed * 25214903917 +11) % 2 48 We can design a singleton random number generator class that all classes would reference. «singleton » SingleRandom setSeed() nextInt() getInstance()

26 March 200692.3913 Ron McFadyen26 Singleton pattern import java.io.*; public class DriverSingleRandom{ public static void main(String[] args){ // generate random numbers between 1 and 6 System.out.println("beginning listing"); SingleRandom r = SingleRandom.getInstance(); int seed = 101; r.setSeed(seed); for (int i=0;i<10;i++){ int result = Math.abs(r.nextInt())%6+1; System.out.println(result); } }// end main }

27 March 200692.3913 Ron McFadyen27 Singleton pattern import java.util.*; public class SingleRandom { private Random generator; private static SingleRandom instance; private SingleRandom() { generator = new Random(); } public void setSeed(int seed) { generator.setSeed(seed); } public int nextInt() { return generator.nextInt(); } public static synchronized SingleRandom getInstance() { if (instance == null) instance = new SingleRandom(); return instance;} }

28 March 200692.3913 Ron McFadyen28 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.

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

30 March 200692.3913 Ron McFadyen30 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

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

32 March 200692.3913 Ron McFadyen32 Command Example: User interfaces typically give you ways to issue a particular command Java Swing classes use the command pattern to perform application-specific functionality. Action objects are used. Button classes, menu items, etc. maintain a reference to an action defined by the Action interface. Whenever a Swing component activates, it calls its action’s (command’s) actionPerformed() method (execute() method). See sample files on web page


Download ppt "March 200692.3913 Ron McFadyen1 Design Patterns In software engineering, a design pattern is a generally repeatable solution to a commonly-occurring problem."

Similar presentations


Ads by Google