Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Similar documents
A few important patterns and their connections

Tuesday, October 4. Announcements

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

Introduction to Software Engineering: Object Design I Reuse & Patterns

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

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

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

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

What is Design Patterns?

SDC Design patterns GoF

Design Patterns. An introduction

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

administrivia today UML start design patterns Tuesday, September 28, 2010

Object-oriented Software Design Patterns

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

The Design Patterns Matrix From Analysis to Implementation

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

Design Patterns: Structural and Behavioural

Plan. Design principles: laughing in the face of change. What kind of change? What are we trying to achieve?

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

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

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

Software Design COSC 4353/6353 D R. R A J S I N G H

Think of drawing/diagramming editors. ECE450 Software Engineering II. The problem. The Composite pattern

Design Patterns. Dr. Rania Khairy. Software Engineering and Development Tool

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

3 Product Management Anti-Patterns by Thomas Schranz

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

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

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

UNIT I Introduction to Design Patterns

C++ for System Developers with Design Pattern

What is Design Patterns?

Design Pattern and Software Architecture: IV. Design Pattern

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

Object Oriented Paradigm

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

TDDB84. Lecture 2. fredag 6 september 13

Object-Oriented Oriented Programming

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

INSTITUTE OF AERONAUTICAL ENGINEERING

Software Development Project. Kazi Masudul Alam

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

Creational Design Patterns

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

A Rapid Overview of UML

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

Lectures 24 and 25 Introduction to Architectural Styles and Design Patterns

CSC207H: Software Design Lecture 6

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

Using Design Patterns in Java Application Development

What is Design Patterns?

UNIT I Introduction to Design Patterns

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. Jef De Smedt Beta VZW

DESIGN PATTERN - INTERVIEW QUESTIONS

A Reconnaissance on Design Patterns

Applying Design Patterns to SCA Implementations

Information systems modelling UML and service description languages

Software Eningeering. Lecture 9 Design Patterns 2

SWEN425 DESIGN PATTERNS

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

Writing your own Java I/O Decorator p. 102 Tools for your Design Toolbox p. 105 Exercise Solutions p. 106 The Factory Pattern Baking with OO

Design Patterns Lecture 2

Development and Implementation of Workshop Management System Application to Explore Combing Multiple Design Patterns

Software Design COSC 4353/6353 D R. R A J S I N G H

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Template Method Pattern. George Blankenship

Nat. Thomas Software Engineering Telephonics Inc. Long Island IEEE Lecture Series

Lecture 4: Observer Pattern, Event Library and Componentization

Lecture 20: Design Patterns II

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

CS/CE 2336 Computer Science II

An Introduction to Patterns

Design to interfaces. Favor composition over inheritance Find what varies and encapsulate it

Functional Design Patterns. Rumours. Agenda. Command. Solution 10/03/10. If you only remember one thing. Let it be this:

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

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

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

Lecture 13: Design Patterns

SYLLABUS CHAPTER - 1 [SOFTWARE REUSE SUCCESS FACTORS] Reuse Driven Software Engineering is a Business

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

Design Patterns. Gunnar Gotshalks A4-1

A Primer on Design Patterns

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

Foundations of Software Engineering Design Patterns -- Introduction

The GoF Design Patterns Reference

Composite Pattern. IV.4 Structural Pattern

Pro Objective-C Design Patterns for ios

CS 520/620 Advanced Software Engineering Fall September 27, 2016

Application Architectures, Design Patterns

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

Chapter 8, Design Patterns Visitor

1 Software Architecture

CSCI 253. Overview. The Elements of a Design Pattern. George Blankenship 1. Object Oriented Design: Iterator Pattern George Blankenship

LECTURE NOTES ON DESIGN PATTERNS MCA III YEAR, V SEMESTER (JNTUA-R09)

Improve Your SystemVerilog OOP Skills

Design Patterns. Hausi A. Müller University of Victoria. Software Architecture Course Spring 2000

Overview of Patterns: Introduction

UP Requirements. Software Design - Dr Eitan Hadar (c) Activities of greater emphasis in this book. UP Workflows. Business Modeling.

Topics. Software Process. Agile. Requirements. Basic Design. Modular Design. Design Patterns. Testing. Quality. Refactoring.

Transcription:

Plan A few important patterns and their connections Perdita Stevens School of Informatics University of Edinburgh Singleton Factory method Facade and how they are connected. You should understand how to use the patterns individually, but more importantly, how they are related and how they contribute to good OO design. See the end of this slide set for which patterns are examinable. NB not spending a lot of lecture time on patterns this year: that is because there are so many good sources of information on them elsewhere, not because they aren t important. See schedule page. Singleton Singleton: class diagram Common problem: In OO systems a class often has only one object. Sometimes, it s important to ensure that it only has one object. E.g., it s maintaining an important datum that needs to be held consistently, in only one version; or it s connecting to an external system with which your system should be having only one conversation. Solution: Singleton, one of the simplest patterns, ensures this. Key element: make the constructor private so that you can control how objects are created. Note the notation (underlining) for the class-level (static, in Java) attribute and operation. These are essential, since the constructor is private! Image: Wikipedia

