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?


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