https://www.lri.fr/~linaye/gl.html

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

Design Patterns. An introduction

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

Slide 1. Design Patterns. Prof. Mirco Tribastone, Ph.D

Topics in Object-Oriented Design Patterns

SDC Design patterns GoF

Object-Oriented Oriented Programming

Design Patterns. Gunnar Gotshalks A4-1

Software Engineering - I An Introduction to Software Construction Techniques for Industrial Strength Software

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

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

6.3 Patterns. Definition: Design Patterns

SOFTWARE PATTERNS. Joseph Bonello

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

Using Design Patterns in Java Application Development

Introduction and History

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

Tuesday, October 4. Announcements

DESIGN PATTERN - INTERVIEW QUESTIONS

Idioms and Design Patterns. Martin Skogevall IDE, Mälardalen University

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

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

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

2.1 Design Patterns and Architecture (continued)

Information systems modelling UML and service description languages

An Introduction to Patterns

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

What is Design Patterns?

Design Patterns. Definition of a Design Pattern

2.1 Design Patterns and Architecture (continued)

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

Object-Oriented Design

Software Engineering I (02161)

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

Applying the Observer Design Pattern

Applying Design Patterns to accelerate development of reusable, configurable and portable UVCs. Accellera Systems Initiative 1

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

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

Object-Oriented Design

Design Patterns. Observations. Electrical Engineering Patterns. Mechanical Engineering Patterns

3 Product Management Anti-Patterns by Thomas Schranz

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Patterns. Erich Gamma Richard Helm Ralph Johnson John Vlissides

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

An Introduction to Patterns

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

Crash course on design patterns

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

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

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

Introduction to Software Engineering: Object Design I Reuse & Patterns

The GoF Design Patterns Reference

Design patterns. OOD Lecture 6

Outline. Design Patterns. Observer Pattern. Definitions & Classifications

Last Lecture. Lecture 17: Design Patterns (part 2) Kenneth M. Anderson Object-Oriented Analysis and Design CSCI 4448/ Spring Semester, 2005

A Reconnaissance on Design Patterns

CS/CE 2336 Computer Science II

Applying the Decorator Design Pattern

Design patterns. Jef De Smedt Beta VZW

Chapter 12 (revised by JAS)

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

Design patterns. Valentina Presutti courtesy of Paolo Ciancarini

Last Lecture. Lecture 26: Design Patterns (part 2) State. Goals of Lecture. Design Patterns

Reuse at Design Level: Design Patterns

CPSC 310 Software Engineering. Lecture 11. Design Patterns

What is Design Patterns?

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

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

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

Design Patterns. CSC207 Fall 2017

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

Design Pa*erns. + Anima/on Undo/Redo Graphics and Hints

Design Patterns. CSC207 Fall 2017

Lecture 13: Design Patterns

THOMAS LATOZA SWE 621 FALL 2018 DESIGN PATTERNS

An Expert System for Design Patterns Recognition

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

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

MVC. Model-View-Controller. Design Patterns. Certain programs reuse the same basic structure or set of ideas

Software methodology and snake oil

Lecture 20: Design Patterns II

Design Patterns. CSC207 Winter 2017

Software Eningeering. Lecture 9 Design Patterns 2

1 Software Architecture

Brief Note on Design Pattern

Design Patterns Lecture 2

Software Engineering I (02161)

Object-oriented Software Design Patterns

CS560. Lecture: Design Patterns II Includes slides by E. Gamma et al., 1995

Coordination Patterns

Design Patterns Reid Holmes

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

Design Patterns 2. Page 1. Software Requirements and Design CITS 4401 Lecture 10. Proxy Pattern: Motivation. Proxy Pattern.

Design Patterns. CSE870: Advanced Software Engineering (Design Patterns): Cheng

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

What is a Pattern? Lecture 40: Design Patterns. Elements of Design Patterns. What are design patterns?

CSC207H: Software Design Lecture 6

Design Patterns! Acknowledgements!

What is Design Patterns?

CS342: Software Design. November 21, 2017

Transcription:

Software Engineering https://www.lri.fr/~linaye/gl.html lina.ye@centralesupelec.fr Sequence 3, 2017-2018 1/50 Software Engineering

