Goals of Lecture. Lecture 27: OO Design Patterns. Pattern Resources. Design Patterns. Cover OO Design Patterns. Pattern Languages of Programming

Similar documents
Pattern Resources. Lecture 25: Design Patterns. What are Patterns? Design Patterns. Pattern Languages of Programming. The Portland Pattern Repository

Lecture 13: Design Patterns

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

More on Design. CSCI 5828: Foundations of Software Engineering Lecture 23 Kenneth M. Anderson

Second Midterm Review

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

Object-Oriented Design

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

Design Patterns. Gunnar Gotshalks A4-1

Design Patterns. An introduction

Design Patterns. Manuel Mastrofini. Systems Engineering and Web Services. University of Rome Tor Vergata June 2011

Software Design Patterns. Background 1. Background 2. Jonathan I. Maletic, Ph.D.

Facade and Adapter. Comp-303 : Programming Techniques Lecture 19. Alexandre Denault Computer Science McGill University Winter 2004

Using Design Patterns in Java Application Development

Factory Method Pattern Creational. » Define an interface for creating an object but lets subclasses decide the specific class to instantiate

Topics in Object-Oriented Design Patterns

SOFTWARE PATTERNS. Joseph Bonello

An Introduction to Patterns

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

Design Patterns. Definition of a Design Pattern

Modellistica Medica. Maria Grazia Pia, INFN Genova. Scuola di Specializzazione in Fisica Sanitaria Genova Anno Accademico

Dr. Xiaolin Hu. Review of last class

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

4.1 Introduction Programming preliminaries Constructors Destructors An example... 3

Reuse at Design Level: Design Patterns

Crash course on design patterns

Design of Software Systems (Ontwerp van SoftwareSystemen) Design Patterns Reference. Roel Wuyts

Command. Comp-303 : Programming Techniques Lecture 22. Alexandre Denault Computer Science McGill University Winter 2004

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Design for change. You should avoid

Design Patterns. CSC207 Fall 2017

Brief Note on Design Pattern

What is Design Patterns?

ADAPTER. Topics. Presented By: Mallampati Bhava Chaitanya

DESIGN PATTERN MATCHING

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

2.1 Design Patterns and Architecture (continued)

SDC Design patterns GoF

Design Patterns. CSC207 Winter 2017

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

2.1 Design Patterns and Architecture (continued)

Laboratorio di Progettazione di Sistemi Software Design Pattern Creazionali. Valentina Presutti (A-L) Riccardo Solmi (M-Z)

Tuesday, October 4. Announcements

Object Oriented Methods with UML. Introduction to Design Patterns- Lecture 8

Design Patterns. CSC207 Fall 2017

Design Patterns Reid Holmes

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

The GoF Design Patterns Reference

Creational Patterns. Factory Method (FM) Abstract Factory (AF) Singleton (SI) Prototype (PR) Builder (BU)

Inheritance. EEC 521: Software Engineering. Dealing with Change. Polymorphism. Software Design. Changing requirements Code needs to be flexible

CSCD01 Engineering Large Software Systems. Design Patterns. Joe Bettridge. Winter With thanks to Anya Tafliovich

An Introduction to Patterns

Design Pattern What is a Design Pattern? Design Pattern Elements. Almas Ansari Page 1

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

3 Product Management Anti-Patterns by Thomas Schranz

Design Patterns IV Structural Design Patterns, 1

CPSC 310 Software Engineering. Lecture 11. Design Patterns

11/12/12. Objectives DESIGN PATTERNS. Design Pattern. Defined Design Patterns. Applying Design Patterns. Motivating Example

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

OODP Session 5a. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

Design patterns. OOD Lecture 6

Object-Oriented Oriented Programming Adapter Pattern. CSIE Department, NTUT Woei-Kae Chen

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

Programmazione. Prof. Marco Bertini

Design Patterns IV. Alexei Khorev. 1 Structural Patterns. Structural Patterns. 2 Adapter Design Patterns IV. Alexei Khorev. Structural Patterns

Design Patterns. Comp2110 Software Design. Department of Computer Science Australian National University. Second Semester

