ECE 355 Design Patterns Tutorial Part 3

Slides:



Advertisements
Similar presentations
1 Structural Design Patterns - Neeraj Ray. 2 Structural Patterns - Overview n Adapter n Bridge n Composite n Decorator.
Advertisements

Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)
Linzhang Wang Dept. of Computer Sci&Tech, Nanjing University The Bridge Pattern.
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.
Bridge The decoupling of abstraction and implementation.
Software Design & Documentation – Design Pattern: Command Design Pattern: Command Christopher Lacey September 15, 2003.
Chapter 8, Object Design Introduction to Design Patterns
ECE 355 Design Patterns Tutorial Part 3 Presented by Igor Ivković
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.
Design Pattern – Bridge (Structural) References Yih-shoung Chen, Department of Information Engineering, Feng Chia University,Taiwan, R.O.C. The Bridge.
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ć
1 CS 501 Spring 2007 CS 501: Software Engineering Lectures 17 & 18 Object Oriented Design 3 & 4.
1 ECE 355: Software Engineering CHAPTER 8 Instructor Kostas Kontogiannis.
Programming Languages and Paradigms Object-Oriented Programming.
Design Patterns.
Smart Reference Proxy Provides additional actions whenever an object is referenced (e.g., counting the number of references to the object) Firewall Proxy.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
The Factory Method Design Pattern Motivation: Class / Type separation – Abstract class serves as type definition and concrete class provides implementation.
Design Pattern Dr. Zhen Jiang West Chester University url:
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
Define an interface for creating an object, but let subclasses decide which class to instantiate.
CS 590L – Distributed Component Architecture 02/20/2003Uttara Paingankar1 Design Patterns: Factory Method The factory method defines an interface for creating.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
Structural Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Proxy Design Pattern By:Diksha Agarwal.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
Bridge Bridge is used when we need to decouple an abstraction from its implementation so that the two can vary independently. This type of design pattern.
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.
The Proxy Pattern (Structural) ©SoftMoore ConsultingSlide 1.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
1 Lecture Material Design Patterns Visitor Client-Server Factory Singleton.
Jim Fawcett CSE776 – Design Patterns Summer 2006
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Factory Method Pattern
Strategy Design Pattern
Chapter 10 Design Patterns.
Software Design Patterns
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
MPCS – Advanced java Programming
Low Budget Productions, LLC
Factory Patterns 1.
Behavioral Design Patterns
Software Design and Architecture
Design Patterns with C# (and Food!)
Factory Method Pattern
Presented by Igor Ivković
Software Engineering Lecture 7 - Design Patterns
State Design Pattern 1.
Mechanisms for improving software reuse
BRIDGE PATTERN.
Structural Pattern part-I introduction
Structural Patterns: Adapter and Bridge
Informatics 122 Software Design II
Presented by Igor Ivković
Software Design Lecture : 38.
Adapter Pattern Jim Fawcett
Software Design Lecture 10.
Adapter Pattern Jim Fawcett
Presentation transcript:

ECE 355 Design Patterns Tutorial Part 3 Delete the F.Y.I. slides after you have read them – They are to facilitate discussion. Presented by Igor Ivković iivkovic@swen.uwaterloo.ca

Something Fun  Creational Structural Patterns Behavioural http://home.earthlink.net/~huston2/dp/patterns.html

Agenda Design Patterns Factory Method Adapter Bridge Command State Proxy Summary and References

Factory Method / 1 Intent: This method allows subclasses to decide which class is to be instantiated while defining an interface for creating an object Also Known As: Virtual Constructor Applicability: When the class of objects to be created cannot be estimated by the class creating them The subclasses must specify the objects created by the class Information about the helper subclass to which classes have delegated responsibility has to be localized

Factory Method / 2 Pattern Structure:

Factory Method / 3 Code / 1: public class Test { public static void main( String arg[] ) try { Creator creator = new ConcreteCreator(); creator.anOperation(); } catch( Exception e ) { e.printStackTrace(); } } public interface Product http://vico.org/pages/PatronsDisseny/Pattern%20Factory%20Method/

Factory Method / 4 Code / 2: // Declares the factory method, // which returns an object of type Product. // May also define a default implementation of factory // method that returns a default ConcreteProduct object, // or may call the factory method to create a Product object public abstract class Creator { public void anOperation() { Product product = factoryMethod(); } protected abstract Product factoryMethod(); }

Factory Method / 5 Code / 3: // Overrides the factory method to return // an instance of a ConcreteProduct. public class ConcreteCreator extends Creator { protected Product factoryMethod() return new ConcreteProduct(); }

Agenda Design Patterns Factory Method Adapter Bridge Command State Proxy Summary and References

