Design Patterns Part two. Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes.

Slides:



Advertisements
Similar presentations
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 12Slide 1 Software Design l Objectives To explain how a software design may be represented.
Advertisements

UMBC Some Additional Patterns CMSC 432. UMBC More Patterns2 Introduction Over the next few lectures we’ll have an introduction to a number of patterns.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
Reza Gorgan Mohammadi AmirKabir University of Technology, Department of Computer Engineering & Information Technology Advanced design.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
Observer Pattern Tu Nguyen. General Purpose When one object changes state, all the dependent objects are notified and updated. Allows for consistency.
BehavioralCmpE196G1 Behavioral Patterns Chain of Responsibility (requests through a chain of candidates) Command (encapsulates a request) Interpreter (grammar.
Design Patterns CS is not simply about programming
System Architecture Lecture 3 CSE 111 Spring /22/20151Copyright William E. Howden.
Command Design Pattern Source: Design Patterns – Elements of Reusable Object- Oriented Software; Gamma, et. al.
Application Architectures Vijayan Sugumaran Department of DIS Oakland University.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Client/Server Software Architectures Yonglei Tao.
Behavioral Patterns  Behavioral patterns are patterns whose purpose is to facilitate the work of algorithmic calculations and communication between classes.
What is Architecture  Architecture is a subjective thing, a shared understanding of a system’s design by the expert developers on a project  In the.
Software Design Refinement Using Design Patterns Instructor: Dr. Hany H. Ammar Dept. of Computer Science and Electrical Engineering, WVU.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VII Observer, Command, and Memento.
Lexi case study (Part 2) Presentation by Matt Deckard.
Facade Introduction. Intent Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Structural Design Patterns
Patterns in programming1. 2 What are patterns? Answers to common design problems. A language used by developers –To discuss answers to design problems.
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
Where Do Surrogates Fit into This Proxy Pattern Observer Pattern Visitor Pattern By Kurt Rehwinkel.
Proxy, Observer, Symbolic Links Rebecca Chernoff.
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.
Behavioural Design Patterns Quote du jour: ECE450S – Software Engineering II I have not failed. I've just found 10,000 ways that won't work. - Thomas Edison.
 Registry itself is easy and straightforward in implementation  The objects of registry are actually complicated to store and manage  Objects of Registry.
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
FacadeDesign Pattern Provide a unified interface to a set of interfaces in a subsystem. Defines a high level interface that makes the subsystem easier.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Manali Joshi1 The Observer Design Pattern Presented By: Manali Joshi.
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Behavioral Patterns1 Nour El Kadri SEG 3202 Software Design and Architecture Notes based on U of T Design Patterns class.
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
1 CSE 331 Model/View Separation and Observer Pattern slides created by Marty Stepp based on materials by M. Ernst, S. Reges, D. Notkin, R. Mercer, Wikipedia.
The Mediator Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
1 Advanced Object-oriented Design – Principles and Patterns Structural Design Patterns.
Component Patterns – Architecture and Applications with EJB copyright © 2001, MATHEMA AG Component Patterns Architecture and Applications with EJB Markus.
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.
Watching the movie the hard way…. Page 256 – Head First Design Patterns.
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.
The Observer Design Pattern Author :Erich Gamma, et al. Source :Elements of Reusable Object-Oriented Software Speaker : Chiao-Ping Chang Advisor : Ku-Yaw.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Command Pattern. Intent encapsulate a request as an object  can parameterize clients with different requests, queue or log requests, support undoable.
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
Design Patterns: MORE Examples
Software Design Refinement Using Design Patterns
Structural Patterns Structural patterns control the relationships between large portions of your applications. Structural patterns affect applications.
MPCS – Advanced java Programming
Behavioral Design Patterns
Observer Design Pattern
Instructor: Dr. Hany H. Ammar
Facade Pattern Jim Fawcett CSE776 – Design Patterns Summer 2010
object oriented Principles of software design
Decorator Design Pattern
Presented by Igor Ivković
Design Patterns Satya Puvvada Satya Puvvada.
Informatics 122 Software Design II
Object Oriented Design Patterns - Structural Patterns
Informatics 122 Software Design II
Presented by Igor Ivković
Presentation transcript:

Design Patterns Part two

Structural Patterns Concerned with how classes and objects are composed to form larger structures Concerned with how classes and objects are composed to form larger structures Structural class patterns – use inheritance to compose interfaces or implementations. Structural class patterns – use inheritance to compose interfaces or implementations. Structural object patterns – describe ways to compose objects creating new functionality. Structural object patterns – describe ways to compose objects creating new functionality.

The Façade Pattern Provides a single interface into a subsystem. Provides a single interface into a subsystem. Used to separate or simplify the subsystem in the program. Used to separate or simplify the subsystem in the program. Used in UIs to separate a GUI from the underlying ‘business logic’ Used in UIs to separate a GUI from the underlying ‘business logic’

Façade – Use When: A simple interface into a complex subsystem is required. A simple interface into a complex subsystem is required. A subsystem’s details are not or should not be required knowledge for other components. A subsystem’s details are not or should not be required knowledge for other components. Subsystems should be compartmentalized, decoupling the system from the ‘black box’. This allows for easier changes within the isolated subsystem, as long as the API for the system isn’t altered Subsystems should be compartmentalized, decoupling the system from the ‘black box’. This allows for easier changes within the isolated subsystem, as long as the API for the system isn’t altered Layering of subsystems is required Layering of subsystems is required

Façade - Benefits Shields clients from subsystem components. Shields clients from subsystem components. Promotes a weak coupling between subsystems, allows subsystem change without affecting clients. Promotes a weak coupling between subsystems, allows subsystem change without affecting clients. This pattern helps simplify complex or circular subsystems for outside users. This pattern helps simplify complex or circular subsystems for outside users. Possible compile time reduction, as not all components may need to be recompiled. Possible compile time reduction, as not all components may need to be recompiled.

Façade – Example You want to make a platform-independent front end to a database You want to make a platform-independent front end to a database  Database runs on any target system  Front end is (almost always) platform dependent Create a façade API to program the GUI to Create a façade API to program the GUI to  Moving to a new platform means reimplementing the GUI, but not the API or the back end code

Implementation Points Subsystem coupling can be reduced if the façade is an abstract class, and concrete classes implement the subsystem itself. This prevents clients from knowing details about which subsystem is used. Subsystem coupling can be reduced if the façade is an abstract class, and concrete classes implement the subsystem itself. This prevents clients from knowing details about which subsystem is used.  Also, consider the use of interfaces in Java Judicious use of public and private classes and methods can help provide subsystem independence and encapsulation. (Consider the use of package security in Java) Judicious use of public and private classes and methods can help provide subsystem independence and encapsulation. (Consider the use of package security in Java)

The Proxy Pattern The Proxy provides a surrogate or placeholder, often to control access to it. The Proxy provides a surrogate or placeholder, often to control access to it. There are four general categories of proxy. There are four general categories of proxy. Used when there needs to be a more versatile reference to an object, or when it is not practical or desired to reference the original. Used when there needs to be a more versatile reference to an object, or when it is not practical or desired to reference the original.  Example: large image that eats a lot of memory

Proxy Categories Remote Proxy: Local representative for objects in different spaces. Remote Proxy: Local representative for objects in different spaces. Virtual Proxy: Creates expensive objects on demand. Virtual Proxy: Creates expensive objects on demand. Protection Proxy: Controls access, can be similar to a Façade. Protection Proxy: Controls access, can be similar to a Façade. Smart Reference: Various value adds, such as object loading, tracking instances, object or database locking. Smart Reference: Various value adds, such as object loading, tracking instances, object or database locking.

Proxy Benefits Remote proxy can hide resident details. Remote proxy can hide resident details. Virtual proxy creates expensive objects on demand, rather than all at startup. Virtual proxy creates expensive objects on demand, rather than all at startup.  Example: dialog boxes Protection and Smart Proxies can allow housekeeping an other tasks to be encapsulated. Protection and Smart Proxies can allow housekeeping an other tasks to be encapsulated.

Proxy – Example Your program must be ready to display a large number of graphical images, but not all of them can be seen by the user at one time. A virtual proxy could be used to load the image only when needed by the user, so that the time to open the application is not slowed by image loading. Your program must be ready to display a large number of graphical images, but not all of them can be seen by the user at one time. A virtual proxy could be used to load the image only when needed by the user, so that the time to open the application is not slowed by image loading.

Behavioral Patterns Concerned with algorithms and the assignment of object responsibilities. Concerned with algorithms and the assignment of object responsibilities. Describe not just the patterns of objects but how they communicate. Describe not just the patterns of objects but how they communicate. Behavioral class patterns use inheritance to distribute behavior to classes. Behavioral class patterns use inheritance to distribute behavior to classes. Behavioral object patterns use object composition rather than inheritance. Often, this describe how a group of objects cooperate to perform a task and what objects need to know about which others. Behavioral object patterns use object composition rather than inheritance. Often, this describe how a group of objects cooperate to perform a task and what objects need to know about which others.

The Command Pattern Encapsulates a request as an object.

Command – Use When: Useful to issue requests without knowing details about the operation or recipient. Useful to issue requests without knowing details about the operation or recipient. Objects can be parameterized by an action to perform. Objects can be parameterized by an action to perform. A framework executes a generic command, and the command object is extended to do the application specific method. A framework executes a generic command, and the command object is extended to do the application specific method.

Command Benefits Command allows the calling object to be separate from the one that can perform it. Command allows the calling object to be separate from the one that can perform it. Commands can be assembled together to form more complex commands. An example of this is the MacroCommand, which will not be discussed here. Commands can be assembled together to form more complex commands. An example of this is the MacroCommand, which will not be discussed here. Adding new commands is easy, and existing classes won’t have to change. Adding new commands is easy, and existing classes won’t have to change.

Command - Benefits Support can be added for operations undo and redo. Support can be added for operations undo and redo. Security can be enhanced by logging user actions through centralized Command objects. Security can be enhanced by logging user actions through centralized Command objects.

Command – Example The application you are working on must support a log of all user activity for security purposes. To do this, you use a central command object which will be able to determine what actions a user has taken and can then log them or do whatever actions are required. The application you are working on must support a log of all user activity for security purposes. To do this, you use a central command object which will be able to determine what actions a user has taken and can then log them or do whatever actions are required.

Implementation Points Consider command object intelligence. It can be a simple object between a requestor and a recipient. Consider command object intelligence. It can be a simple object between a requestor and a recipient. For undo/redo, consider what parameters and information must be stored. For undo/redo, consider what parameters and information must be stored. If using C++, templates may be used if commands aren’t undoable and don’t require arguments. If using C++, templates may be used if commands aren’t undoable and don’t require arguments.  Try to limit the number of undoable commands; it bug the user….

The Observer Pattern Defines a one-to-many dependency or communication flow between objects. When one object is updated, all dependents are notified automatically. Defines a one-to-many dependency or communication flow between objects. When one object is updated, all dependents are notified automatically. This is one of the most important patterns in GUI development, as it abstracts the notion of the “callback” This is one of the most important patterns in GUI development, as it abstracts the notion of the “callback”  Some event occurs (user clicks a button) and something else is notified (function is called)

Observer – Use When: An abstraction has two aspects, one dependent on the other. Encapsulating these pieces in separate objects lets you vary and reuse them independently. An abstraction has two aspects, one dependent on the other. Encapsulating these pieces in separate objects lets you vary and reuse them independently. A change to one object requires changing others; the number to be changed may be dynamic or unknown. A change to one object requires changing others; the number to be changed may be dynamic or unknown. An object should be able to notify others without making assumptions about what the objects are. An object should be able to notify others without making assumptions about what the objects are.

Observer - Benefits Abstracts the coupling between the notifying object and observers. All the notifying object knows is it has a group of observing objects to send a message to. Abstracts the coupling between the notifying object and observers. All the notifying object knows is it has a group of observing objects to send a message to. Good object oriented programming, where a message is sent, but each object’s action is independent. Good object oriented programming, where a message is sent, but each object’s action is independent. Support for broadcast communication. Objects can register or deregister as needed, and the notifying object only has to know which objects currently need notification. Support for broadcast communication. Objects can register or deregister as needed, and the notifying object only has to know which objects currently need notification.

Observer - Drawbacks Care must be taken to ensure that messages sent to observers don’t themselves trigger more messages to be sent, causing an operation to become an infinite loop. Care must be taken to ensure that messages sent to observers don’t themselves trigger more messages to be sent, causing an operation to become an infinite loop.

Observer – Example 1 A user presses the File->Load menu item. An application object called FileLoader is notified (or observes the press) and goes about loading a file. A user presses the File->Load menu item. An application object called FileLoader is notified (or observes the press) and goes about loading a file.

Observer – Example 2 A company that makes medical monitors needs a way to display data to nurses and physicians so they can ascertain a patient’s condition quickly and completely. By using an observer, when new data is added to the database, notifications can be sent to multiple observers, say the monitor at the patient’s bedside, the nurses’ station and the physician who happens to be monitoring the patient live in their office at the same time. A company that makes medical monitors needs a way to display data to nurses and physicians so they can ascertain a patient’s condition quickly and completely. By using an observer, when new data is added to the database, notifications can be sent to multiple observers, say the monitor at the patient’s bedside, the nurses’ station and the physician who happens to be monitoring the patient live in their office at the same time.

Implementation Points How to map notifying objects to observers. Often down with a simple “connection” function call How to map notifying objects to observers. Often down with a simple “connection” function call Observers may have to know what object is sending the notification, especially if it registers as a listener to more than one notifying object. Observers may have to know what object is sending the notification, especially if it registers as a listener to more than one notifying object. Similarly, it may also be more efficient to send data each observer rather than sending an update message and having each observer retrieve it. Similarly, it may also be more efficient to send data each observer rather than sending an update message and having each observer retrieve it.

Deleting an observer should not result in a dangling pointer in the notifying object. A way to deal with this could be to make each item an observer of the other so references can be removed when it is deleted. Deleting an observer should not result in a dangling pointer in the notifying object. A way to deal with this could be to make each item an observer of the other so references can be removed when it is deleted. A consideration of who triggers the update is very important. Objects can be responsible for sending the notification, but if the code is forgotten at the client, or a bug prevents it from being sent, then the update is never broadcast. A consideration of who triggers the update is very important. Objects can be responsible for sending the notification, but if the code is forgotten at the client, or a bug prevents it from being sent, then the update is never broadcast.

Push and Pull Models of Implementation. One extreme is the push, which pushes data to each observer, without knowing if it needs it or not. This may broadcast more information to observers than they need to know, and make code reuse more difficult. One extreme is the push, which pushes data to each observer, without knowing if it needs it or not. This may broadcast more information to observers than they need to know, and make code reuse more difficult. Most GUI toolkits do this Most GUI toolkits do this The pull model sends a relatively simple message, which may require each observer to then request potentially identical information. This may hide communication and implementation better, but may result in a more inefficient model. The pull model sends a relatively simple message, which may require each observer to then request potentially identical information. This may hide communication and implementation better, but may result in a more inefficient model.

References “Design Patterns”, Gamma, Helm, Johnson and Vlissides, Addison-Wesley, 1995 “Design Patterns”, Gamma, Helm, Johnson and Vlissides, Addison-Wesley, 1995