be used for more than one use case (for instance, for use cases Create User and Delete User, one can have one UserController, instead of two separate

Design Pattern. CMPSC 487 Lecture 10 Topics: Design Patterns: Elements of Reusable Object-Oriented Software (Gamma, et al.)

Prototype Description. Interpreter. interpreter Calculator Design Rationales. Prototype Participants. Interpreter with Factory Method.

Object-Oriented Oriented Programming Factory Method Pattern Abstract Factory Pattern. CSIE Department, NTUT Woei-Kae Chen

INTERNAL ASSESSMENT TEST III Answer Schema

Object-Oriented Design

Coordination Patterns

DESIGN PATTERN - INTERVIEW QUESTIONS

Chapter 12 (revised by JAS)

Design Patterns. GoF design patterns catalog

Adapter Pattern Structural

What is Design Patterns?

State Pattern. CSIE Department, NTUT Woei-Kae Chen. State: Intent. Allow an object to alter its behavior when its internal state changes.

Produced by. Design Patterns. MSc in Communications Software. Eamonn de Leastar

Design Pattern Detection

A Reconnaissance on Design Patterns

Object-Oriented Oriented Programming Adapter Pattern

Design Pattern- Creational pattern 2015

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Design Patterns Reid Holmes

Trusted Components. Reuse, Contracts and Patterns. Prof. Dr. Bertrand Meyer Dr. Karine Arnout

Ingegneria del Software Corso di Laurea in Informatica per il Management. Design Patterns part 1

Information Systems Analysis and Design. XIV. Design Patterns. Design Patterns 1

Object-Oriented Oriented Programming Command Pattern. CSIE Department, NTUT Woei-Kae Chen

Factory Method. Comp435 Object-Oriented Design. Factory Method. Factory Method. Factory Method. Factory Method. Computer Science PSU HBG.

APPLYING DESIGN PATTERNS TO SCA IMPLEMENTATIONS

Socket attaches to a Ratchet. 2) Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Design Patterns. it s about the Observer pattern, the Command pattern, MVC, and some GUI. some more

Design Pattern Detection

The Factory Method Pattern

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Transcription:

Goals of Lecture Lecture 27: OO Design Patterns Cover OO Design Patterns Background Examples Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 6448 - Spring Semester, 2001 April 24, 2001 Kenneth M. Anderson, 2001 2 Pattern Resources Pattern Languages of Programming Technical conference on Patterns The Portland Pattern Repository http://c2.com/ppr/ Patterns Homepage http://hillside.net/patterns/patterns.html Design Patterns Addison-Wesley book published in 1995 Erich Gamma Richard Helm Ralph Johnson John Vlissides Known as The Gang of Four Presents 23 Design Patterns ISBN 0-201-63361-2 April 24, 2001 Kenneth M. Anderson, 2001 3 April 24, 2001 Kenneth M. Anderson, 2001 4

What are Patterns? Patterns, continued Christopher Alexander talking about buildings and towns 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 Alexander, et al., A Pattern Language. Oxford University Press, 1977 Patterns can have different levels of abstraction In Design Patterns (the book), Patterns are not classes Patterns are not frameworks Instead, Patterns are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context April 24, 2001 Kenneth M. Anderson, 2001 5 April 24, 2001 Kenneth M. Anderson, 2001 6 Patterns, continued So, patterns are formalized solutions to design problems They describe techniques for maximizing flexibility, extensibility, abstraction, etc. These solutions can typically be translated to code in a straightforward manner Pattern Name Elements of a Pattern More than just a handle for referring to the pattern Each name adds to a designer s vocabulary Enables the discussion of design at a higher abstraction The Problem Gives a detailed description of the problem addressed by the pattern Describes when to apply a pattern Often with a list of preconditions April 24, 2001 Kenneth M. Anderson, 2001 7 April 24, 2001 Kenneth M. Anderson, 2001 8

