Introduction with a few design patterns

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

 Recent researches show that predicative programming can be used to specify OO concepts including classes, objects, interfaces, methods, single and multiple.
Observer Method 1. References Gamma Erich, Helm Richard, “Design Patterns: Elements of Reusable Object- Oriented Software” 2.
18-1 Verifying Object Behavior and Collaboration Role playing – the act of simulating object behavior and collaboration by acting out an object’s behaviors.
CSE3308/CSC Software Engineering: Analysis and DesignLecture 5B.1 Software Engineering: Analysis and Design - CSE3308 Patterns CSE3308/CSC3080/DMS/2000/12.
DESIGN PATTERNS OZGUR RAHMI DONMEZ.
Observer Pattern Fall 2005 OOPD John Anthony. What is a Pattern? “Each pattern describes a problem which occurs over and over again in our environment,
(c) 2009 University of California, Irvine – André van der Hoek1June 13, 2015 – 21:42:16 Informatics 122 Software Design II Lecture 8 André van der Hoek.
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.
Adapters Presented By Zachary Dea. Definition A pattern found in class diagrams in which you are able to reuse an ‘adaptee’ class by providing a class,
(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.
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.
1 An introduction to design patterns Based on material produced by John Vlissides and Douglas C. Schmidt.
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.
CSE 332: Design Patterns (Part I) Introduction to Design Patterns Design patterns were mentioned several times so far –And the Singleton Pattern was discussed.
An Introduction to Design Patterns. Introduction Promote reuse. Use the experiences of software developers. A shared library/lingo used by developers.
CSSE 374: 3½ Gang of Four Design Patterns These slides derived from Steve Chenoweth, Shawn Bohner, Curt Clifton, and others involved in delivering 374.
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.
Design Patterns in Java Chapter 1 Introduction Summary prepared by Kirk Scott 1.
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.
CSE 403 Lecture 14 Design Patterns. Today’s educational objective Understand the basics of design patterns Be able to distinguish them from design approaches.
Design Patterns CS 124 Reference: Gamma et al (“Gang-of-4”), Design Patterns.
ECE450S – Software Engineering II
Design Patterns CSIS 3701: Advanced Object Oriented Programming.
1 Design Patterns Object-Oriented Design. 2 Design Patterns 4Reuse of design knowledge and experience 4Common in many engineering disciplines 4Avoids.
Behavioral Patterns CSE301 University of Sunderland Harry R Erwin, PhD.
Design Patterns. 1 Paradigm4 Concepts 9 Principles23 Patterns.
Design Patterns Introduction
BEHAVIORAL PATTERNS 13-Sep-2012 Presenters Sanjeeb Kumar Nanda & Shankar Gogada.
CSE 332: Design Patterns (Part II) Last Time: Part I, Familiar Design Patterns We’ve looked at patterns related to course material –Singleton: share a.
CLASSIFICATION OF DESIGN PATTERNS Hladchuk Maksym.
Design Patterns CSCE 315 – Programming Studio Spring 2013.
Design Patterns: MORE Examples
The Object-Oriented Thought Process Chapter 15
Chapter 10 Design Patterns.
Chapter 5:Design Patterns
Software Design Patterns
MPCS – Advanced java Programming
Introduction to Design Patterns
Design Patterns Lecture part 2.
Introduction to Design Patterns
How to be a Good Developer
Design Patterns Introduction
Observer Design Pattern
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.
Presented by Igor Ivković
Design Patterns - A few examples
Advanced Programming Behnam Hatami Fall 2017.
Software Engineering Lecture 7 - Design Patterns
Web Programming Language
Introduction to Design Patterns Part 1
Informatics 122 Software Design II
CSE 403 Software Design.
DESIGNING YOUR SYSTEM.
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2005 Instructor: Patrice Chalin.
DESIGN PATTERNS : Introduction
SOEN 343 Software Design Computer Science and Software Engineering Department Concordia University Fall 2004 Instructor: Patrice Chalin.
Informatics 122 Software Design II
Presented by Igor Ivković
Software Design Lecture : 27.
Adapter Pattern Jim Fawcett
Adapter Pattern Jim Fawcett
Presentation transcript:

Introduction with a few design patterns Copyright © 2016 Curt Hill

Introduction Design is a hard thing It takes knowledge, experience and sometimes luck Designing a large program is no different This presentation introduces the notion of design patterns Copyright © 2016 Curt Hill

An Example I have a characteristic way to build a fstream file reading loop in C++ initialize file while(true) { read an item if(!thefile) break; Process item read } Copyright © 2016 Curt Hill

Commentary This is not the only way to do this but it is an easy pattern to use This a pattern but there are many variations to this pattern This is not a piece of code that I can code even as a template class There are too many possibilities for the thing to be read and the processing to be done Yet the pattern makes is easier and less complex Copyright © 2016 Curt Hill

Another Pattern You may have also seen a pattern involving container classes Three classes One encapsulates the object and has its own pointer type One is main class that has all the public methods One is an iterator class that must have two way communication with main class This may be used with lists, trees and other pointer structures of various forms Copyright © 2016 Curt Hill

List Example class LinkedNode{ friend class LinkedList; friend class LinkedIterator; int key; wxString data; LinkedNode * next; }; class LinkedList{ LinkedNode * root; … }; class LinkedIterator{ LinkedNode * current; LinkedList * thelist; void changed(); Copyright © 2016 Curt Hill

Similarly In the design process we want to do a similar thing When we recognize a situation that may suggest a series of classes that have handled similar situations We may not use any code we have used before, but we reuse the roles we have used before We use a pattern that we are confident will work We try to avoid inventing a new way Copyright © 2016 Curt Hill

References Design Patterns came into popularity after publication of a book of the same name Authors: Gamma, Helm, Johnson, Vlissides AKA Gang of Four The concepts were used prior to this, but the book applies a name and creates a catalog of patterns Copyright © 2016 Curt Hill

Christopher Alexander An architect who said this: Each pattern describes a problem which occurs over and over again in our environment, and then describes the core of the solution to that problem, in such a way that you can use this solution a million times over, without ever doing it the same way twice. His environment is not ours but the statement still holds Copyright © 2016 Curt Hill

More Description A pattern is a stylized description of good design practice, which has been tried and tested in different environments A pattern should have at least four distinct pieces Pattern name Problem it describes Solution it proposes Results and trade-offs of using this Copyright © 2016 Curt Hill

Elements Revisited The pattern name is a few words It allows us to discuss options with others It is possible that a pattern has an alias, but standard naming is helpful The problem gives the context for application of the the pattern Might contain a list of conditions that should be present to apply Copyright © 2016 Curt Hill

Elements Revisited The solution gives the pieces that make up the designs An abstract description of what is needed, rather than a specific existing set of classes Usually classes and interfaces Trade-offs Required for evaluating design choices Often are about space and time trade-off, but others are also possible Copyright © 2016 Curt Hill

Examples We will next look at a few example patterns First we will consider the Adapter Next we will consider the MVC, which is also discussed in the Sommerville text Copyright © 2016 Curt Hill

Adapter Purpose of the adapter pattern is to convert the interface of one class into that which another class expects This allows two classes that are different to work together Wrapper is an alias of Adapter Copyright © 2016 Curt Hill

Adapter Context 1 Suppose we are constructing a drawing editor We create an abstract base class Shape and derive from it the shapes that we are interested in The base class demands that we have a Draw method so that the descendent classes can draw themselves Most of the descendent classes are normal shapes like Square and Circle which are easy to implement Copyright © 2016 Curt Hill

Adapter Context 2 So far so good Next we want to implement a shape that displays a box of text We already have a TextView object but it does most of what we need but does not conform to the requirements of Shape What we do is create an Adapter class to fit the TextView into the Shape hierarchy Copyright © 2016 Curt Hill

TextView Adapter We have two possibilities A class adapter An object adapter The class adapter uses multiple inheritance from both Shape and TextView to make a TextShape The object adapter makes a derivation of Shape, called TextShape, that includes a TextView object as a property Copyright © 2016 Curt Hill

Participant Classes Target – this is the Shape class Client – the editor that uses Shapes Adaptee – the TextView class Adapter – the TextShape Copyright © 2016 Curt Hill

Commentary Regardless of whether this is a class adapter or an object adapter, we implement the required Shape methods by calling TextView methods or combination of methods Next we look at the class diagrams for both the multiple inheritance approach and the composition approach Copyright © 2016 Curt Hill

Multiple Inheritance Client Target Adaptee Adaptor Request() XRequest() Adaptor Adapts Request form to XRequest form Request() Copyright © 2016 Curt Hill

Composition Client Target Adaptor Adaptee Adaptee->XRequest Copyright © 2016 Curt Hill

STL Example You should have seen examples of this already In the Standard Template Library the Stack is an adapter It implements methods Push and Pop (needed by Stack) and converts them into container class methods push_back and pop_back (provided by a vector or list) It also connects empty method with size() == 0 Copyright © 2016 Curt Hill

Java Integer Class Java has several wrapper classes Including Integer and Double These classes take a primitive with no methods and makes it into a class that has the methods that every class should have The Object class, which is the root of the single inheritance tree provides (among others) the following methods: equals toString Copyright © 2016 Curt Hill

Java Wrappers Again Most of Java’s container classes, such as lists, trees, vectors, take contained classes of type Object Thus they use polymorphism instead of templates to accomplish what they need Templates did not appear until Java 5 They cannot use primitives, but can use wrappers of primitives Copyright © 2016 Curt Hill

MVC Model View Controller Separates presentation and interaction from the system data Used when there are multiple ways to view and interact with data Often used when the future requirements for interaction and presentation of data are unknown Copyright © 2016 Curt Hill

MVC Three components that interact with each other The Model manages the system data and associated operations on that data The View defines and manages how the data is presented to the user The Controller manages user interaction and passes these interactions to the View and the Model. Copyright © 2016 Curt Hill

MVC Picture Controller View Select view Chooses the view. Sends user changes to model Displays model. Sends user actions to controller Requests updates User action Notify change State change based on user action Request state Model Maintains internal state of the data Notifies view of state changes Copyright © 2016 Curt Hill

Pros and Cons Data is independent of its representation Presentation of the same data in different ways Changes made in one representation shown in all of them If the data and interactions are simple it can make the code more complex Copyright © 2016 Curt Hill

Commentary You see the need for this in many applications Most of the office suite This is a very common form in SmallTalk It is generalized into the Observer design pattern Copyright © 2016 Curt Hill

Observer Define a one-to-many relationship between objects One subject with many observers When the one object changes state all of the others are notified This is also known as Publish-Subscribe or Dependents As we saw in MVC, very common in the display and editing of data Copyright © 2016 Curt Hill

The Subject Subject – only one Observer – may be many Maintains the central data structure Responds to requests to change Maintains a list of objects that need to be notified when something changes Observer – may be many Requests notification of state changes May also request that a change be made Copyright © 2016 Curt Hill

When to use Data has two or more aspects, where all are dependent on one Changing one forces a change in another Notifications of change need to be made, but there is no assumption on the behavior of the object to be notified Loose coupling of the objects Copyright © 2016 Curt Hill

Consider a word processor The subject keeps the internal form in which the text resides Observers would include All possible views of this data An Undo/Redo processor Backup processor Print processor Copyright © 2016 Curt Hill

Pros and Cons Notification may be easily broadcast to many observers All of the observers are ignorant of how many other observers there may be Thus an update may cascade into lots of work The observers all comply with a simple interface They may be widely varying in function and the observer does not know or care Copyright © 2016 Curt Hill

Next Next is a class diagram It has an abstract Subject and Observer Derived or concrete versions of both Copyright © 2016 Curt Hill

Observer Structure Subject Observer Attach(Observer) Detach(Observer) Notify() Update() for all o in observers o->Update() ConcreteObserver ConcreteObserver ConcreteSubject observerState observerState subjectState … Update () Update () GetState() SetState() Copyright © 2016 Curt Hill

Design Patterns Book lists 23 patterns of three types Creational Abstract factory, Builder, Factory method, Prototype, Singleton Structural Adaptor, Bridge, Composite, Decorator, Façade, Flyweight, Proxy Behavioral Chain of responsibility, Command, Interpreter Iterator, Mediator, Mementor, Observer, State, Strategy, Template Method, Visitor Copyright © 2016 Curt Hill

Other Books Sommerville includes design patterns from differing levels of abstraction Layered, Client Server, Pipe and filter McConnell’s Code Complete consider some of the same patterns as Design Patterns and mention others as well Copyright © 2016 Curt Hill

Finally The dragon that needs slaying is complexity It exists at design and coding times Design patterns give us tried and true solutions to those problems that commonly come up This makes for simpler solutions More likely to work as well We may have to deal with these further Copyright © 2016 Curt Hill