Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

Similar documents
Design Patterns. Bernd Bruegge. Carnegie Mellon University Department of Computer Science 15 October 1998

CSC 301H, Introduction to Software Engineering

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

Introduction and History

Page 1. Chapter 8, Object Design: Design Patterns II. Recall: Why reusable Designs? Definitions. A Taxonomy of Design Patterns

What is Design Patterns?

8. Object Design: Reusing Pattern Solutions

What is Design Patterns?

The GoF Design Patterns Reference

2.1 Design Patterns and Architecture (continued)

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

2.1 Design Patterns and Architecture (continued)

Design patterns. OOD Lecture 6

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

SDC Design patterns GoF

EMBEDDED SYSTEMS PROGRAMMING Design Patterns

Design Patterns. Softwaretechnik. Matthias Keil. Albert-Ludwigs-Universität Freiburg

Creational Design Patterns

Lecture 21: Design Patterns III

Object-Oriented Design

Review of last class. 1.Class inheritance versus interface inheritance 2.Inheritance versus composition. Dr. Xiaolin Hu

What is Design Patterns?

6.3 Patterns. Definition: Design Patterns

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

Design Patterns. Software Engineering. Sergio Feo-Arenis slides by: Matthias Keil

Lecture 20: Design Patterns II

Observer Pattern. CS580 Advanced Software Engineering October 31, Yu Sun, Ph.D.

The Strategy Pattern Design Principle: Design Principle: Design Principle:

An Introduction to Patterns

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

Softwaretechnik. Design Patterns. Matthias Keil. Albert-Ludwigs-Universität Freiburg

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

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

Softwaretechnik. Design Patterns. Stephan Arlt SS University of Freiburg. Stephan Arlt (University of Freiburg) Softwaretechnik SS / 47

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

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

EPL 603 TOPICS IN SOFTWARE ENGINEERING. Lab 6: Design Patterns

Tuesday, October 4. Announcements

DESIGN PATTERN - INTERVIEW QUESTIONS

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

CS 2720 Practical Software Development University of Lethbridge. Design Patterns

Second Midterm Review

SOFTWARE PATTERNS. Joseph Bonello

Information systems modelling UML and service description languages

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

Using Design Patterns in Java Application Development

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

Topics in Object-Oriented Design Patterns

Object-Oriented Oriented Programming

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

Introduction to Software Engineering: Object Design I Reuse & Patterns

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

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

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

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

Object-Oriented Design

Summary of the course lectures

Dr. Xiaolin Hu. Review of last class

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Brief Note on Design Pattern

Design Patterns. An introduction

CS 349 / SE 382 Design Patterns. Professor Michael Terry January 21, 2009

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

Object-oriented Software Design Patterns

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

Object Design II: Design Patterns

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

Design Patterns. Gunnar Gotshalks A4-1

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

Egon Borger (Pisa) Capturing Design Pattern Abstractions by ASMs

Design Patterns! Acknowledgements!

An Introduction to Patterns

Chapter 8, Object Design: Reusing Pattern Solutions

Software Engineering Prof. Rushikesh K.Joshi IIT Bombay Lecture-15 Design Patterns

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

Design Patterns. CSC207 Fall 2017

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

Design Patterns. Definition of a Design Pattern

Design Patterns. Observer Pattern*

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

Design Patterns. CSC207 Winter 2017

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

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

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 Patterns. CSC207 Fall 2017

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

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

CS342: Software Design. November 21, 2017

Design Pattern and Software Architecture: IV. Design Pattern

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

CHAPTER 6: CREATIONAL DESIGN PATTERNS


Object Oriented Paradigm

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

CPSC 427a: Object-Oriented Programming

Design Patterns. Design Patterns

Lecture 17: Patterns Potpourri. Copyright W. Howden 1

Model-based Software Engineering (02341, spring 2016) Ekkart Kindler

Design Patterns. (and anti-patterns)

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

Transcription:

Proxy : Motivation Design s 2 It is 3pm. I am sitting at my 10Mbps connection and go to browse a fancy web page from the US, This is prime web time all over the US. So I am getting 100kbps What can you do? Software Requirements and Design CITS 4401 Lecture 10 (From Bruegge and Dutoit Chap 8 and Appendix A) Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 1 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 2 Proxy Proxy pattern Subject What is expensive? Object Creation Object Initialization Defer object creation and object initialization to the time that you need the object Proxy pattern: Uses another object ( the proxy ) that acts as a stand-in for the real object Reduces the cost of accessing objects The proxy creates the real object only if the user asks for it Proxy Request() realsubject Request() 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. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 3 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 4 Page 1

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 stand-in 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. Virtual Proxy example ProxyImage boundingbox() draw() realsubject Image boundingbox() draw() Images are stored and loaded separately from text RealImage boundingbox() draw() 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 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 5 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 6 Before After Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 7 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 8 Page 2