Elements of a Pattern, continued The Solution Describes the elements that make up the design, their relationships, responsibilities, and collaborations Does not describe a concrete solution Instead a template to be applied in many situations Elements of a Pattern, continued The consequences Describes the results and tradeoffs of applying the pattern Critical for evaluating design alternatives Typically include Impact on flexibility, extensibility, or portability Space and Time tradeoffs Language and Implementation issues April 24, 2001 Kenneth M. Anderson, 2001 9 April 24, 2001 Kenneth M. Anderson, 2001 10 Design Pattern Template Examples Pattern Name and Classification Creational Structural Behavioral Also Known As Structure Implementation Sample Code Known Uses Related Patterns Singleton Factory Method Adapter Decorator Command State April 24, 2001 Kenneth M. Anderson, 2001 11 April 24, 2001 Kenneth M. Anderson, 2001 12

Singleton Ensure a class has only one instance, and provide a global point of access to it Some classes represent objects where multiple instances do not make sense or can lead to a security risk (e.g. Java security managers) Singleton, continued Use the Singleton pattern when there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point when the sole instance should be extensible by subclassing, and clients should be able to use an extended instance without modifying their code April 24, 2001 Kenneth M. Anderson, 2001 13 April 24, 2001 Kenneth M. Anderson, 2001 14 Singleton, continued Singleton Structure Just the Singleton class Clients access a Singleton instance solely through Singleton s Instance operation Controlled access to sole instance Reduced name space (versus global variables) Permits a variable number of instances (if desired) Singleton static Instance() public SingletonOperation() public GetSingletonData() private static uniqueinstance private singletondata {return uniqueinstance} April 24, 2001 Kenneth M. Anderson, 2001 15 April 24, 2001 Kenneth M. Anderson, 2001 16

Factory Method Define an interface for creating an object, but let subclasses decide which class to instantiate Also Known As Virtual Constructor Frameworks define abstract classes, but any particular domain needs to use specific subclasses; how can the framework create these subclasses? Factory Method, continued Use the Factory Method pattern when a class can t anticipate the class of objects it must create a class wants its subclasses to specify the objects it creates classes delegate responsibility to one of several helper subclasses, and you want to localize the knowledge of which helper subclass is the delegate April 24, 2001 Kenneth M. Anderson, 2001 17 April 24, 2001 Kenneth M. Anderson, 2001 18 Factory Method, continued Factory Method Structure Product Defines the interface of objects the factory method creates Concrete Product Implements the Product Interface Creator declares the Factory method which returns an object of type Product Concrete Creator overrides the factory method to return an instance of a Concrete Product Product ConcreteProduct «instantiate» product = FactoryMethod() Creator FactoryMethod() AnOperation() ConcreteCreator FactoryMethod() return new ConcreteProduct() April 24, 2001 Kenneth M. Anderson, 2001 19 April 24, 2001 Kenneth M. Anderson, 2001 20

Factory Method Consequences Adapter Factory methods eliminate the need to bind application-specific classes into your code Potential disadvantage is that clients must use subclassing in order to create a particular ConcreteProduct In single-inherited systems, this constrains your partitioning choices Provides hooks for subclasses Connects parallel class hierarchies Convert the interface of a class into another interface clients expect. Adapter lets classes work together that could not otherwise because of incompatible interfaces Also Known As Wrapper Sometimes a toolkit class that is designed for reuse is not reusable because its interface does not match the domain-specific interface an application requires Page 139-140 of Design Patterns provides an example April 24, 2001 Kenneth M. Anderson, 2001 21 April 24, 2001 Kenneth M. Anderson, 2001 22 Adapter, continued Use the Adapter pattern when you want to use an existing class, and its interface does not match the one you need you want to create a reusable class that cooperates with unrelated or unforeseen classes Adapter, continued Target defines the domain-specific interface that Client uses Client collaborates with objects conforming to the Target interface Adaptee defines an existing interface that needs adapting Adapter adapts the interface of Adaptee to the Target interface April 24, 2001 Kenneth M. Anderson, 2001 23 April 24, 2001 Kenneth M. Anderson, 2001 24