Notes on Singleton Exercise Advantage: lazy instantiation possible the instance need not actually be created unless or until it is needed. Drawback: introduces global state. Drawback: great care is needed in multi-threaded applications. Advantage: often useful in conjunction with other patterns, e.g. Factory Method, Facade (coming up). Use sparingly Read the Wikipedia article on Singleton, which includes many implementations and discussions of their pros and cons, as well as discussion of the use and abuse of Singleton. For extra education, read the Talk page too. new considered harmful? Alternatives to (uncontrolled use of) new In typical OO programming, the clients of Classname use...new Classname... code to control object instantiation directly. This has two effects: 1. responsibility for deciding whether, when, and how objects are created rests with the clients; it may be widely distributed through the system, especially if there are many clients; 2. new is not polymorphic. It takes a single specific class name and creates an object of exactly that class. So a client s code must be modified, if it is to be made to work with a subclass of Classname. These effects may or may not be problems it depends on the context. new considered sometimes harmful, if misplaced The Singleton pattern can be seen as a way to give an alternative to point 1. A client of a Singleton class can still get an instance, but the responsibility for actually creating the instance belongs to the Singleton itself. Note that, despite the name, there could even be more than one object created the point is that clients don t control how many there are, the Singleton class itself does. With a private constructor, we can t subclass a Singleton, so we don t address 2. With care, we could make the constructor protected and do so... But would we ever want to? This takes us to Factory Method.

Factory Method Factory Method: class diagram Common problem: your class needs to own an object and use its services. The services you need are described by a fairly abstract class/interface (Product, say) which has various subclasses and/or a complicated creation process, that shouldn t be your class s business. Only one line of your code depends on which kind of Product you re going to have and how it s going to be built: Product p = new ConcreteProduct(...); Solution: Instead, get something else to build and give you a Product so that all your code is written purely in terms of Product. In effect, your class says Give me an appropriate Product and declines to concern itself with exactly what is returned or how it is created. Image: http://stackoverflow.com/questions/5739611/ differences-between-abstract-factory-pattern-and-factory-method Where does a factory method live? Static factory method example In a factory, of course :-) In most versions of the Factory Method pattern, and in the original Gamma description, it s important that the factory that creates a product is in a different class and class hierarchy from the product itself. Without that, we don t get advantage 2. above (making object creation polymorphic). We want depending on a factory class not to imply depending on the concrete product class, and if they re the same class, of course it does! But some other advantages of Factory Method apply when a product can be its own factory, and these days this is sometimes also included in Factory Method. Other sources give this a different name, e.g. static factory method, and get irate when people lump them together. (The sourcemaking page is a little ambivalent, I think: watch out.) class Point { private float x, y; // secretly, we use Cartesian coordinates inside private Point (float xin, float yin) { x = xin; y = yin; } public static Point createcartesianpoint(float x, float y) { return new Point(x, y); } public static Point createpolarpoint(float r, float theta) { // calculate x and y using trigonometry... return new Point(x, y); } NB because we can t override static methods, it s not straightforward to get all the benefits of Factory Method this way.

Relation between Factory Method and Singleton A simple example of Factory Method How does your class contact the object that provides the factory method? One possibility: it may be a Singleton. In fact, the Singleton s getinstance() method is a static factory method, so if you count that as Factory Method, you can see Singleton as a special case of Factory Method. is the single method ContentHandler createcontenthandler(string mimetype) of the interface ContentHandlerFactory in the JDK. A class that implements ContentHandlerFactory must implement that method, i.e. provide a way for clients to supply a string describing a mime type and get back a(n appropriate, we hope!) ContentHandler. This makes it possible to write clients that work uniformly on all ContentHandlers, and do not have to concern themselves with what subclasses ContentHandler may have, or indeed, with how mime type strings map to those classes. Naming: a further advantage of Factory Method Visibility of constructor In the Point example, we used meaningful names to distinguish different ways of creating an object. More generally: could be different in argument list, or meaning of arguments (as in Point) or in other characteristics e.g. efficiency. Constructor overloading doesn t get you all the flexibility you want, and leads to unreadable code anyway. NB this is an advantage we get both from Factory Method and from static factory methods. E.g. Product p = factory.createspaceoptimisedproduct(17); might be creating an object of a subclass of Product, or not: we don t need to know. If your factory is not the same class as the product, the product class still needs a public (or at least, package visible) constructor or its own static factory method! as the factory will need to use it. Inside the product class, you have a choice. Constructor could be: public: maximum flexibility for clients. If you ve added the factory method to old code, leaving the public constructor means old clients don t have to be rewritten. private: now your objects can only be created via the factory method(s). The class cannot be subclassed. protected: now the class can be subclassed, and subclasses might add new factory methods. Risk and benefit.

Abstract Factory Facade An Abstract Factory is an object containing a collection of related Factory Methods. Useful where which family of Product subclasses is appropriate is determined by one condition, e.g., which operating system we re running on. Classic example: GUI toolkits. See for example java.awt.toolkit. Common problem: you have roughly separated your classes into two packages, but there are several dependencies of classes in package A on classes in package B. It becomes hard to work out what the effect of a change in package B will be, and developers who want to use the services of package B have to understand the detail of what s inside it. Solution: you create a Facade class whose job it is to present a single, simple interface to package B. All requests for services from anything in package B are sent to an object of the Facade class. This object may just forward the request to the right object, or it may do more complex things. Facade: class diagram Notes on Facade Advantage: very useful way to control dependencies Advantage: hides a multitude of sins. Lets you redesign a subsystem behind a facade without impacting what is outside. Disadvantage: incurs the cost of extra method calls (usually not a problem). The Facade may be a Singleton, but this isn t always necessary. Image: http://best-practice-software-engineering.ifs.tuwien.ac. at/patterns/facade.html

Creational patterns Structural patterns * Abstract Factory Builder * Factory Method Prototype * Singleton (Learn those with *) Adapter Bridge * Composite * Decorator * Facade Flyweight Proxy (Learn those with *) Behavioral patterns Chain of responsibility Command Interpreter Iterator Mediator Memento * Observer * State * Strategy Template method * Visitor (Learn those with *)