Towards a Taxonomy A Taxonomy Structural s Adapters, Bridges, Façades, 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 Encapsulate complex structures Behavioural s Concerned with algorithms and the assignment of responsibilities between objects: Who does what? Characterize complex control flow that is difficult to follow at runtime. Creational s Abstract the instantiation process. Structural Behavioral Command Observer Strategy Abstract Factory Creational Builder Make a system independent from the way its objects are created, composed and represented. Adapter Bridge Facade Proxy Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 9 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 10 Observer pattern Observer pattern (continued) Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically. (p. 293) Also called Publish and Subscribe Observers Subject The Observer pattern: Maintains consistency across redundant state Optimizes batch changes to maintain consistency 9Designs2.ppt Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 11 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 12 Page 3

Observer pattern (continued) Animated Sequence diagram Subject attach(observer) detach(observer) notify() observers * Observer afile Attach() aninfoview Attach() alistview ConcreteSubject getstate() setstate(newstate) subjectstate subject ConcreteObserver observerstate setstate( foo ) notify() The Subject represents the actual state, the Observers represent different views of the state. Subject is a super class (needs to store the observers vector) not an interface. getstate() foo Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 13 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 14 Observer pattern implementation in Java Abstract Factory Motivation // 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 interface Observer { public abstract void update(observable o, Object arg); public class Subject extends Observable{ public void setstate(string filename); public string getstate(); Consider a user interface toolkit that supports multiple looks and feel standards such as Motif, Windows 10 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 a smart house that supports different control systems such as Siemens Instabus, Johnson & Control Metasys or Zumtobel s proprietary standard. How can you write a single control system that is independent from the manufacturer? Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 15 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 16 Page 4

Abstract Factory Client ConcreteFactory1 CreateProductA CreateProductB AbstractFactory CreateProductA CreateProductB ConcreteFactory2 CreateProductA CreateProductB AbstractProductA ProductA2 ProductA1 AbstractProductB ProductB1 ProductB2 Applicability for Abstract Factory Independence from Initialization or Representation: The system should be independent of how its products are created, composed or represented Manufacturer Independence: A system should be configured with one of multiple family of products 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 Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 17 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 18 OWL System for the The Intelligent Workplace at Carnegie Mellon University Facility Mgt System SiemensFactory InitLightSystem InitBlindSystem InitACSystem IntelligentWorkplace InitLightSystem InitBlindSystem InitACSystem InstabusLight InstabusBlind LightBulb Blinds ZumtobelLight ZumtobelBlind Singleton (127) The Singleton is used to ensure a class has only one instance and provide a global access point to it. [Gamma et al 95] If we write a class to manage a file system, we do not want someone to make multiple instances of the class, as changes in the file system may not be properly recorded. In Java a Singleton uses static operations and access modifiers to prevent clients from making multiple instances of the class ZumtobelFactory InitLightSystem InitBlindsystem InitACSystem Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 19 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 20 Page 5

The Singleton in Java public class Singleton{ // declare the unique instance of the class private static Singleton unique = new Singleton(); //private constructor can only be access from this class private Singleton (){//Constructor code here //public method to return a reference to the unique //instance of the class public static Singleton getinstance(){ return unique; //other methods here Summary Structural s Focus: How objects are composed to form larger structures Problems solved: To realize new functionality from old functionality, To provide flexibility and extensibility Behavioral s Focus: Algorithms and the assignment of responsibilities to objects Problem solved: Overly tight coupling to a particular algorithm Creational s Focus: Creation of complex objects Problem solved: Hide how complex objects are created and put together Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 21 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 22 Other Design s Conclusion Builder (97) - Separate the construction of a complex object from its representation so the same process can create different representations. Flyweight (195) - Use sharing to support large numbers of finegrained objects efficiently. Command (223) - Encapsulate requests as objects, allowing you to treat them uniformly. Iterator (257) - Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. State (305) - Allow an object to alter its behavior when its internal state changes. The object will appear to change its class. 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??? Reading and studying design patterns will give you a library of solutions, and an awareness of consequences in object oriented software design. Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 23 Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 24 Page 6

Further Reading Bruegge & Dutoit, 2010: Chapter 8 and Appendix A Gamma, Helm, Johnson, Vlissides, Design s: Elements of Reusable Object-Oriented Software, Addison-Wesley, Reading, MA, 1994 http://www.fluffycat.com/java/patterns.html is a reference and example site for Design s in Java. It contains links to Amazon.com books on design patterns too. Design Card: http://undergraduate.csse.uwa.edu.au/units/cits4401/readings/desig npatternscard.pdf Bernd Bruegge & Allen Dutoit Object-Oriented Software Engineering: Conquering Complex and Changing Systems 25 Page 7