Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 1 Outline of the Lecture  Patterns covered  Composite:

Slides:



Advertisements
Similar presentations
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Advertisements

Chapter 8, Object Design: Design Patterns II Using UML, Patterns, and Java Object-Oriented Software Engineering.
CSE3308/CSC Software Engineering: Analysis and DesignLecture 5B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/CSC3080/DMS/2000/12.
Chapter 8, Object Design Reuse and Patterns II Using UML, Patterns, and Java Object-Oriented Software Engineering.
1 CS 501 Spring 2005 CS 501: Software Engineering Lecture 17 Object Oriented Design 3.
CS CS 5150 Software Engineering Lecture 17 Object Oriented Design 3.
Chapter 8, Object Design Introduction to Design Patterns
Chapter 8, Object Design: Reuse and Patterns (Lecture II)
1 CS 501 Spring 2008 CS 501: Software Engineering Lectures 17 & 18 Object Oriented Design 3 & 4.
Design Patterns Based on Design Patterns. Elements of Reusable Object-Oriented Software. by E.Gamma, R. Helm, R. Johnson,J. Vlissides.
Chapter 8 Object Design Reuse and Patterns. Finding Objects The hardest problems in object-oriented system development are: –Identifying objects –Decomposing.
CS CS 5150 Software Engineering Lecture 16 Object Oriented Design 2.
CS CS 5150 Software Engineering Lecture 16 Object Oriented Design 2.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
Reuse Activities Selecting Design Patterns and Components
1 CS 501 Spring 2007 CS 501: Software Engineering Lectures 17 & 18 Object Oriented Design 3 & 4.
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Chapter 8, Object Design: Design Patterns II Using UML, Patterns, and Java Object-Oriented Software Engineering.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Object Design: Reuse and Patterns III.
Chapter 8, Object Design Reuse and Patterns II Using UML, Patterns, and Java Object-Oriented Software Engineering.
Design Patterns.
Chapter 8, Object Design: Design Patterns II Using UML, Patterns, and Java Object-Oriented Software Engineering.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
UNB CS3013 Software Engineering II lectures adapted from Bernd Bruegge & Allen Dutoit, Object-Oriented Software Engineering: Conquering Complex and Changing.
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Object Design: Reuse and Patterns III.
Conquering Complex and Changing Systems Object-Oriented Software Engineering Chapter 6, System Design Design Patterns II.
Using UML, Patterns, and Java Object-Oriented Software Engineering Object Design II: Design Patterns Bernd Bruegge Applied Software Engineering Technische.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
CEN 4010 Ninth Lecture March 4, 2005 Introduction to Software Engineering (CEN-4010) Spring 2005 Instructor: Masoud Sadjadi
Proxy Pattern: Motivation
COP 3331 Object-Oriented Analysis and Design 1 Patterns Again  Review of design pattern concepts  What is a design pattern?  Modifiable designs  Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
CS 240, Prof. Sarwar Slide 1 CS 240: Software Project Fall 2003 Sections 1 & 2 Dr. Badrul M. Sarwar San Jose State University Lecture #22.
Structural Design Patterns
ECE450S – Software Engineering II
Using UML, Patterns, and Java Object-Oriented Software Engineering Chapter 8, Object Design: Introduction to Design Patterns.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 1 Software Engineering October 17, 2001 Design.
Example to motivate discussion We have two lists (of menu items) one implemented using ArrayList and another using Arrays. How does one work with these.
Proxy Pattern defined The Proxy Pattern provides a surrogate or placeholder for another object to control access to it by creating a representative object.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Chapter 8 Object Design Reuse and Patterns. More Patterns Abstract Factory: Provide manufacturer independence Builder: Hide a complex creation process.
CS 5150 Software Engineering Lecture 16 Program Design 3.
Reference – Object Oriented Software Development Using Java - Jia COP 3331 Object Oriented Analysis and Design Chapter 10 – Patterns Jean Muhammad.
COP 3331 Object-Oriented Analysis and Design 1 Design Overview  Design Overview (Ch 6)  Review of RAD  System Design  System Design Concepts  System.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
CEN 5011 Sixth Lecture (2 nd part) Oct 20, 2004 Advance Software Engineering (CEN-5011) Fall 2004 Instructor: Masoud Sadjadi
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Chapter 8 Object Design: Reuse and Patterns.
Chapter 8 Object Design: Reuse and Patterns 2.
Design Patterns: MORE Examples
Chapter 8, Object Design: Reuse and Patterns III
Chapter 8, Object Design Introduction to Design Patterns
Chapter 8 Object Design: Reuse and Patterns 2.
Chapter 8, Design Patterns Bridge
Chapter 8 Object Design: Reuse and Patterns.
Chapter 8, Object Design Reuse and Patterns II
Chapter 6, System Design Design Patterns
Presented by Igor Ivković
Chapter 8, Object Design Introduction to Design Patterns
Design Patterns - A few examples
Chapter 8, Object Design Introduction to Design Patterns
Informatics 122 Software Design II
Informatics 122 Software Design II
Chapter 8, Design Patterns Introduction
Presented by Igor Ivković
Chapter 8, DesignPatterns Facade
Presentation transcript:

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 1 Outline of the Lecture  Patterns covered  Composite: Model dynamic aggregates  Facade: Interfacing to subsystems  Adapter: Interfacing to existing systems (legacy systems)  Bridge: Interfacing to existing and future systems  Abstract Factory  Proxy  Command  Observer  Strategy

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 2 Another Source for Finding Objects : Design Patterns  What are Design Patterns?  A design pattern describes a problem which occurs over and over again in our environment  Then it describes the core of the solution to that problem, in such a way that you can use the this solution a million times over, without ever doing it the same twice

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 3 What is common between these definitions?  Definition Software System  A software system consists of subsystems which are either other subsystems or collection of classes  Definition Software Lifecycle:  The software lifecycle consists of a set of development activities which are either other actitivies or collection of tasks

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 4 Introducing the Composite Pattern  Models tree structures that represent part-whole hierarchies with arbitrary depth and width.  The Composite Pattern lets client treat individual objects and compositions of these objects uniformly Client Component Leaf Operation() Composite Operation() AddComponent RemoveComponent() GetChild() Children

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 5 What is common between these definitions?  Software System:  Definition: A software system consists of subsystems which are either other subsystems or collection of classes  Composite: Subsystem (A software system consists of subsystems which consists of subsystems, which consists of subsystems, which...)  Leaf node: Class  Software Lifecycle:  Definition: The software lifecycle consists of a set of development activities which are either other actitivies or collection of tasks  Composite: Activity (The software lifecycle consists of activities which consist of activities, which consist of activities, which....)  Leaf node: Task

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 6 Modeling a Software System with a Composite Pattern Software System Class Subsystem Children * User

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 7 Modeling the Software Lifecycle with a Composite Pattern Software Lifecycle Task Activity Children * Manager

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 8 The Composite Patterns models dynamic aggregates University School Department Organization Chart (variable aggregate): Dynamic tree (recursive aggregate): Car Fixed Structure: DoorsWheels BatteryEngine Compound Statement Simple Statement Program Block * * * * * * Dynamic tree (recursive aggregate): Composite Pattern

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 9 Graphic Applications also use Composite Patterns Client Graphic Circle Draw() Picture Draw() Add(Graphic g) RemoveGraphic) GetChild(int) Children Line Draw() The Graphic Class represents both primitives (Line, Circle) and their containers (Picture)

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 10  Delegation is used to bind an Adapter and an Adaptee  Interface inheritance is use to specify the interface of the Adapter class.  Target and Adaptee (usually called legacy system) pre-exist the Adapter.  Target may be realized as an interface in Java. Adapter pattern Client ClientInterface Request() LegacyClass ExistingRequest() Adapter Request() adaptee

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 11 Adapter Pattern  “Convert the interface of a class into another interface clients expect.”  The adapter pattern lets classes work together that couldn’t otherwise because of incompatible interfaces  Used to provide a new interface to existing legacy components (Interface engineering, reengineering).  Also known as a wrapper  Two adapter patterns:  Class adapter:  Uses multiple inheritance to adapt one interface to another  Object adapter:  Uses single inheritance and delegation  Object adapters are much more frequent. We will only cover object adapters (and call them therefore simply adapters)

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 12 Bridge Pattern  Use a bridge to “decouple an abstraction from its implementation so that the two can vary independently”. (From [Gamma et al 1995])  Also know as a Handle/Body pattern.  Allows different implementations of an interface to be decided upon dynamically.

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 13 Using a Bridge  The bridge pattern is used to provide multiple implementations under the same interface.  Examples: Interface to a component that is incomplete, not yet known or unavailable during testing  JAMES Project: if seat data is required to be read, but the seat is not yet implemented, known, or only available by a simulation, provide a bridge: VIP Seat (in Vehicle Subsystem) SeatImplementation Stub Code SARTSeat AIMSeat imp GetPosition() SetPosition()

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 14 Seat Implementation public interface SeatImplementation { public int GetPosition(); public void SetPosition(int newPosition); } public class Stubcode implements SeatImplementation { public int GetPosition() { // stub code for GetPosition }... } public class AimSeat implements SeatImplementation { public int GetPosition() { // actual call to the AIM simulation system } …. } public class SARTSeat implements SeatImplementation { public int GetPosition() { // actual call to the SART seat simulator }... }

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 15 Bridge Pattern

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 16 Adapter vs Bridge  Similarities:  Both are used to hide the details of the underlying implementation.  Difference:  The adapter pattern is geared towards making unrelated components work together  Applied to systems after they’re designed (reengineering, interface engineering).  A bridge, on the other hand, is used up-front in a design to let abstractions and implementations vary independently.  Green field engineering of an “extensible system”  New “beasts” can be added to the “object zoo”, even if these are not known at analysis or system design time.

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 17 Facade Pattern  Provides a unified interface to a set of objects in a subsystem.  A facade defines a higher-level interface that makes the subsystem easier to use (i.e. it abstracts out the gory details)  Facades allow us to provide a closed architecture

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 18 Design Example  Subsystem 1 can look into the Subsystem 2 (vehicle subsystem) and call on any component or class operation at will.  This is “Ravioli Design”  Why is this good?  Efficiency  Why is this bad?  Can’t expect the caller to understand how the subsystem works or the complex relationships within the subsystem.  We can be assured that the subsystem will be misused, leading to non-portable code Subsystem 2 Subsystem 1 AIM Card SA/RT Seat

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 19 Subsystem Design with Façade, Adapter, Bridge  The ideal structure of a subsystem consists of  an interface object  a set of application domain objects (entity objects) modeling real entities or existing systems  Some of the application domain objects are interfaces to existing systems  one or more control objects  We can use design patterns to realize this subsystem structure  Realization of the Interface Object: Facade  Provides the interface to the subsystem  Interface to existing systems: Adapter or Bridge  Provides the interface to existing system (legacy system)  The existing system is not necessarily object-oriented!

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 20 Realizing an Opaque Architecture with a Facade  The subsystem decides exactly how it is accessed.  No need to worry about misuse by callers  If a façade is used the subsystem can be used in an early integration test  We need to write only a driver VIP Subsystem AIM Card SA/RT Seat Vehicle Subsystem API

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 21 Design Patterns encourage reusable Designs  A facade pattern should be used by all subsystems in a software system. The façade defines all the services of the subsystem.  The facade will delegate requests to the appropriate components within the subsystem. Most of the time the façade does not need to be changed, when the component is changed,  Adapters should be used to interface to existing components.  For example, a smart card software system should provide an adapter for a particular smart card reader and other hardware that it controls and queries.  Bridges should be used to interface to a set of objects  where the full set is not completely known at analysis or design time.  when the subsystem must be extended later after the system has been deployed and client programs are in the field(dynamic extension).  Model/View/Controller should be used  when the interface changes much more rapidly than the application domain.

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 22 Summary  Design patterns are partial solutions to common problems such as  such as separating an interface from a number of alternate implementations  wrapping around a set of legacy classes  protecting a caller from changes associated with specific platforms.  A design pattern is composed of a small number of classes  use delegation and inheritance  provide a robust and modifiable solution.  These classes can be adapted and refined for the specific system under construction.  Customization of the system  Reuse of existing solutions

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 23 Summary II  Composite Pattern:  Models trees with dynamic width and dynamic depth  Facade Pattern:  Interface to a subsystem  closed vs open architecture  Adapter Pattern:  Interface to reality  Bridge Pattern:  Interface to reality and prepare for future

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 24 Additional Design Heuristics  Never use implementation inheritance, always use interface inheritance  A subclass should never hide operations implemented in a superclass  If you are tempted to use implementation inheritance, use delegation instead

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 25 Java‘s AWT library can be modeled with the component pattern Graphics Component Button TextField Label * TextArea Text Component Container add(Component c) paint(Graphics g) getGraphics()

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 26 Proxy Pattern: Motivation  It is 15:00pm. I am sitting at my 14.4 baud modem connection and retrieve a fancy web site from the US, This is prime web time all over the US. So I am getting 10 bits/sec.  What can I do?

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 27 Proxy Pattern  What is expensive?  Object Creation  Object Initialization  Defer object creation and object initialization to the time you need the object  Proxy pattern:  Reduces the cost of accessing objects  Uses another object (“the proxy”) that acts as a stand-in for the real object  The proxy creates the real object only if the user asks for it

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 28 Proxy pattern  Interface inheritance is used to specify the interface shared by Proxy and RealSubject.  Delegation is used to catch and forward any accesses to the RealSubject (if desired)  Proxy patterns can be used for lazy evaluation and for remote invocation.  Proxy patterns can be implemented with a Java interface. Subject Request() RealSubject Request() Proxy Request() realSubject

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 29 Proxy Applicability  Remote Proxy  Local representative for an object in a different address space  Caching of information: Good if information does not change too often  Virtual Proxy  Object is too expensive to create or too expensive to download  Proxy is a standin  Protection Proxy  Proxy provides access control to the real object  Useful when different objects should have different access and viewing rights for the same document.  Example: Grade information for a student shared by administrators, teachers and students.

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 30 Virtual Proxy example  Images are stored and loaded separately from text  If a RealImage is not loaded a ProxyImage displays a grey rectangle in place of the image  The client cannot tell that it is dealing with a ProxyImage instead of a RealImage  A proxy pattern can be easily combined with a Bridge Image boundingBox() draw() realSubject RealImage boundingBox() draw() ProxyImage boundingBox() draw()

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 31 Towards a Pattern Taxonomy  Structural Patterns  Adapters, Bridges, Facades, and Proxies are variations on a single theme:  They reduce the coupling between two or more classes  They introduce an abstract class to enable future extensions  They encapsulate complex structures  Behavioral Patterns  Here we are concerned with algorithms and the assignment of responsibilies between objects: Who does what?  Behavorial patterns allow us to characterize complex control flows that are difficult to follow at runtime.  Creational Patterns  Here we our goal is to provide a simple abstraction for a complex instantiation process.  We want to make the system independent from the way its objects are created, composed and represented.

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 32 A Pattern Taxonomy Pattern Structural Pattern Behavioral Pattern Creational Pattern AdapterBridgeFacade Proxy Command ObserverStrategy Abstract Factory Builder Pattern Command

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 33 Command Pattern: Motivation  You want to build a user interface  You want to provide menus  You want to make the user interface reusable across many applications  You cannot hardcode the meanings of the menus for the various applications  The applications only know what has to be done when a menu is selected.  Such a menu can easily be implemented with the Command Pattern

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 34 Command pattern  Client creates a ConcreteCommand and binds it with a Receiver.  Client hands the ConcreteCommand over to the Invoker which stores it.  The Invoker has the responsibility to do the command (“execute” or “undo”). Command execute() Receiver action() Client Invoker ConcreteCommand execute() binds

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 35 Command pattern Applicability  “Encapsulate a request as an object, thereby letting you  parameterize clients with different requests,  queue or log requests, and  support undoable operations.”  Uses:  Undo queues  Database transaction buffering

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 36 Observer pattern  “Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.”  Also called “Publish and Subscribe”  Uses:  Maintaining consistency across redundant state  Optimizing batch changes to maintain consistency

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 37 Observer pattern (continued) 9DesignPatterns2.ppt Observers Subject

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 38 Observer pattern (cont’d) Observer update() Subject attach(observer) detach(observer) notify() ConcreteSubject getState() setState(newState) subjectState ConcreteObserver update() observerState observers subject *  The Subject represents the actual state, the Observers represent different views of the state.  Observer can be implemented as a Java interface.  Subject is a super class (needs to store the observers vector) not an interface.

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 39 Sequence diagram for scenario: Change filename to “foo” getState() update() aListViewanInfoViewaFile setState(“foo”) notify() Attach() “foo” Subject goes through all its observers and calls update() on them, asking for the new state is decoupled from the notification

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 40 Animated Sequence diagram getState() aListViewanInfoViewaFile notify() Attach() “foo” setState(“foo”) update()

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 41 Observer pattern implementation in Java // import java.util; public class Observable extends Object { public void addObserver(Observer o); public void deleteObserver(Observer o); public boolean hasChanged(); public void notifyObservers(); public void notifyObservers(Object arg); } public abstract interface Observer { public abstract void update(Observable o, Object arg); } public class Subject extends Observable{ public void setState(String filename); public string getState(); }

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 42 A Pattern Taxonomy Pattern Structural Pattern Behavioral Pattern Creational Pattern AdapterBridgeFacade Proxy Command ObserverStrategy Abstract Factory Builder Pattern Command Strategy

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 43 Strategy Pattern  Many different algorithms exists for the same task  Examples:  Breaking a stream of text into lines  Parsing a set of tokens into an abstract syntax tree  Sorting a list of customers  The different algorithms will be appropriate at different times  Rapid prototyping vs delivery of final product  We don’t want to support all the algorithms if we don’t need them  If we need a new algorithm, we want to add it easily without disturbing the application using the algorithm

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 44 Strategy Pattern Strategy AlgorithmInterface Context ContextInterface() ConcreteStrategyC AlgorithmInterface() * ConcreteStrategyB AlgorithmInterface() ConcreteStrategyA AlgorithmInterface() Policy Policy decides which Strategy is best given the current Context

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 45 Applying a Strategy Pattern in a Database Application Strategy Sort() Database Search() Sort() Strategy * BubbleSort Sort() QuickSort Sort() MergeSort Sort()

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 46 Applicability of Strategy Pattern  Many related classes differ only in their behavior. Strategy allows to configure a single class with one of many behaviors  Different variants of an algorithm are needed that trade-off space against time. All these variants can be implemented as a class hierarchy of algorithms

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 47 A Pattern Taxonomy Pattern Structural Pattern Behavioral Pattern Creational Pattern AdapterBridgeFacade Proxy Command ObserverStrategy Abstract Factory Builder Pattern Command Strategy Abstract Factory

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 48 Abstract Factory Motivation  2 Examples  Consider a user interface toolkit that supports multiple looks and feel standards such as Motif, Windows 95 or the finder in MacOS.  How can you write a single user interface and make it portable across the different look and feel standards for these window managers?  Consider a facility management system for an intelligent house that supports different control systems such as Siemens’ Instabus, Johnson & Control Metasys or Zumtobe’s proprietary standard.  How can you write a single control system that is independent from the manufacturer?

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 49 Abstract Factory CreateProductA CreateProductB CreateProductA CreateProductB AbstractProductA ProductA1 ProductA2 AbstractProductB ProductB1 ProductB2 ConcreteFactory1 CreateProductA CreateProductB ConcreteFactory2 Client Initiation Assocation: Class ConcreteFactory2 initiates the associated classes ProductB2 and ProductA2

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 50 Applicability for Abstract Factory Pattern  Independence from Initialization or Represenation:  The system should be independent of how its products are created, composed or represented  Manufacturer Independence:  A system should be configured with one family of products, where one has a choice from many different families.  You want to provide a class library for a customer (“facility management library”), but you don’t want to reveal what particular product you are using.  Constraints on related products  A family of related products is designed to be used together and you need to enforce this constraint  Cope with upcoming change:  You use one particular product family, but you expect that the underlying technology is changing very soon, and new products will appear on the market.

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 51 Example: A Facility Management System for the Intelligent Workplace IntelligentWorkplace InitLightSystem InitBlindSystem InitACSystem InitLightSystem InitBlindSystem InitACSystem LightBulb InstabusLight Controller ZumbobelLight Controller Blinds InstabusBlind Controller ZumtobelBlind Controller SiemensFactory InitLightSystem InitBlindsystem InitACSystem ZumtobelFactory Facility Mgt System

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 52 Summary  Structural Patterns  Focus: How objects are composed to form larger structures  Problems solved:  Realize new functionality from old functionality,  Provide flexibility and extensibility  Behavioral Patterns  Focus: Algorithms and the assignment of responsibilities to objects  Problem solved:  Too tight coupling to a particular algorithm  Creational Patterns  Focus: Creation of complex objects  Problems solved:  Hide how complex objects are created and put together

Bernd Bruegge & Allen H. Dutoit Object-Oriented Software Engineering: Using UML, Patterns, and Java 53 Conclusion  Design patterns  Provide solutions to common problems.  Lead to extensible models and code.  Can be used as is or as examples of interface inheritance and delegation.  Apply the same principles to structure and to behavior.  Design patterns solve all your software engineering problems  My favorites: Composite, Bridge, Builder and Observer