Class Adapter Client Adapter Structure Target Request() Adaptee SpecificRequest() Object Adapter Client Adapter Structure Target Request() Adapter Request() SpecificRequest() Adapter Request() Adaptee SpecificRequest() adaptee adaptee.specificrequest() April 24, 2001 Kenneth M. Anderson, 2001 25 April 24, 2001 Kenneth M. Anderson, 2001 26 Adapter, continued Clients call operations on an Adapter instance. In turn, the adapter calls Adaptee operations that carry out the request Class Adapters adapts Adaptee to Target by committing to concrete Adapter class; Adapter can override Adaptee behavior Object Adapters lets a single Adapter work with many Adaptees; makes it harder to override Adaptee behavior Decorator Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality Also Known As Wrapper Sometimes we want to add responsibilities to individual objects, not to an entire class (like adding scrollbars to windows in GUI toolkits) April 24, 2001 Kenneth M. Anderson, 2001 27 April 24, 2001 Kenneth M. Anderson, 2001 28

Decorator, continued Decorator, continued Use Decorator to add responsibilities to individual objects dynamically for responsibilities that can be withdrawn when extension by subclassing is impractical Component defines interface of objects to decorate ConcreteComponent defines an object to decorate Decorator and ConcreteDecorator Decorator maintains a reference to component and defines an interface that conforms to Component s interface; ConcreteDecorator adds responsibilities to the component Structure Page 177 of Design Patterns Decorator forwards requests to its Component object. It may optionally perform additional operations before and after forwarding the request More flexibility than static inheritance Avoids feature-laden classes high up in the hierarchy A decorator and its component are not identical Lots of little objects April 24, 2001 Kenneth M. Anderson, 2001 29 April 24, 2001 Kenneth M. Anderson, 2001 30 Command Command, continued Encapsulate a request as an object, thereby letting you parameterize clients with different requests, queue or log requests, and support undoable operations Also Known As Action, Transaction Separate details of a request from the requestor and the requestor from the receiver of the request Example: Menus Use the Command pattern to parameterize objects by an action to perform specify, queue, and execute requests support undo and logging structure a system around high-level operations built on primitive command April 24, 2001 Kenneth M. Anderson, 2001 31 April 24, 2001 Kenneth M. Anderson, 2001 32

Command, continued Command, continued Command declares an interface for executing an operation ConcreteCommand defines a binding between a Receiver object and an action implements Command interface Client creates a Concrete Command object and sets its receiver Invoker asks the command to carry out the request Receiver knows how to perform the operations of the command April 24, 2001 Kenneth M. Anderson, 2001 33 Structure Page 236 of Design Patterns The client creates a ConcreteCommand object and specifies its receiver An Invoker object stores the ConcreteCommand The invoker issues a request by calling Execute on Command The ConcreteCommand invokes operations on the Receiver Page 237 of Design Patterns April 24, 2001 Kenneth M. Anderson, 2001 34 Command, continued Command decouples the object that invokes an operation from the one that implements it Commands are first-class objects Commands can be assembled into composite commands It is easy to add new commands State Allow an object to alter its behavior when its internal state changes TCPConnection example A TCPConnection class must respond to an open operation differently based on its current state: established, closed, listening, etc. April 24, 2001 Kenneth M. Anderson, 2001 35 April 24, 2001 Kenneth M. Anderson, 2001 36

State, continued State, continued Use State when an object s behavior depends on its state operations have large, multipart conditional statements that depend on the object s state Context defines the interface of interest to clients maintains an instance of a ConcreteState subclass State defines an interface for encapsulating the behavior associated with a particular state of the Context ConcreteState each subclass of State implements a different behavior that implements the correct behavior for a particular state April 24, 2001 Kenneth M. Anderson, 2001 37 Structure Page 306 of Design Patterns Context delegates state-specific requests to the current ConcreteState object A context may pass itself as an argument to the State object handling the request Context is the primary interface of clients Either Context or ConcreteState subclasses can decide which state succeeds another and under what circumstances April 24, 2001 Kenneth M. Anderson, 2001 38 State, continued State localizes state-specific behavior and partitions behavior for different states State makes state transitions explicit State objects can be shared April 24, 2001 Kenneth M. Anderson, 2001 39