Plan 1 2 3 4 5 2/50 Software Engineering

ground Evolution of Program 3/50 Software Engineering

Definition What is a design pattern 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. [C. Alexander 77] 4/50 Software Engineering

Definition What is a design pattern 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. [C. Alexander 77] The design patterns are descriptions of communicating objects and classes that are customized to solve a general design problem in a particular context.[e. Gamma 97] 4/50 Software Engineering

Definition Essential elements Pattern name: describes a design problem, its solutions, and consequences in a word or two Problem: describes when to apply the pattern Solution: describes the elements that make up the design, their relationship, responsibilities and collaborations Consequences: the results and trade-offs of applying the pattern 5/50 Software Engineering

Sources A Pattern Language: Towns, Buildings, Construction : Christopher Alexander, Sara Ishikawa and Murray Silverstein Design Patterns, Elements of Reusable Object-Oriented Software : Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides wikipedia: design patterns 6/50 Software Engineering

Categories Pattern can be divided into three categories with respect to their purpose. Creational patterns: the process of object creation Structural patterns: the composition of classes or objects Behavioral patterns: the ways in which classes or objects interact and distribute responsibility 7/50 Software Engineering

List Abstract Factory: provide an interface for creating families of related or dependent objects without specifying their concrete classes. Builder: separate the construction of a complex object from its representation. Prototype: specify the kinds of objects to create using a prototypical instance, and create new objects by copying this prototype. Singleton: ensure a class only has one instance, and provide a global point of access to it. 8/50 Software Engineering

Abstract factory Problem: create the group of objects without knowing their exact concrete class Example: program with a GUI Mac and Windows (manager the elements of graphic interface) Solution: the creation of the object is transferred to another specific class 9/50 Software Engineering

Abstract factory: structure 10/50 Software Engineering

User interface toolkit 11/50 Software Engineering

