Copyright W. Howden1 Singleton, Wrapper, and Facade Patterns CSE 111.

Slides:



Advertisements
Similar presentations
Welcome to. Who am I? A better way to code Design Patterns ???  What are design patterns?  How many are there?  How do I use them?  When do I use.
Advertisements

18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
Design Patterns Copyright © Vyacheslav Mukhortov, Nikita Nyanchuk-Tatarskiy, Copyright © INTEKS LLC,
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Copyright W. Howden1 Lecture 7: Functional and OO Design Descriptions.
Lecture 5a: Sequence Interaction Diagrams CSE 111 Copyright W. Howden1.
05/26/2004www.indyjug.net1 Indy Java User’s Group June Knowledge Services, Inc.
Copyright W. Howden1 Lecture 6: Design Evaluation and Intro to OO Design Patterns.
Copyright W. Howden1 Lecture 8: O/O Programming. Copyright W. Howden2 Topics OO Programming Languages Developing programs from Designs –Class and method.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
Copyright W. Howden1 Lecture 14: Callbacks, Singletons and Wrappers.
Design Patterns. What are design patterns? A general reusable solution to a commonly occurring problem. A description or template for how to solve a problem.
March R McFadyen1 GoF (Gang of Four): Gamma, Johnson, Helm & Vlissides Book: Design Patterns: Elements of Reusable Object-Oriented Software.
(c) 2010 University of California, Irvine – André van der Hoek1June 29, 2015 – 08:55:05 Informatics 122 Software Design II Lecture 8 André van der Hoek.
Lecture a: Additional UML Models: Package, Activity, Deployment Lecture b: Generalization, Aggregation and Additional Domain Model Notation Copyright W.
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
PRESENTED BY SANGEETA MEHTA EECS810 UNIVERSITY OF KANSAS OCTOBER 2008 Design Patterns.
Design Patterns academy.zariba.com 1. Lecture Content 1.What are Design Patterns? 2.Creational 3.Structural 4.Behavioral 5.Architectural 6.Design Patterns.
CERN – European Organization for Nuclear Research GS Department – Administrative Information Services Design Patterns in Groovy Nicolas Décrevel Advanced.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
Vrije Universiteit amsterdamPostacademische Cursus Informatie Technologie Idioms and Patterns polymorphism -- inheritance and delegation idioms -- realizing.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Patterns Alan Shalloway, James Trott, Design Patterns Explained, Addison-Wesley, Gamma, Helm, Johnson, Vlissides, Design Patterns, Elements.
Design Patterns.
05 - Patterns Intro.CSC4071 Design Patterns Designing good and reusable OO software is hard. –Mix of specific + general –Impossible to get it right the.
CSSE 374: Introduction to Gang of Four Design Patterns
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
January 12, Introduction to Design Patterns Tim Burke References: –Gamma, Erich, et. al. (AKA, The Gang of Four). Design Patterns: Elements of Reusable.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
Design Patterns CSCI 5801: Software Engineering. Design Patterns.
18 April 2005CSci 210 Spring Design Patterns 1 CSci 210.
L11-12: Design Patterns Definition Iterator (L4: Inheritance)‏ Factory (L4: Inheritance)‏ Strategy (L5: Multiple Inheritance)‏ Composite (L6: Implementation.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
Testing Extensible Design Patterns in OO Frameworks through Scenario Templates D.S. Sanders Software Verification & Validation.
ECE450S – Software Engineering II
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.
Introduction to Design Patterns. Questions What is a design pattern? Who needs design patterns? How different are classes and objects in APL compared.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: MORE Examples
Design Patterns Spring 2017.
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
Design Patterns with C# (and Food!)
object oriented Principles of software design
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
DESIGN PATTERNS : Introduction
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Design Patterns Imran Rashid CTO at ManiWeber Technologies.
Informatics 122 Software Design II
Presentation transcript:

Copyright W. Howden1 Singleton, Wrapper, and Facade Patterns CSE 111

Patterns Pattern Components: Context + problem -> solution Purpose of a pattern describes a problem that occurs over and over and the core of a design idea that can be used over and over without using the same solution twice you must re-implement a pattern in each application Copyright W. Howden2

History 1991: Patterns Workshop at OOPSOL conference 1994: Design Patterns: Elements of Reusable Object-Oriented Software, by Erich Gamma, Richard Helm, Ralph Johnson and John Vlissides (the GOF – Gang of Four) Copyright W. Howden3

Design Patterns (GOF) Creational Abstract Factory, Singleton, Factory Method, Builder, Prototype Structural Adapter, Bridge, Composite, Decorator, Façade, Flyweight, Proxy Behavioral Chain of Responsibility, Command(undo/redo), Interpreter, Iterator, Mediator, Memento, Observer, State, Strategy, Template, Method, Visitor Non-GoF patterns Model-View-Controller, Expert, Creator, Controller Copyright W. Howden4

5 Singleton Pattern Satisfies the following requirements: –global access to data –only one copy/instance of a class –lazy instantiation the one copy is not created until it is needed desired because of potential costly initialization, and object may not always be needed Applications –logging object, wherein different parts of the system are recording log events

How is it done? public class Logger {private static Logger instance; // additional class variables private Logger() {} public static Logger getInstance() {if (instance == null) {instance = new Logger();} return instance; } // possible additional methods } Copyright W. Howden6

Interesting Properties private constructor ensures that no one can directly create instances of the class have to go through getInstance() which only allows one instance getInstance() method is static since we can’t create an instance of the class, how are we going to call the getInstance method? –make it static Lazy instantiation: not created until needed Copyright W. Howden7

Eager Instantiation Version public class Logger {private static Logger instance = new Logger(); // additional class variables private Logger() {} public static Logger getInstance() {return instance; } // possible additional methods } Copyright W. Howden8

How does Eager Work When an instance of a class is created, or its static variables are referenced, the class loader loads Logger At this point all of its static initializers are run, so that the singleton instance of Logger Copyright W. Howden9

All Static Alteranative public class Logger {// public static class variables private Logger() {} // public static methods } Copyright W. Howden10

Singletons vs Static? Singletons can implement lazy evaluation Singletons can be modified to form “multitons” (up to k instances, where k > 1) Singletons are objects and can be passed around Singletons can implement interfaces –(e.g. the non-static methods) Copyright W. Howden11

Copyright W. Howden12 DS Singleton Example Want to have a globally accessible object that records session history statistics Gets updated from different places, e.g. when different kinds of messages are generated Use the singleton pattern for a History class

Copyright W. Howden13 Wrappers Take an existing entity and alter its behavior or interface by “embedding” it inside a class Types of wrappers –> Decorator: alters behavior or responsibilities but not the interface –Adapter: emphasis is on altering interface, but could add some additional responsibilities or behavior

Copyright W. Howden14 Decorator Strategies? Wrapper class subclasses wrappee class so has same interface Wrapper constructor takes wrappee as an argument, so it is “altering” it at runtime –uses wrappee methods to define new behavior –alters behavior without subclassing –client calls wrapper methods

Copyright W. Howden15 Composition Strategy for Decorators Wrappee +Wrapper(in Wrappee x) -Wrappee:x Wrapper

Comments An object may be multiply-wrapped, requiring a more complex pattern Set of possible behavior decorations can be chosen at run time Will see this in the following DS example Copyright W. Howden16

Dating System Example for Decorator Wrapper We decide to allow the user to ask to get a date, as before, but also specify several possible additional options 1.send an to the selected date 2.send a reminder to me 3.send flowers to the selected date One or none of these “decorations” can be added Copyright W. Howden17

DS Decorator Pattern Class Structure Copyright W. Howden18

Class Usage Pattern Copyright W. Howden19

Sequence of Execute Calls Message 5 will call the execute() method of the “outermost” class. This could be dR:DateRequest if there were no decorator options specified. Suppose there was one, the Date option. Then the outermost execute() method will be that of the instance x of Date that was created. x.execute() will call the execute() method for dR:DateRequst, which was passed in by the constructor for x. dr.execute() will find a date, i.e. memberData for that date, and return it to the calling x.execute(). x,execute() will “decorate” the return by sending an It will then return the date to the calling DomainLogic method. Copyright W. Howden20

Notes on Class Structure No concrete decorator class is used more than once The DateRequest class is used exactly once Copyright W. Howden21

execute() definitions DR: calls its component’s execute() DRDecorator: abstract DateRequest: original method that looks for a date, returns null if no date found. Other wise returns an instance of MemberData Concrete wrappers: execute() calls its component’s execute() Copyright W. Howden22

Concrete Decorator execute() Definitions – E.g. Me class Me extends DRDecorator { DR: mydR public Me(DR dR) { this.mydR = dR);} public MemberData execute() {memberData = mydR.execute(); if (memberData != null) {/** send to me;} return(memberData); } Copyright W. Howden23

Possible Additional Details Might create a new class called Date –Consists of both the MemberData for the user/dater asking for a data, and the MemberData for the discovered datee –Returned from the execute() method of a new DateRequest when a date is found Why? –when the Me decorator has to me, the user, it would have the data necessary to send that Copyright W. Howden24

Copyright W. Howden25 An Additional Decorator Example – Java Stream Wrapper FileInputStream has a simple read() operation that reads a single byte Very inefficient, would like to buffer the input Solution: “decorate” FileInputStream with a decorator class BufferedInputStream Diagram shows two wrappers for FileInput Stream –BufferedInputStream and LineNumberInputStream

File Decoration in Java Copyright W. Howden26

Comments on File Decorator BufferedInputStream and LineNumberInputStream both have class variables of type InputStream because they subclass the abstract decorator class FilterInputStream Instances of wrappers can be created with instances of FileInputStream and certain other streams such as StringBufferInputStream Copyright W. Howden27

Copyright W. Howden28 Wrappers (once again) Take an existing entity and alter its behavior or interface by “embedding” it inside a class Types of wrappers –Decorator: alters behavior or responsibilities but not the interface –> Adapter: emphasis is on altering interface, but could add some additional responsibilities or behavior

Copyright W. Howden29 Adapter Strategies Can use either inheritance or composition –Inheritance, or Class Adapter Adapter class implements the required new interface Adapter extends the adaptee, with new methods for performing the adaptee responsibilities, which conform to the new interface –Composition, or Object Adapter Adapter class implements the required new interface Adapter’s constructor takes an adaptee object as an argument Adaptor methods reference the adaptee object via its adaptee class methods

Copyright W. Howden30 Class Adapter

Copyright W. Howden31 Object Adapter

DS Adapter Pattern - Object Adapter Original DateRequest has a constructor with two parameters: userName and daterPrefs It returns a memberData object (or null) In the expanded version given in our Decorator pattern, two of the decorators need the address for the userMember in the case where a date is found -> use a DateRequest adaptor to return a Date object Copyright W. Howden32

DateRequestAdapter Copyright W. Howden33

New Adapter execute() Needs to augment the returned MemberData object to form a Date object Uses the getMemberData(UserName) method in the DataBase subsystem’s interface to get the needed memberData Returns null if MemberData return is null or if UserName name is not in the DB Copyright W. Howden34

Risky Programming? The first version of the system returned a MemberData object for a member who satisfied the DaterPrefs The UserName it passed was actually ignored, so does this mean a date could be found for a person who was not a member? No, since UserName is the type of a name that has been verified to be a user Copyright W. Howden35

Another Object Adapter Need to create a class version of the Boolean primitive data type used to pass data back out through a method parameter recall all parameters in Java are call by value –primitive value of address of object Target interface for new adapter class has set() and get() methods instead of the infix methods for primitive types (i.e. x = y) Copyright W. Howden36

Object Adaptor Copyright W. Howden37

Copyright W. Howden38 Interface: BoolRefInterface { setBool(boolean b); boolean getBool() } Adaptee: primitive boolean value Adaptor: class BooleanRef implements BoolRefInterface {public boolean b; public BooleanRef(boolean x){this.b = x} public set(boolean x) {this.b = b;} public get() {return this.b;} }

Copyright W. Howden39 Facade Pattern Purpose: Provide a unified interface to a set of classes in a subsystem. Rationale: Makes system easier to use, promotes low coupling, supports layering, re-use DS Example: the Business/Domain Logic and the DataBase subsystems have facade classes that serve as subsystem controllers and interfaces

Without and With a Facade Copyright W. Howden40

Comments on Phase II Deliverables Re-doing design documents with added functionality (domain models, sequence diagrams, collaboration diagrams, class diagrams) New models: State model for GUI Observations on each of the patterns where used if used where may or may not be useful Copyright W. Howden41

Assignment 12 Consider potential applications of the the three patterns here: –singleton, wrappers, facade –either apply them in your project or explain why you do not think it is practical or useful, with examples Copyright W. Howden42