Adapter / 1 Intent: allow classes with incompatible interfaces to work together, by converting the interface of a given class into one expected by the clients Also Known As: Wrapper Applicability: The interface of an existing class does not match the need The creation of a reusable class that can work with unforeseen or unrelated classes with incompatible interfaces Adapting interfaces of several existing subclasses is not feasible, in which case the interface of the parent class can be adapted by the object adapter

Adapter / 2 Pattern Structure – Class Adapter: Uses multiple inheritance to adapt one interface to another

Adapter / 3 Pattern Structure – Object Adapter: Relies on object composition adaptee

Adapter / 4 Code / 1: public class Test { public static void main( String arg[] ) try { Adapter adapter = new Adapter(); adapter.request(); } catch( Exception e ) { e.printStackTrace(); } } http://www.vico.org/pages/PatronsDisseny/Pattern%20Adapter%20Class/

Adapter / 5 Code / 2: // Defines the domain specific interface that Client uses public interface Target { void request(); } // Adapts the interface of Adaptee to the Target interface public class Adapter extends Adaptee implements Target public void request() { specificRequest(); }

Adapter / 6 Code / 3: // Defines an existing interface that needs adapting public class Adaptee { public void specificRequest() }

Adapter / 7 Example / 1: Consider a plug-in architecture: We have an existing plug-in PluginX for one software (e.g., FirstBrowser) We have similar software (e.g., SecondBrowser) with a different plug-in interface We want to use the PluginX as part of the SecondBrowser without re-writing it Courtesy of: Ali Razavi

<<Interface>> Adapter / 8 Example / 2: FirstBrowser <<Interface>> FirstBrowserPlugin StartPlugin():void PluginX SecondBrowserPlugin InitPlugin():void Start():void SecondBrowser XAdapter InitPlugin(); Start(); +

Agenda Design Patterns Factory Method Adapter Bridge Command State Proxy Summary and References

Bridge / 1 Intent: The decoupling of an abstraction from its implementation in order to vary both independently Also Known As: Handle / Body Applicability: To avoid permanent binding between an abstraction and its implementation Both the abstraction and its implementation should be extensible through subclasses The clients should not be impacted by changes in the abstraction implementation The client is hidden from the fact that multiple objects share the same implementation

Bridge / 2 Pattern Structure:

Bridge / 3 Code / 1: public class Test { public static void main( String arg[] ) try { Abstraction abstraction = new RefinedAbstraction(); Implementor implementor = new ConcreteImplementorB(); abstraction.setImplementor( implementor ); abstraction.operation(); } catch( Exception e ) { e.printStackTrace(); } }

Bridge / 4 Code / 2: // Extends the interface defined by Abstraction public class RefinedAbstraction extends Abstraction { public void operation() { super.operation(); } }

Bridge / 5 Code / 3: // Defines the interface for implementation classes that does not have to exactly correspond to Abstraction's interface. Usually the Implementor interface provides only primitive operations, and Abstraction defines higher-level operations based on these primitives public interface Implementor { void operation(); }

Bridge / 6 Code / 4: // Implements the Implementor interface and defines its conrete implementation public class ConcreteImplementorB implements Implementor { public void operation() { } } // Implements the Implementor interface and defines its concrete implementation public class ConcreteImplementorA implements Implementor

Bridge / 7 Code / 5: // Defines the abstraction's interface, and maintains a reference to an object of type Implementor public class Abstraction { private Implementor implementor; public void setImplementor( Implementor implementor ) { this.implementor = implementor; } public Implementor getImplementor() { return implementor; } public void operation() { implementor.operation(); } } http://www.vico.org/pages/PatronsDisseny/Pattern%20Bridge/

Bridge / 8 Example: The JDBCAdapter class adapts information in a database table to appear in Swing Jtable component.

Bridge / 9 Example Code / 1: Common use: Overall design of an application that uses drivers. Application development is separated from the development of the drivers that implement the application's abstract operations The JDBC architecture decouples an abstraction from its implementation so that the two can vary // To use a JDBC driver, you load it, connect to a database, and create a Statement object Class.forName(driverName); Connection c = DriverManager.getConnection(url, user, passwd); Statement s = c.createStatement();

Bridge / 10 Example Code / 2: // The variable s is a Statement object, capable of issuing SQL queries that return result sets. ResultSet r = s.executeQuery( "select name, apogee from firework"); while(r.next()) { String name = r.getString("name"); int apogee = r.getInt("apogee"); System.out.println(name + ", " + apogee); } http://www.awprofessional.com/articles/article.asp?p=29302&rl=1

Agenda Design Patterns Factory Method Adapter Bridge Command State Proxy Summary and References

Command / 1 Intent: A request is encapsulated as an object, allowing the parameterization of clients with different requests, queuing or logging of requests, and supporting undoable operations Also Known As: Action, Transaction Applicability: Parameterization of objects with an action to perform Specification, queuing, and execution of requests at different times, where a command object can have a lifetime independent of original request Support of logging changes that can be reapplied if the system crashes A system of high-level operations built on primitives operations

