CSC 301H, Introduction to Software Engineering Presented by Soheil Hassas Yeganeh Slides By Golnaz Elahi
Outline Design Pattern Concept Types of Design Patterns Overview of some Patterns: Abstract Factory Observer Command Strategy
Design Pattern Concept A design pattern is a template solution to a recurring design problem. Reusable design knowledge in form of Problem, Condition, Solution A design pattern expresses an idea rather than a fixed class combination.
Types of Design Patterns Creational Patterns: Creating a collection of objects Creating many versions of the collection at runtime Constraining the objects created Structural pattern: Representing a collection of related objects Controlling constraints on the number or style of objects, constructors, using external or distributed functionality, Behavioral pattern: Capturing behavior among a collection of objects A collection of objects exhibit functionality, separation of behavior from the object, interfaces without references.
Abstract Factory Pattern 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?
Abstract Factory Pattern Goals: Create a family of related objects chosen at runtime from several possible families. Independence from Initialization or Representation The system should be independent of how its products are created, composed or represented
AbstractFactory AbstractProductA Client CreateProductA CreateProductB ProductA1 ProductA2 ConcreteFactory1 AbstractProductB CreateProductA CreateProductB ProductB1 ProductB2 ConcreteFactory2 CreateProductA CreateProductB Initiation Assocation: Class ConcreteFactory2 initiates the associated classes ProductB2 and ProductA2
Abstract Factory Pattern
In between What is an interface? Public interface Bicycle { void changegear(int newvalue); void speedup(int increment); void applybrakes(int decrement); } Public class ACMEBicycle implements Bicycle { // remainder of this class implemented as before }
Observer Pattern
Analyzing a Java Example GUI Scenario: Two windows have been created First (InputForm) is for user input; Second (DisplayForm) is for display. When the data has been entered in the textfield in the first, the second window gets the message and display it with a dialog. Inner classes are used to handle observer pattern
Observer Implementation in Example - Structure
Observer Implementation in Example Behavior
Strategy Pattern Many different algorithms exists for the same task. Like: 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.
Policy Context ContextInterface() * Strategy AlgorithmInterface ConcreteStrategyA AlgorithmInterface() ConcreteStrategyB AlgorithmInterface() ConcreteStrategyC AlgorithmInterface() Policy decides which Strategy is best given the current Context
Applying a Strategy Pattern in a Database Application Database Search() Sort() Strategy * Strategy Sort() BubbleSort Sort() QuickSort Sort() MergeSort Sort()
Strategy Pattern
Strategy Pattern // MainApp test application class MainApp { public static void main(string[] args) { Context context; // Three contexts following different strategies context = new Context(new ConcreteStrategyA()); context.execute(); context = new Context(new ConcreteStrategyB()); context.execute(); context = new Context(new ConcreteStrategyC()); context.execute(); }
Strategy Pattern interface IStrategy { void execute(); }
Strategy Pattern class ConcreteStrategyA implements IStrategy { public void execute() { System.out.println( "Called ConcreteStrategyA.execute()" ); } } class ConcreteStrategyB implements IStrategy { public void execute() { System.out.println( "Called ConcreteStrategyB.execute()" ); } } class ConcreteStrategyC implements IStrategy { public void execute() { System.out.println( "Called ConcreteStrategyC.execute()" ); }
Strategy Pattern class Context { IStrategy strategy; // Constructor public Context(IStrategy strategy) { this.strategy = strategy; } } public void execute() { strategy.execute(); }
Strategy Pattern
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 standin for the real object The proxy creates the real object only if the user asks for it
Proxy pattern Subject Request() Proxy Request() realsubject RealSubject Request() 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.
Command Pattern 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
Client Invoker Command execute() Receiver action() binds ConcreteCommand execute() 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 ).