Design Patterns Section 7.1 (JIA’s) Section 7.2.1 (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section 10.4.1 (JIA’s)

Slides:



Advertisements
Similar presentations
Design Patterns.
Advertisements

Design Patterns based on book of Gang of Four (GoF) Erich Gamma, Richard Helm, Ralph Johnson, and John VlissidesGang of Four (GoF) Elements of Reusable.
 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
COP 3331 Object Oriented Analysis and Design Chapter 7 – Design by Abastraction Jean Muhammad.
Design Patterns Nelson Padua-Perez Chau-Wen Tseng Department of Computer Science University of Maryland, College Park.
Patterns Lecture 2. Singleton Ensure a class only has one instance, and provide a global point of access to it.
IEG3080 Tutorial 7 Prepared by Ryan.
March Ron McFadyen1 Adapter An adapter converts the interface of a class into another interface clients expect. An adapter lets classes work.
Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Design Patterns.
Chapter 8 Object Design Reuse and Patterns. Finding Objects The hardest problems in object-oriented system development are: –Identifying objects –Decomposing.
ECE 355 Design Patterns Tutorial Part 2 (based on slides by Ali Razavi) Presented by Igor Ivković
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.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Software Design Design Pattern Basics. Introduction to Design Patterns©2002, Michael J. Lutz2 What Are Patterns? Each pattern describes a problem which.
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.
CS 325: Software Engineering March 17, 2015 Applying Patterns (Part A) The Façade Pattern The Adapter Pattern Interfaces & Implementations The Strategy.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
Patterns in programming 1. What are patterns? “A design pattern is a general, reusable solution to a commonly occurring problem in software. A design.
SOFTWARE DESIGN AND ARCHITECTURE LECTURE 27. Review UML dynamic view – State Diagrams.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns IX Interpreter, Mediator, Template Method recap.
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.
Chapter 8 Object Design Reuse and Patterns. Object Design Object design is the process of adding details to the requirements analysis and making implementation.
Patterns COM379 University of Sunderland James Malone.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
Design Principle & Patterns by A.Surasit Samaisut Copyrights : All Rights Reserved.
ECE450 - Software Engineering II1 ECE450 – Software Engineering II Today: Design Patterns VIII Chain of Responsibility, Strategy, State.
Programmeerimine Delphi keskkonnas MTAT Programmeerimine Delphi keskkonnas MTAT Jelena Zaitseva
Design Patterns By Mareck Kortylevitch and Piotreck Ratchinsky.
Copyright © Active Frameworks Inc. - All Rights Reserved - V2.0Design Pattern Catalog - Page L3-1 PS95&96-MEF-L10-1 Dr. M.E. Fayad Creationa.
Design Pattern. Definition: A design pattern is a general reusable solution to a commonly occurring problem within a given context in software design.
Sadegh Aliakbary. Copyright ©2014 JAVACUP.IRJAVACUP.IR All rights reserved. Redistribution of JAVACUP contents is not prohibited if JAVACUP.
CSE 332: Design Patterns Review: Design Pattern Structure A design pattern has a name –So when someone says “Adapter” you know what they mean –So you can.
Design Patterns References: Xiaoping Jia, Object-Oriented Software Development Using Java;Douglas C.Schmidt, Design Pattern Case Studies with C++
Class & Object Adapter Patterns (with a focus on Class Adapter) Tim Gutowski CSPP 51023, Winter 2008.
Design Patterns Introduction
1 Chapter 5:Design Patterns. 2 What are design pattern?  Schematic description of design solution to recurring problems in software design and,  Reusable.
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
CS251 – Software Engineering Lectures 18: Intro to DP Slides by Rick Mercer, Christian Ratliff, Oscar Nierstrasz and others 1 و ابتغ فيما آتاك الله الدار.
The Template Method Pattern (Behavioral) ©SoftMoore ConsultingSlide 1.
The Factory Method Pattern (Creational) ©SoftMoore ConsultingSlide 1.
CSC 480 Software Engineering Design With Patterns.
CS 325: Software Engineering March 19, 2015 Applying Patterns (Part B) Code Smells The Decorator Pattern The Observer Pattern The Template Method Pattern.
Banaras Hindu University. A Course on Software Reuse by Design Patterns and Frameworks.
Object Design More Design Patterns Object Constraint Language Object Design Specifying Interfaces Review Exam 2 CEN 4010 Class 18 – 11/03.
Design Patterns Creational Patterns. Abstract the instantiation process Help make the system independent of how its objects are created, composed and.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
TEMPLATE METHOD DESIGN PATTERN -SWAPNIL SHAH. WHAT IS A DESIGN PATTERN… A design pattern is a general reusable solution to a commonly occurring problem.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Examples (D. Schmidt et al)
Patterns in programming
Design Patterns: MORE Examples
Design Patterns: Brief Examples
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
Introduction to Design Patterns
Software Design and Architecture
Software Design and Architecture
Presented by Igor Ivković
Advanced Programming Behnam Hatami Fall 2017.
Structural Patterns: Adapter and Bridge
Introduction to Design Patterns
CS 325: Software Engineering
Informatics 122 Software Design II
Presented by Igor Ivković
Presentation transcript:

Design Patterns Section 7.1 (JIA’s) Section (till page 259) (JIA’s) Section 7.2.2(JIA’s) Section (JIA’s)

Design Patterns Pattern is a term used to describe architectural designs Recurring Problems in architectural designs (Christopher Alexander, 1977) describe solutions for those problems A pattern is a generic or reusable solution to a recurring problem Can that be applied to software?

Design Patterns Similarities between software and architecture Both are based on creative processes Design must satisfy customer’s needs Design my be feasible to engineer Designers must balance many competing constraints and requirements As a result, software design patterns are descriptions (templates) of solutions to recurring problems in software design Not a finished design!

Design Patterns Why use design patterns? Why reinvent the wheel? Use (reuse) common solutions for common problems Boost confidence in software systems -- use established design patterns that have been proven effective Provide a common vocabulary for software designers to communicate about software design Work started by Gamma et al. Design Patterns [Gamma et al., 1995]

Design Patterns 23 general-purpose design patterns [Gamma et al.] Creational patterns Deal with the process of object creation E.g. Singleton --- One instance of class can exist Structural patterns Deal with the static composition and structure of classes and objects E.g. Adapter --- Convert an interface of a class to a different one expected by other classes Behavioral patterns Deal with dynamic interaction among classes and objects E.g. Template --- Separate variant and invariant behaviors among related classes

Design Patterns Pattern name (Also known as) Problem addressed Detailed description When to apply (list of preconditions) The Solution Class diagram that depicts the participants of the pattern and the relationships among them A list of classes participating in the pattern Does not describe a concrete solution, is a template instead Describe results and tradeoffs

Singleton Design Pattern Pattern name: Singleton Category: Creational design pattern Intent: Ensure that a class has only one instance and provides a global point of access to it Also known as: N/A Applicability: Use the Singleton pattern when there must be exactly one instance of a class and it must be accessible to clients from a well-known access point a GUI application must have a single mouse an active modem needs one and only one telephone line an operating system can only have one window manager or, a PC is connected to a single keyboard Such objects are accessed by disparate objects throughout a software system, and therefore require a global point of access Structure: NEXT SLIDE Participants: Itself

Singleton’s Structure

Singleton Declares the unique instance of the class as a static variable Defines a static method getInstance() for clients to access the unique instance --- global access point Code public class Singleton { private static Singleton theOnlyOne = null; //usually has other instance variables as well private Singleton() { } public static Singleton getInstance() { // global access point if(theOnlyOne == null) { theOnlyOne = new Singleton(); } return theOnlyOne; } }

Singleton public class SingletonInstantiator { public SingletonInstantiator() { Singleton instance = Singleton.getInstance(); Singleton anotherInstance = new Singleton(); !!! } } Test if working properly?

Example Singleton public class SingleRandom { private Random gen; private static SingleRandom theOnlyOne = new SingleRandom(); private SingleRandom() {gen = new Random(); } public void setSeed(int seed) { gen.setSeed(seed); } public int nextInt() { return gen.nextInt(); } public static SingleRandom getInstance() { return theOnlyOne; }

Template Design Pattern One of the most widely used design patterns Useful for separating the variant and the invariant object behavior Invariant means common to all subclasses The invariant behavior is placed in the abstract class (template) Concrete methods common to all subclasses Abstract class contains abstract methods for the variant behavior Any subclasses that inherit it can override the abstract methods and implement the specifics needed in that context In the body of TemplateMethod() --- invariant --- there are calls to operation1() and operation2() … operation1() and operation2() are abstract defined by the subclasses which override them

Template Design Pattern Why abstract classes for templates? Contain behavior that is common to all its subclasses encapsulated in non-abstract methods Might even be defined as final The abstract methods in such a class require that context-specific behavior be implemented for each concrete subclass Called Hook methods

Template Design Pattern A program that sorts data using different sorting algorithms: The AbstractClass would have a method called Sort() -- the invariant behavior Sort uses the abstract methods in the class, which are specified by any class that inherits from it The hook methods in this case might be compare() (compares two objects and returns the one that is "higher") sortPass() (performs one iteration of a particular sorting algorithm) The usual control structure of object calls and relations is reversed! It is the parent class that calls the method in the subclass

Template Design Pattern Pattern Name: Template Method Category: Behavioral design pattern Intent: To define the skeleton of an algorithm deferring some steps to subclasses, thus allowing the subclasses to redefine certain steps of the algorithm This way, subclasses can override parts of the algorithm without changing its overall structure Applicability: The Template Method Pattern should be used to implement the invariant parts of an algorithm once and leave it to the subclasses to implement variant behavior localize the common behavior among subclasses to avoid code duplication

Structure of the Template Design Pattern

####

Template Design Pattern Participants: Generic (or Abstract ) Class Defines the abstract hook methods (variant behavior) that concrete classes override to implement the steps of an algorithm Implements a template method (invariant behavior) that defines the skeleton of an algorithm called by the hook methods Concrete Class Implements the hook methods to carry-out subclass-specific steps of the algorithm defined in the template method Example 7.3 page tter/index.html tter/index.html

Adapter Design Pattern Pattern Name: Adapter Category: Structural design pattern Intent: To convert the contract of a class into another contract that new clients expect The Adapter pattern is used so that two incompatible interfaces can work together The join between them is called an Adapter We convert interface of one class into interface expected by the client A.K.A: Wrapper Applicability: when we need to use an existing class with a “different” interface than provided

Structure of Object Adapter Pattern <<>>

Structure of Object Adapter Pattern Object adapters use delegation to adapt one interface to another The adapter implements the target interface that the client expects to see, while it holds an instance of the adaptee Can extend adaptee instead (limited to a single adaptee ) Object (composition) vs. Class (inheritance) Adapter When the client calls the request() method on its target object (the adapter), the request is translated into the corresponding specific request on the adaptee Adapters enable the client and the adaptee to be completely decoupled from each other Only the adapter knows about both of them

Structure of Class Adapter Pattern Adaptee <<>>

Adapter Design Pattern Participants Client Requires objects conforming to ATarget ATarget interface Defines the interface expected by the Client Adaptee class Defines the “undesired” interface of an existing class Adapter Adapts the interface of Adaptee to ATarget via delegation (composition) or inhertince

Example Object Adapter CLIENT application would like to use a stack interface on DList (i.e. adapt ADAPTEE to TARGET) ADAPTEE /* DoubleLinkedList */ class DList { public void insert (int pos, Object o) {... } public void remove (int pos, Object o) {... } public void insertHead (Object o) {... } public void insertTail (Object o) {... } public Object removeHead () {... } public Object removeTail () {... } public Object getHead () {... } public Object getTail () {... } } TARGET interface Stack { void push (Object); Object pop (); Object peek(); }

Example Object Adapter ADAPTER /* Adapt DList class to Stack interface */ class DListStack implements Stack { private DList _dlist; public DListStack() {_dlist = new DList(); } public void push (Object o) { _dlist.insertHead (o); } public Object pop () {return _dlist.removeHead ();} public Object peek() {return _dlist.getHead ();} }

Example Class Adaptor /* Adapt DList class to Stack interface */ ADAPTER class DListImpStack extends DList implements Stack { public void push (Object o) {insertHead (o); } public Object pop () {return removeHead (); } public Object peek() {return getHead(); } }

Example (Object Adapter) Example page Table (Generic display table) Client Expects entries of type TableEntry ListTableModel TableEntry (row in a table) Target Student (Information on a Student) Adaptee Does not conform to TableEntry StudentEntry2 Adapter Adapts Student to TableEntry

Example

Example (Class Adapter) Table (Generic display table) Client Expects entries of type TableEntry TableEntry (row in a table) Target Student (Information on a Student) Adaptee Does not conform to TableEntry StudentEntry Adapter Adapts Student to TableEntry

Example