Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia.

Slides:



Advertisements
Similar presentations
Design Patterns.
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.
Patterns Reusable solutions to common object-oriented programming problems When given a programming problem, re-use an existing solution. Gang of Four.
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.
DESIGN PATTERNS Redesigning Applications And
Design Patterns Module Name - Object Oriented Modeling By Archana Munnangi S R Kumar Utkarsh Batwal ( ) ( ) ( )
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 Standardized Recurring model Fits in many location Opposite of customization Fundamental types of pattern Choose and use as desired and.
Design Patterns.
Design patterns. What is a design pattern? Christopher Alexander: «The pattern describes a problem which again and again occurs in the work, as well as.
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.
Design Pattern. The Observer Pattern The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all.
SOEN 6011 Software Engineering Processes Section SS Fall 2007 Dr Greg Butler
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.
DESIGN PATTERNS CSC532 Adv. Topics in Software Engineering Shirin A. Lakhani.
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.
CS 210 Adapter Pattern October 19 th, Adapters in real life Page 236 – Head First Design Patterns.
Computing IV Singleton Pattern Xinwen Fu.
Object Oriented Software Engineering Chapter 16 and 17 review 2014/06/03.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), 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.
Creational Patterns
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.
Proxy.
The Singleton Pattern SE-2811 Dr. Mark L. Hornick 1.
DESIGN PATTERNS COMMONLY USED PATTERNS What is a design pattern ? Defining certain rules to tackle a particular kind of problem in software development.
FACTORY METHOD. Design Pattern Space Purpose ScopeCreationalStructuralBehavioral ClassFactory MethodAdapterInterpreter Template Method ObjectAbstract.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
CS616: Software Engineering Spring 2009 Design Patterns Sami Taha.
Software Design Patterns Curtsy: Fahad Hassan (TxLabs)
Design Patterns Software Engineering CS 561. Last Time Introduced design patterns Abstraction-Occurrence General Hierarchy Player-Role.
Design Patterns Introduction
Java Design Patterns Java Design Patterns. What are design patterns? the best solution for a recurring problem a technique for making code more flexible.
Interface Patterns. Adapter Provides the interface a client expects, using the services of a class with a different interface Note Avoid using object.
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:
Five Minute Design Patterns Doug Marttila Forest and the Trees May 30, 2009 Template Factory Singleton Iterator Adapter Façade Observer Command Strategy.
An object's behavior depends on its current state. Operations have large, multipart conditional statements that depend on the object's state.
7 April 2004CSci 210 Spring Design Patterns 2 CSci 210.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
MPCS – Advanced java Programming
Design Patterns Lecture part 2.
Introduction to Design Patterns
How to be a Good Developer
object oriented Principles of software design
WARNING These slides are not optimized for printing or exam preparation. These are for lecture delivery only. These slides are made for PowerPoint 2010.
How to be a Good Developer
Design Patterns in Operating Systems
Software Engineering Lecture 7 - Design Patterns
Singleton design pattern
Design Patterns Part 2: Factory, Builder, & Memento
Presentation transcript:

Design Patterns SE464 Derek Rayside images from NetObjectives.com & Wikipedia

modes of normal composition fitness for future

CreationalStructuralBehavioural Abstract Factory Builder Factory Method Prototype Singleton Adapter Bridge Composite Decorator Facade Flyweight Proxy Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor

CreationalStructuralBehavioural Singleton Adapter Bridge Composite Facade Command Interpreter Observer Strategy Visitor

CreationalStructuralBehavioural Singleton Adapter Bridge Facade Command Observer Strategy

CreationalStructuralBehavioural Singleton Adapter Bridge Observer Strategy

Observer (Publish/Subscribe) Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Observer (Publish/Subscribe) Define a one-to-many dependency between objects so that when one object changes state, all its dependents are notified and updated automatically.

Observer (Publish/Subscribe) Design challenge: the observers need to know more