Factory code public abstract class Button { public abstract void paint(); public class WinButton extends Button { public void paint(){... public class MacButton extends Button { public void paint(){... 12/50 Software Engineering

Factory code public abstract class Button { public abstract void paint(); public class WinButton extends Button { public void paint(){... public class MacButton extends Button { public void paint(){... 12/50 Software Engineering

Factory code public abstract class ScrollBar { public abstract void paint(); public class WinScrollBar extends ScrollBar { public void paint(){... public class MacScrollBar extends ScrollBar { public void paint(){... 13/50 Software Engineering

Factory code public abstract class ScrollBar { public abstract void paint(); public class WinScrollBar extends ScrollBar { public void paint(){... public class MacScrollBar extends ScrollBar { public void paint(){... 13/50 Software Engineering

Factory code public abstract class GUIFactory { public Button createbutton(); public ScrollBar createscrollbar(); public class WinFactory extends GUIFactory { public Button createbutton(){ return new WinButton(); public ScrollBar createscrollbar(){ return new WinScrollBar(); public class MacFactory extends GUIFactory {... 14/50 Software Engineering

Factory code public abstract class GUIFactory { public Button createbutton(); public ScrollBar createscrollbar(); public class WinFactory extends GUIFactory { public Button createbutton(){ return new WinButton(); public ScrollBar createscrollbar(){ return new WinScrollBar(); public class MacFactory extends GUIFactory {... 14/50 Software Engineering

Factory code GUIFactory guifactory; Button button; ScrollBar scrollbar; if (ismac()) { guifactory=new MacFactory(); if (iswin()) { guifactory=new WinFactory(); button=guifactory.createbutton(); scrollbar=guifactory.createscrollbar(); 15/50 Software Engineering

Factory code GUIFactory guifactory; Button button; ScrollBar scrollbar; if (ismac()) { guifactory=new MacFactory(); if (iswin()) { guifactory=new WinFactory(); button=guifactory.createbutton(); scrollbar=guifactory.createscrollbar(); 15/50 Software Engineering

Factory code GUIFactory guifactory; Button button; ScrollBar scrollbar; if (ismac()) { guifactory=new MacFactory(); if (iswin()) { guifactory=new WinFactory(); button=guifactory.createbutton(); scrollbar=guifactory.createscrollbar(); 15/50 Software Engineering

Factory code GUIFactory guifactory; Button button; ScrollBar scrollbar; if (ismac()) { guifactory=new MacFactory(); if (iswin()) { guifactory=new WinFactory(); button=guifactory.createbutton(); scrollbar=guifactory.createscrollbar(); 15/50 Software Engineering

Abstract Factory 16/50 Software Engineering

Singleton 17/50 Software Engineering

Singleton Problem: guarantee that one class has only one instance; one unique and global access point to this instance Example: class represents the configuration of a system; class manipulates the window of one application Solution: intercept requests to create new objects; define a static method that returns the instance 18/50 Software Engineering

Singleton code public class Singleton { private static Singleton instance; private Singleton() { public static Singleton getinstance() { if (Singleton.instance==null) { Singleton.instance=new Singleton(); return Singleton.instance; Singleton s1=singleton.getinstance(); Singleton s2=singleton.getinstance(); System.out.println(s1==s2); 19/50 Software Engineering

List Adapter: convert the interface of a class into another interface clients expect. Bridge: decouple an abstraction from its implementation so that the two can vary independently. Composite: compose objects into tree structures to represent part-whole hierarchies. Decorator: attach additional responsibilities to an object dynamically to extend functionality. Facade: provide a unified interface to a set of interfaces in a subsystem. 20/50 Software Engineering

Composite Problem Represent the set of objects by the tree structure Let clients treat individual objects and compositions of objects uniformly. 21/50 Software Engineering

Composite Problem Represent the set of objects by the tree structure Let clients treat individual objects and compositions of objects uniformly. Example A draw application (a line and the set of lines should be treated in the same way) An arithmetic expression 21/50 Software Engineering

Composite Problem Represent the set of objects by the tree structure Let clients treat individual objects and compositions of objects uniformly. Example A draw application (a line and the set of lines should be treated in the same way) An arithmetic expression Solution an abstract class representing an object or a tree a child class representing the leafs a child class representing a node of the tree 21/50 Software Engineering

Composite: example 22/50 Software Engineering

Composite: example 22/50 Software Engineering

Composite: example 22/50 Software Engineering

Composite: structure 23/50 Software Engineering

Composite Pattern objects Component: interface for all objects in this pattern (interface or an abstract class with some methods common to all objects). Leaf: the behaviors for the primitive elements. It is the building block and implements base component. Composite: consists of leaf or composite elements (children) and implements the operations by calling those of its children. 24/50 Software Engineering

Composite code public interface Shape { public void draw (String fillcolor); public class Triangle implements Shape { public void draw(string fillcolor) { System.out.println("Drawing Triangle with color"+fillcolor); public class Circle implements Shape { public void draw(string fillcolor) { System.out.println("Drawing Circle with color"+fillcolor); 25/50 Software Engineering

Composite code public interface Shape { public void draw (String fillcolor); public class Triangle implements Shape { public void draw(string fillcolor) { System.out.println("Drawing Triangle with color"+fillcolor); public class Circle implements Shape { public void draw(string fillcolor) { System.out.println("Drawing Circle with color"+fillcolor); 25/50 Software Engineering

Composite code public class Drawing implements Shape { private List<Shape> shapes = new ArrayList<Shape>(); public void draw (String fillcolor) { for(shape sh : shapes) sh.draw(fillcolor); public void add (Shape s) { this.shapes.add(s); public void remove (Shape s) { shapes.remove(s); public void clear () { shapes.clear(); System.out.println("Clearing all shapes"); 26/50 Software Engineering

Composite code public class Drawing implements Shape { private List<Shape> shapes = new ArrayList<Shape>(); public void draw (String fillcolor) { for(shape sh : shapes) sh.draw(fillcolor); public void add (Shape s) { this.shapes.add(s); public void remove (Shape s) { shapes.remove(s); public void clear () { shapes.clear(); System.out.println("Clearing all shapes"); 26/50 Software Engineering

Composite code Shape tri = new Triangle(); Shape tri1 = new Triangle(); Shape cir = new Circle(); Drawing drawing = new Drawing(); drawing.add(tri); drawing.add(tri1); drawing.add(cir); drawing.draw( Red ); drawing.clear(); drawing.add(tri); drawing.add(cir); drawing.draw( Green ); 27/50 Software Engineering

Composite code Shape tri = new Triangle(); Shape tri1 = new Triangle(); Shape cir = new Circle(); Drawing drawing = new Drawing(); drawing.add(tri); drawing.add(tri1); drawing.add(cir); drawing.draw( Red ); drawing.clear(); drawing.add(tri); drawing.add(cir); drawing.draw( Green ); Results Drawing Triangle with color Red Drawing Triangle with color Red Drawing Circle with color Red Clearing all the shapes from drawing Drawing Triangle with color Green Drawing Circle with color Green 27/50 Software Engineering

Composite code Shape tri = new Triangle(); Shape tri1 = new Triangle(); Shape cir = new Circle(); Drawing drawing = new Drawing(); drawing.add(tri); drawing.add(tri1); drawing.add(cir); drawing.draw( Red ); drawing.clear(); drawing.add(tri); drawing.add(cir); drawing.draw( Green ); Results Drawing Triangle with color Red Drawing Triangle with color Red Drawing Circle with color Red Clearing all the shapes from drawing Drawing Triangle with color Green Drawing Circle with color Green 27/50 Software Engineering

Composite hierarchy 28/50 Software Engineering

Facade Problem unified interface by defining a higher-level interface that makes the subsystem easier to use. 29/50 Software Engineering

Facade Problem unified interface by defining a higher-level interface that makes the subsystem easier to use. Example recuperate old codes that are hard to be refactored several complex interfaces 29/50 Software Engineering

Facade Problem unified interface by defining a higher-level interface that makes the subsystem easier to use. Example recuperate old codes that are hard to be refactored several complex interfaces Solution one high-level class that reuses the useful functionalities 29/50 Software Engineering

Facade: goal 30/50 Software Engineering

Facade: structure 31/50 Software Engineering

Facade code class ComplexeA1{ public void actiona11 () {... public void actiona12 () {... class ComplexeA2{ public void actiona21 () {... public void actiona22 () {... class ComplexeB1 { public void actionb11 () {... public void actionb12 () {... class ComplexeB2 { public void actionb21 () {... public void actionb22 () {... 32/50 Software Engineering

Facade code class Facade { complexea1 a1; complexeb2 b2; Facade(){ a1=new ComplexeA1(); b2=new ComplexeB2(); void performactionsab1 () { a1.actiona11(); b2.actionb22(); void performactionsab2 () { a1.actiona11(); a1.actiona12(); b2.actionb21(); 33/50 Software Engineering

Facade code class Facade { complexea1 a1; complexeb2 b2; Facade(){ a1=new ComplexeA1(); b2=new ComplexeB2(); void performactionsab1 () { a1.actiona11(); b2.actionb22(); void performactionsab2 () { a1.actiona11(); a1.actiona12(); b2.actionb21(); Facade facade=new Facade(); facade.performactionab1(); 33/50 Software Engineering

List Command: an object is used to encapsulate all information needed to perform an action or trigger an event at a later time. Interpreter: define a representation for the grammar of a given language with an interpreter to interpret sentences in this language. Iterator: provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation. 34/50 Software Engineering

List Observer: define a one-to-many dependency between objects so that one object changes state, all its dependents are notified and updated automatically. State: allow an object to alter its behavior when its internal state changes. Strategy: define a family of algorithms such that they are interchangeable. Visitor: separate an algorithm from an object by defining a new operation without changing the classes of the elements on which it operates. 35/50 Software Engineering

Strategy Problem Allow different algorithms for the same object 36/50 Software Engineering

Strategy Problem Allow different algorithms for the same object Example Robots with different behaviors Different sorting algorithms 36/50 Software Engineering

Strategy Problem Allow different algorithms for the same object Example Robots with different behaviors Different sorting algorithms Solution The algorithms are encapsulated by classes. The class using the algorithm will have an instance of this class as attribute. 36/50 Software Engineering

Strategy: structure 37/50 Software Engineering

Strategy code abstract class SortStrategy { abstract void sort (List list); class Quick extends SortStrategy { void sort (List list) {... class Merge extends SortStrategy { void sort (List list) {... 38/50 Software Engineering

Strategy code abstract class SortStrategy { abstract void sort (List list); class Quick extends SortStrategy { void sort (List list) {... class Merge extends SortStrategy { void sort (List list) {... 38/50 Software Engineering

Strategy code class List { private List elements; private SortStrategy ss; public void setsort (SortStrategy s) { this.ss=s; public void sort () { this.ss.sort(this.elements); 39/50 Software Engineering

Strategy code class List { private List elements; private SortStrategy ss; public void setsort (SortStrategy s) { this.ss=s; public void sort () { this.ss.sort(this.elements); 39/50 Software Engineering

Observer Problem Allow following the state modification of an object 40/50 Software Engineering

Observer Problem Allow following the state modification of an object Example Graphic interface depending on the application engine. Model-View-Controller 40/50 Software Engineering

Observer Problem Allow following the state modification of an object Example Graphic interface depending on the application engine. Model-View-Controller Solution The observable is linked to all its observers The observable should notify all its observers when its state is changed Each observer requires the necessary information 40/50 Software Engineering

Observer: example 41/50 Software Engineering

Observer: example 42/50 Software Engineering

Observer: structure 43/50 Software Engineering

Observer code class Subject { private List<Observer> observers; private int state; public void setstate (int val) { state=val; notifyall(); public void attach (Observer ons) { observers.add(obs); public void notifyall () { for (Observer obs: observers) obs.update(); 44/50 Software Engineering

Observer code class Subject { private List<Observer> observers; private int state; public void setstate (int val) { state=val; notifyall(); public void attach (Observer ons) { observers.add(obs); public void notifyall () { for (Observer obs: observers) obs.update(); 44/50 Software Engineering

Observer code abstract class Observer { protected Subject subject; public abstract void update(); class ConcreteObserver { public ConcreteObserver(Subject sub) { this.subject=sub; this.subject.attach(this); public void update () { System.out.println("new value: " +subject.getvalue()); 45/50 Software Engineering

Observer code abstract class Observer { protected Subject subject; public abstract void update(); class ConcreteObserver { public ConcreteObserver(Subject sub) { this.subject=sub; this.subject.attach(this); public void update () { System.out.println("new value: " +subject.getvalue()); 45/50 Software Engineering

MVC Pattern used to implement an user s interface Widely adopted in web applications 46/50 Software Engineering

MVC Pattern used to implement an user s interface Widely adopted in web applications Separate the elements of an application into three groups View: output representation of information that allows the interaction with users Model: central component that expresses the application s behaviors, independent of the user interface Controller: accepts input and converts it to commands for the model or view 46/50 Software Engineering

MVC Pattern used to implement an user s interface Widely adopted in web applications Separate the elements of an application into three groups View: output representation of information that allows the interaction with users Model: central component that expresses the application s behaviors, independent of the user interface Controller: accepts input and converts it to commands for the model or view Simultaneous development with separate parts Ease of modification Multiple views or controls for a model 46/50 Software Engineering

MVC 47/50 Software Engineering

MVC in Java Packages are used to represent model/view/controller The elements in each group are represented by classes 48/50 Software Engineering

Example Problem An application to draw geometrical forms with the library SWING 49/50 Software Engineering

Example Problem An application to draw geometrical forms with the library SWING One possible solution a view package containing one window class one draw surface class one class to manage mouse events 49/50 Software Engineering

Example Problem An application to draw geometrical forms with the library SWING One possible solution a view package containing one window class one draw surface class one class to manage mouse events a model package containing a form class with its sub-classes like polygon, circle, etc. an arrangement class (contains an array of forms)... 49/50 Software Engineering

Example Problem An application to draw geometrical forms with the library SWING One possible solution a view package containing one window class one draw surface class one class to manage mouse events a model package containing a form class with its sub-classes like polygon, circle, etc. an arrangement class (contains an array of forms)... a controller package containing a controller class 49/50 Software Engineering

Simple and elegant solutions to specific problems Useful for designing reusable object-oriented software Several key steps: 1 factor pertinent objects into classes at the right granularity 2 define class interfaces and inheritance hierarchies 3 establish key relationships among them 50/50 Software Engineering