Design Patterns Spring 2017.

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

Matt Klein. Decorator Pattern  Intent  Attach Additional responsibilities to an object by dynamically. Decorators provide a flexible alternative to.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
IEG3080 Tutorial 7 Prepared by Ryan.
Design Patterns CS is not simply about programming
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.
Dept. of Computer Engineering, Amir-Kabir University 1 Design Patterns Dr. Noorhosseini Lecture 2.
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.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
BDP Behavioral Pattern. BDP-2 Behavioral Patters Concerned with algorithms & assignment of responsibilities Patterns of Communication between Objects.
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.
Creational Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
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.
Software Design Patterns (1) Introduction. patterns do … & do not … Patterns do... provide common vocabulary provide “shorthand” for effectively communicating.
Computing IV Singleton Pattern Xinwen Fu.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Design Patterns Gang Qian Department of Computer Science University of Central Oklahoma.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Structural Design Patterns
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.
Factory Method Explained. Intent  Define an interface for creating an object, but let subclasses decide which class to instantiate.  Factory Method.
What to know for the exam. Smalltalk will be used for questions, but there will not be questions about the grammar. Questions might ask – how particular.
08 - StructuralCSC4071 Structural Patterns concerned with how classes and objects are composed to form larger structures –Adapter interface converter Bridge.
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.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
CS 210 Final Review November 28, CS 210 Adapter Pattern.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.
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.
CS 210 Proxy Pattern Nov 16 th, RMI – A quick review A simple, easy to understand tutorial is located here:
Behavioural Patterns GoF pg Iterator GoF pg. 257 – 271 Memento GoF pg By: Dan Sibbernsen.
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
Overview of Creational Patterns ©SoftMoore ConsultingSlide 1.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Abstract Factory Pattern
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Common Design Patterns
Design Patterns Lecture part 2.
Factory Patterns 1.
Introduction to Design Patterns
Behavioral Design Patterns
How to be a Good Developer
Design Patterns with C# (and Food!)
object oriented Principles of software design
Abstract Factory Pattern
Decorator Design Pattern
Intent (Thanks to Jim Fawcett for the slides)
Multiuser Protection and the Mediator Pattern
Software Engineering Lecture 7 - Design Patterns
Informatics 122 Software Design II
Design Patterns Part 2: Factory, Builder, & Memento
Structural Patterns: Adapter and Bridge
Informatics 122 Software Design II
Presentation transcript:

Design Patterns Spring 2017

Head First Design Patterns Book Useful books The “Gang of Four” book 1994 Head First Design Patterns Book 2004

What you can do A design pattern is a general solution to a common problem in a context. What you want What you have

Chain of Responsibility Design Pattern Index Creational Structural Behavioural Factory Method Adapter Template Abstract Factory Bridge Strategy Builder Composite Command Singleton Decorator State Multiton Facade Visitor Object pool Flyweight Chain of Responsibility Prototype Front controller Interpreter Proxy Observer Iterator Mediator Memento

Motivation A cook knows a lot of recipes They look up the ones they forget Design patterns are recipes for programmers How can you choice a good strategy If you don’t know the range of available strategies How can you talk about the choices If they don’t have well understood names Angel cake Instruction: Fold don’t stir.. I used a food blender because it was there My angel cake was afterwards remembered as a pancake

WARNING: Overuse of design patterns can lead to code that is downright over-engineered. Always go with the simplest solution that does the job and introduce patterns only where the need emerges. [Head First Design Patterns]

Good design principles Program to interfaces not to an implementation Separate what changes from what does not Encapsulate what varies behind an interface Favor composition over inheritance Loosely couple objects that interact Classes should be open for extension, but closed for modification Each class should have one responsibility Depend on abstractions, not concrete classes

Good use of Design Pattern Principles Let design patterns emerge from your design, don’t use them just because you should Always choose the simplest solution that meets your need Always use the pattern if it simplifies the solution Know all the design patterns out there Talk a pattern when that simplifies conversation

Creational Design Patterns

Factory Design Pattern Goals Separate what changes from what does not Encapsulate what varies behind an interface Depend on abstractions, not concrete classes Loosely couple objects that interact Design should be open for extension but closed to modification

Problems with using new So myClass = new MyClass(….) is bad.. It might be invoked from many places It might be replaced by new MyBetterClass(…) Many different subclasses might exist Want to be able to decide the subclass at runtime Want to create the correct subclass not old superclass Without ugly code present everywhere Want perhaps to pass around a tool to create whatever the tool is designed to create May not know what to create until run time May wish to identify classes by GUID’s, and register them

Factory Solution Wrap code that creates objects inside a method Interface = createMyClass(parameters) We can change the class created now in one place Parameter can identify what to create Might make createMyClass static Ie. static MyClass::createMyClass(…) But this reassociates the creation with the class If we want to pass method around put it in a factory object (clearer than pointer to method)

Advantages Protects higher level code from ugly lower level detail such as which class should be created Any change to the class created is made just once We can make the objects to create dynamic Simply pass a pointer to a factory object to the things that need to create the objects Then the factory object becomes conceptually the instruction regarding what to create We can override createMyClass in subclasses or use it in any other way we wish (ie. Use with template design pattern described later)