Command / 2 Pattern Structure:

Command / 3 Code / 1: public class Test { public static void main( String arg[] ) { try { Client client = new Client(); Command command = client.setup(); command.execute(); } catch( Exception e ) { e.printStackTrace(); } } http://www.vico.org/pages/PatronsDisseny/Pattern%20Command/

Command / 4 Code / 2: // Knows how to perform the operations associated with carrying out a request. Any class may serve as a Receiver public interface Receiver { void action(); } // Implementation of the Receiver interface public class ConcreteReceiver implements Receiver { public void action() { }

Command / 5 Code / 3: // Defines a binding between a Receiver object and an action. Implements Execute by invoking the corresponding operation on Receiver public class ConcreteCommand implements Command { private Receiver receiver; public void setReceiver( Receiver receiver ) { this.receiver = receiver; } public Receiver getReceiver() { return receiver; } public void execute() { receiver.action(); } }

Command / 6 Code / 4: // Declares an interface for executing an operation public interface Command { void setReceiver( Receiver receiver ); Receiver getReceiver(); void execute(); } // Creates a ConcreteCommand object & specifies its receiver public class Client { public Command setup() { Command command = new ConcreteCommand(); Receiver receiver = new ConcreteReceiver(); command.setReceiver( receiver ); // Return the command so that the Invoker may use it return command; } }

Agenda Design Patterns Factory Method Adapter Bridge Command State Proxy Summary and References

State / 1 Intent: When the internal state of an object changes it is allowed to alter its behaviour. The object appears to change its class Also Known As: Objects for States Applicability: An object must undergo change in its behaviour at run-time depending on its state Operations that have large, multipart conditional statements that depend on the object’s state

State / 2 Pattern Structure:

State / 3 Code / 1: public class Test { public static void main( String arg[] ) { try { State state = new ConcreteStateA(); Context context = new Context(); context.setState( state ); context.request(); } catch( Exception e ) { e.printStackTrace(); } } } // Defines an interface for encapsulating the behavior associated with a particular state of the Context public interface State { void handle(); } http://www.vico.org/pages/PatronsDisseny/Pattern%20State/

State / 4 Code / 2: // Defines the interface of interest to clients. Maintains an instance of a ConcreteState subclass that defines the current state public class Context { private State state; public void setState( State state ) { this.state = state; } public State getState() { return state; } public void request() { state.handle(); }}

State / 5 Code / 3: // Each subclass implements a behavior associated with a state of the Context public class ConcreteStateB implements State { public void handle() { } } public class ConcreteStateA implements State { } }

Agenda Design Patterns Factory Method Adapter Bridge Command State Proxy Summary and References

Proxy / 1 Intent: “Provide a surrogate or placeholder for another object to control access to it” Also Known As: Surrogate Applicability: Need for a more versatile or sophisticated reference to an object than a simple pointer Remote, virtual, protection proxies Smart proxy Design Patterns – Gamma et. al.

Proxy / 2 Pattern Structure:

Proxy / 3 Code / 1: public class Test { public static void main( String arg[] ) { try { Subject real = new RealSubject(); Proxy proxy = new Proxy(); proxy.setRealSubject( real ); proxy.request(); } catch( Exception e ) { e.printStackTrace(); } } } // Defines the common interface for RealSubject and Proxy so that a Proxy can be used anywhere a RealSubject is expected public interface Subject { void request(); }

Proxy / 4 Code / 2: // Defines the real object that the proxy represents public class RealSubject implements Subject { public void request() { // Do something based on the interface } } // Maintains a reference that lets the proxy access the real subject. Proxy may refer to a Subject if the RealSubject and Subject interfaces are the same. Provides an interface identical to Subject's so that a proxy can by substituted for the real subject http://www.vico.org/pages/PatronsDisseny/Pattern%20Proxy/

Proxy / 5 Code / 3: public class Proxy implements Subject { private Subject realSubject; public void setRealSubject( Subject subject ) { realSubject = subject; } public Subject getRealSubject() { // This may not be possible if the proxy is communicating over a network return realSubject; } public void request() { // This is very simplified. This could actually be a call to a different run-time environment locally, a machine over a TCP socket, or something completely different. realSubject.request(); } }

Agenda Design Patterns Factory Method Adapter Bridge Command State Proxy Summary and References

Tutorial Summary In this tutorial, we have presented the following design patterns: Factory Method, Adapter, Bridge, Command, State, and Proxy We have demonstrated intent, applicability, and illustrative examples for each pattern We have also discussed pointers for further study

References E. Gamma, R. Helm, R. Johnson, H. Vlissides, Design Patterns, Addison-Wesley, 1994. B. Bruegge and A. H. Dutoit, Object-Oriented Software Engineering, Prentice Hall, 2004.