Observer (Publish/Subscribe) Design challenge: the observers need to know more Solution 1: add parameters to notify method public interface Observer { public notify(String acct, double amt, String Addr, String overdraftAcct); } public Audit implements Observer { public void notify(String auditedAccount, double overdrawnAmount, String ignore Addr, String ignoreOverdraftAcct) { // write info to log, take other appropriate action } }

Observer (Publish/Subscribe) Complications: Need to add new parameters in all existing Listeners May be sending unused data to Listeners

Observer (Publish/Subscribe) Design challenge: the observers need to know more Solution 2: callbacks

Observer (Publish/Subscribe) Complications: May reveal too much information to Listeners o Solution: pass a Msg object that encapsulates the Account information instead of passing Account object by reference Each listener will be blocked until previous listeners are done (in single-threaded situations) The state of the objects passed to Listeners might change in concurrent applications

Observer (Publish/Subscribe) Design challenge: the observers need to know more Solution 3: reify the event

Observer (Publish/Subscribe) Design challenge: the observers need to know more Solution 4: new Observer interface Very simple How to distinguish between the old (legacy) interface and the new Observer interface o ex. The new interface extends old interface and adds two new methods.

Strategy Vary the algorithm independently of the clients who use it.

Strategy Vary the algorithm independently of the clients who use it.

Strategy Vary the algorithm independently of the clients who use it. 1. Who chooses the strategy? 2. Are strategy objects mutable? Examples: Strategy used to sort a list of numbers - if known the list is almost sorted, use merge sort; otherwise use quicksort

Strategy Who chooses the strategy? a. client b. context c. config file (not pictured)

Strategy Mutable Strategy Objects easier to return more complex results need to be instantiated for each use Stateless Strategy Objects reusable re-entrant simpler API usage rules can be Singleton

Singleton Ensure a class has only one instance, and provide a global point of access to it.

Singleton Advantages convenience controlled access reduced namespace can substitute alternatives more flexible than static methods Issues global variables o make testing harder synchronization may reduce parallelism memory leaks initialization class-loaders vs VMs distributed systems may hinder re-use

Singleton: traditional implementation public class Singleton { private static final Singleton INSTANCE = new Singleton(); // Private constructor prevents external instantiation private Singleton() {} public static Singleton getInstance() { return INSTANCE; } }

Singleton: safer initialization [Pugh] public class Singleton { // Private constructor prevents external instantiation private Singleton() {} /** * SingletonHolder is loaded on the first execution * of Singleton.getInstance() or the first access to * SingletonHolder.INSTANCE, not before. */ private static class SingletonHolder { static final Singleton INSTANCE = new Singleton(); } public static Singleton getInstance() { return SingletonHolder.INSTANCE; } }

Adapter (Wrapper) Convert the interface of a class into another interface clients expect.

Adapter

To consider: 1.Large delta between local & foreign => facade 2.Exceptions? 3.Instantiation? 4.Does the adapter need to add functionality? example: SWT and Swing. 5.Stateful? Probably shouldn't be.

Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Bridge Decouple an abstraction from its implementation so that the two can vary independently.

Adapter vs Bridge Adapter connection unforseen interfaces already exist Bridge connection forseen need to be able to subclass both abstraction and implementation

Other patterns you need to know Lectures so far & Lab1 visitor interpreter iterator composite Reading & Future Lecture facade [cf Bowman] command

Comprehension Questions What are some ways of adapting an existing Observer class when more information is needed by a type of event? What are some pros and cons of these methods? Name some benefits and downsides of mutable and stateless Strategy objects. What are some issues with the Singleton pattern? Name some uses of the Strategy, Adapter, Singleton, Bridge, and Observer patterns. In GUI Framework, when a button is pressed, an OnButtonPressed event is received. What type of pattern is being used? o Answer: Observer Design Pattern is used. All the Observers (or Listeners in Java) are notified.

Comprehension Questions Why is it generally considered bad practice to have mutable Singleton classes? What are some differences between the Adapter and the Bridge design pattern? Give an example of both to support your answer. In Java, switching between different layouts using a context makes use of what design pattern? o Answer: Strategy Design Pattern