Factory Pattern Definition The Factory Method Pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. Question: How should we delete objects created using a factory. Should we have a matching deleteMyClass() Should it make the pointer to the object null Or should we use reference counting??

Abstract Factory Pattern Suppose we have a factory that can be told what objects to create from a choice of all within a framework Then we can create a new factory with the same “abstract” interfaces to create the same objects but from a very different framework Deciding which framework to use then becomes deciding which concrete instantiation of an abstract factory class to use

Advantages Protects us from creating components from many frameworks that can’t talk to each other Makes it easy to decide at run time which framework we want to use The framework chosen is transparent to all higher level code Makes it easier to add new frameworks if we have need to do so

Example Domino’s pizza’s are flat Pizza Hut’s pizza’s are deep dish pizza’s Both offer a variety of subclasses of pizzas If we create an abstract factory Subclass a Domino’s pizza factory Subclass a Pizza Hut’s pizza factory We won’t need to create a pizza store for each Just give each pizza store a different factory Adding support for New York Pizza Piece of cake (pizza)

This is employing the template design pattern (described later)

This is the template approach Can also pass factory to pizzastore Save it internally This is the strategy design pattern

Abstract Factory Definition The Abstract Factory Pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes

Builder Design Pattern Want to construct a complex object built from many component objects Want to say what must be constructed But don’t want to expose the details of how the object is composed from its components Use a builder object that keeps track of the object being built and allows construction to be performed via method calls in the builder

Advantage Programming to an interface Objects built according to actions, not code The composite objects are be constructed differently within the builder transparently Additional useful structures such as indices, hashing etc. can be implemented as needed inside the builder But extra layer of indirection

Builder Definition The Builder Pattern encapsulates the construction of a product and allows it to be constructed as a sequence of steps.

Singleton Design Pattern Ensure there is only one instance of an object Ensures that global objects are not misused Use a factory to construct it Have the factory return it if already constructed Make the constructor(s) private to the class Put the factory method in the class Be careful to ensure logic is thread safe

= null;

The constructor is private so it can’t be invoked directly from outside of the class

Singleton Definition The Singleton Pattern ensures that a class has only one instance, and provides a global point of access to it Need to ensure that the assignment of the single instance to a static variable (as well as other operations) are thread safe if might be using singleton in multiple threads Question: how might a singleton object be deleted

Multiton Pattern Generalisation of the Singleton Design Pattern Creates a singleton object associated with a given key Enforces a one-to-one mapping between keys and the object (if present) associated with that key

Object Pool Design Pattern Consider implementing if you have a lot of objects of the same type being constantly created and deleted Factory maintains a list of constructed but now release objects Creation involves returning an object from the pool if it exists else creating a new object Deletion involves adding deleted object to the pool for later reuse

Advantages Malloc/new are expensive operations Dramatically reducing the need to create memory can produce significant performance improvement Can return elements of a large array instead of creating each element independently Can also manage the maximum number of objects of a type created more easily and potentially transparently Can potentially control number of threads created

Prototype A collection of proto-typical instantiations of a class exist To create a new instance of one of these classes the class is cloned from its prototype Or if the class has no state it can simply be reused in many places (see: flyweight pattern)

Advantages How an instance of a class is to be created is fully determined by referring to an example This might simplify the construction of a very complex object New prototypes can be added to the list Afterwards easy to create copies of these Users may choose the prototype they want Can visually show the choices easily But copying an object can also be complicated

Structural Design Patterns

Adapter uses uses wants to use

Adapter Intent: “Convert the interface of one class to make it compatible with another class.” Motivation: Components often have incompatible interfaces that cannot be used by a client but also cannot be modified. Applicability: When an intermediate representation between one component and another is required When you want to isolate a client from changes in an external component’s interface

Adapter Two kinds of adapters: Object adapters (use composition) Class adapters (use multiple inheritance)

Object Adapter [en.wikipedia.org]

Class Adapter [en.wikipedia.org]

Adapter Participants: Client: Calls the adapter; is completely isolated from the adaptee. Adapter: Forwards calls between client and adapter. The adapter may have to interact with multiple classes. Adaptee: A component being adapted.

Adapter There might be no exact match between old and new interfaces: Clients have to watch for potential exceptions

Bridge Design Pattern Separates abstraction from its implementation The abstraction can be very different from the implementation The abstraction uses code to “bridge” the differences Replaces is-a inheritance / implementation relationship with has-as composition relationship Essentially a wrapper hiding implementation Useful when the implementation might be selectable or switchable at run time

One usage of strategy design pattern

Composite Design Pattern Given a hierarchy implement the same interface on every member of this hierarchy even if members differ Subclass from this common functionality Do not distinguish between actions on a leaf and actions on a subtree Eg . Move location of leaf / subtree Traverse all nodes in/under node Print tree

Composite Definition The Composite Pattern allows you to compose objects into tree structures to represent part-whole hierarchies. Composite lets client treat individual objects and composition of objects uniformly, irrespective of how the objects within the hierarchy differ.