Presentation is loading. Please wait.

Presentation is loading. Please wait.

Design Patterns Outline

Similar presentations


Presentation on theme: "Design Patterns Outline"— Presentation transcript:

1 Design Patterns Outline
What is a Design Pattern One Example: Proxy (structural) Some Object-oriented Design Principles Some Examples: Adapter (structural) Observer (behavioral) Abstract Factory (creational) 1

2 Design Pattern “Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice” [2] And they are meant for idea reuse, design reuse and also code reuse Design patterns help to write good programs. 2

3 A Pioneer Book on Design Patterns:
Design Patterns elements of Reusable Object-Oriented Software[2] The book explains what is meant by a design pattern and 23 patterns are presented in catalog form 3

4 What is a Design Pattern?
A design pattern is extracted/abstracted from existing software solutions; they represent abstract knowledge about how to solve a design problem. Design patterns are object-oriented and typically presented in UML 4

5 Anti-Patterns – a Design to Avoid
From Wikipedia In computer science, anti-patterns are specific repeated practices that appear initially to be beneficial, but ultimately result in bad consequences that outweigh the hoped-for advantages. Languages have built-in patterns, e.g. handling of list in Lisp – also called idiom-patterns! 5

6 According to [2] a Pattern has Four Essential Elements:
1. Pattern name. 2. The problem describes when to apply the pattern. 3. The solution describes the elements (typically classes and objects) that makes up the design, their relationships, responsibilities, and collaborations. 4. The consequences are the results and tradeoffs of applying the pattern (time, space, language, flexibility, portability). 6

7 Classifying the Design Patterns after Purpose
Creational: Describe how objects are created and configured. Structural: Describe composition of objects. Behavioral: Describes the way objects interact and distribute responsibility. 7

8 Design Patterns Outline
What is a Design Pattern One Example: Proxy (structural) Some Object-oriented Design Principles Some Examples: Adapter (structural) Observer (behavioral) Abstract Factory (creational) 8

9 Proxy (structural) [2] Purpose:
Provide a surrogate or placeholder for another object to control access to it. 9

10 <<interface>> Subject
The Proxy Pattern <<interface>> Subject request() Client refersTo RealSubject Proxy request() request() refersTo-> request() 10

11 Proxy Pattern - Interaction Diagram
:Client :Proxy :RealSubject request() request() 11

12 Design Patterns Outline
What is a Design Pattern One Example: Proxy (structural) Some Object-oriented Design Principles Some Examples: Adapter (structural) Observer (behavioral) Abstract Factory (creational) 12

13 Some Object-Oriented Design Principles
Keep the accessibility to classes and objects low. E.g. by: Using a high level of abstraction so that implementation details are hidden. Using encapsulation (e.g. getters / setters). Reference to interfaces and not to implementation classes. Abstraction as opposed to referencing implementation details! Program to an interface – allows change of implementation.

14 Open/Closed Principle
From Wikipedia Software entities (classes, modules, functions, etc.) should be open for extension, but closed for modification; that is, such an entity can allow its behaviour to be modified without altering its source code. Two ways to realize this: That class could reuse coding from the original class through inheritance. The use of abstracted interfaces, where the implementations can be changed and multiple implementations could be created. This is especially valuable in a production environment, where changes to source code may necessitate code reviews, unit tests, and other such procedures to qualify it for use in a product: Code obeying the principle doesn't change when it is extended, and therefore needs no such effort.

15 Liskov Substitution Principle
From Wikipedia In object-oriented programming, the Liskov substitution principle is a particular definition of subtype that was introduced by Barbara Liskov and Jeannette Wing in a 1993 paper entitled Family Values: A Behavioral Notion of Subtyping. The principle was formulated succinctly as follows: Let q(x) be a property provable about objects x of type T. Then q(y) should be true for objects y of type S where S is a subtype of T. Thus, Liskov and Wing's notion of "subtype" is based on the notion of substitutability; that is, if S is a subtype of T, then objects of type T in a program may be replaced with objects of type S without altering any of the desirable properties of that program (e.g., correctness).

16 Design Patterns Outline
What is a Design Pattern One Example: Proxy (structural) Some Object-oriented Design Principles Some Examples: Adapter (structural) Observer (behavioral) Abstract Factory (creational) 16

17 Adapter (structural) Adapter pattern – From Wikipedia, the free encyclopedia Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. Example: Temperature reading from different sensors.

18 Design Patterns Outline
What is a Design Pattern One Example: Proxy (structural) Some Object-oriented Design Principles Some Examples: Adapter (structural) Observer (behavioral) Abstract Factory (creational) 18

19 Observer Pattern (behavioral)
Defines a one-to-many dependency between objects so that when one object changes state, all it dependents are notified and updated automatically. This resembles the event source and listener structure of Java Beans. 19

20 Observer Pattern Model
observers Subject Observer update() * attach(Observer) detach(Observer) notify() for all o in observers{ o.update(); } subject ConcreteSubject ConcreteObserver Invariant: observeState = subjectState subjectState observerState getSubjectState() setSubjectState() update() return subjectState observeState = subject.getState() 20

21 Subject-Observer

22 Listener Pattern in Java beans
Button addActionListener(ActionListener) removeActionListener(ActionListener) processActionEvent(ActionEvent e) actionListener * <<interface>> ActionListener actionPerformed(ActionEvent) ConcreteListener actionPerformed(ActionEvent) for all l in actionlistener{ l. actionPerformed(e); } MyButton Typically not necessary. Button class used directly ActionEvent getActionCommand() Returns the command name associated with this action. Drop! 22

23 Design Patterns Outline
What is a Design Pattern One Example: Proxy (structural) Some Object-oriented Design Principles Some Examples: Adapter (structural) Observer (behavioral) Abstract Factory (creational) 23

24 Abstract Factory (Creational) [2]
Provides an interface for creating families of related or dependent objects without specifying their concrete classes. Example: An application supporting multiple look-and-feel standards like Windows and Motif. Most of the code should be without knowledge of which look-and-feel that is used.

25 Abstract Factory: Example
WidgetFactory createScrollBar() createWindow() Client ScrollBar WindowsScrollBar MotifScrollBar MotifFactory createScrollBar() createWindow() WindowsFactory WidgetFactory is abstract, so either MotifFactory or WindowsFactory is instantiated “instantiate” Window WindowsWindow MotifWindow

26 Abstract Factory: Object Diagram
WidgetFactory factory = new MotifFactory(); ... ScrollBar sb = factory.createScrollBar(); … Window win = factory.createWindow() Motif look-and-feel is selected; CreateScrollBar and CreateWindow are called. factory:MotifFactory :Client sb:MotifScrollBar win:MotifWindow

27 Abstract Factory: a General Class Diagram
Client AbstractProductA createProductA() createProductB() “instantiate” ProductA2 ProductA1 ConcreteFactory1 ConcreteFactory2 AbstractProductB createProductA() createProductB() createProductA() createProductB() “instantiate” ProductB2 ProductB1 “instantiate” “instantiate”

28 Abstract factory pattern - From Wikipedia, the free encyclopedia
Use of interfaces

29 Abstract factory pattern - From Wikipedia, the free encyclopedia
Use of interfaces

30 References [1] Christopher Alexander et al.: A Pattern Language Oxford University Press, New York, 1977 [2] Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides: Design Patterns Elements of Reusable Object-oriented Software Addison-Wesley, 1995 [3] Grady Booch, James Rumbaugh, Ivar Jacobson: The Unified Modeling Language User Guide Addison-Wesley, 1999 [4] Clemens Szyperski: Component Software Beyond Object-Oriented Programming Addison-Wesley, 1999 30


Download ppt "Design Patterns Outline"

Similar presentations


Ads by Google