Software Design Patterns. Aliaksei Syrel

Similar documents
SDC Design patterns GoF

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

Creational Design Patterns

TDDB84. Lecture 2. fredag 6 september 13

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

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

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

What is Design Patterns?

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

DESIGN PATTERN - INTERVIEW QUESTIONS

UNIT I Introduction to Design Patterns

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

Design Pattern and Software Architecture: IV. Design Pattern

Design Patterns. SE3A04 Tutorial. Jason Jaskolka

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

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

Introduction to Software Engineering: Object Design I Reuse & Patterns

Object-oriented Software Design Patterns

TDDB84: Lecture 6. Adapter, Bridge, Observer, Chain of Responsibility, Memento, Command. fredag 4 oktober 13

Design Patterns: Structural and Behavioural

CSCI Object Oriented Design: Frameworks and Design Patterns George Blankenship. Frameworks and Design George Blankenship 1

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

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

A few important patterns and their connections

Plan. A few important patterns and their connections. Singleton. Singleton: class diagram. Singleton Factory method Facade

Design Patterns. An introduction

An Introduction to Patterns

TDDB84: Lecture 09. SOLID, Language design, Summary. fredag 11 oktober 13

Tuesday, October 4. Announcements

C++ for System Developers with Design Pattern

Information systems modeling. Tomasz Kubik

Software Eningeering. Lecture 9 Design Patterns 2

Topics. Software Process. Agile. Requirements. Basic Design. Modular Design. Design Patterns. Testing. Quality. Refactoring.

Design Patterns Lecture 2

Design Patterns. (and anti-patterns)

What is Design Patterns?

Applying Design Patterns to SCA Implementations

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

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

UNIT I Introduction to Design Patterns

Object-Oriented Oriented Programming

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

CSC207H: Software Design Lecture 6

Object oriented programming. Encapsulation. Polymorphism. Inheritance OOP

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

The GoF Design Patterns Reference

COSC 3351 Software Design. Design Patterns Behavioral Patterns (I)

CSE 219 COMPUTER SCIENCE III STRUCTURAL DESIGN PATTERNS SLIDES COURTESY: RICHARD MCKENNA, STONY BROOK UNIVERSITY.

Pro Objective-C Design Patterns for ios

The Design Patterns Matrix From Analysis to Implementation

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

Software Reengineering Refactoring To Patterns. Martin Pinzger Delft University of Technology

Cloning Enums. Cloning and Enums BIU OOP

Design Patterns Reid Holmes

Material and some slide content from: - GoF Design Patterns Book. Design Patterns #1. Reid Holmes. Lecture 11 - Tuesday October

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

INSTITUTE OF AERONAUTICAL ENGINEERING

UP Requirements. Software Design - Dr Eitan Hadar (c) Activities of greater emphasis in this book. UP Workflows. Business Modeling.

Overview CS Kinds of Patterns. Design Pattern. Factory Pattern Rationale. Kinds of Factory Patterns

Design patterns. Jef De Smedt Beta VZW

DESIGN PATTERNS MOCK TEST DESIGN PATTERNS MOCK TEST II

Using Design Patterns in Java Application Development

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

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

Software Quality Management

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

Ownership in Design Patterns. Master's Thesis Final Presentation Stefan Nägeli

CS 2720 Practical Software Development University of Lethbridge. Design Patterns

What is Design Patterns?

CS/CE 2336 Computer Science II

OODP Session 4. Web Page: Visiting Hours: Tuesday 17:00 to 19:00

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

Advanced C++ Programming Workshop (With C++11, C++14, C++17) & Design Patterns

A Primer on Design Patterns

CSE 219 COMPUTER SCIENCE III BEHAVIORAL DESIGN PATTERNS SLIDES COURTESY: RICHARD MCKENNA, STONY BROOK UNIVERSITY

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

CHAPTER 6: CREATIONAL DESIGN PATTERNS

Design Patterns. Lecture 10: OOP, autumn 2003

What are patterns? Design Patterns. Design patterns. Creational patterns. The factory pattern. Factory pattern structure. Lecture 10: OOP, autumn 2003

Design Patterns. "Gang of Four"* Design Patterns. "Gang of Four" Design Patterns. Design Pattern. CS 247: Software Engineering Principles

CS 247: Software Engineering Principles. Design Patterns

Abstract Classes and Interfaces

Software Development Project. Kazi Masudul Alam

Applying Some Gang of Four Design Patterns CSSE 574: Session 5, Part 3

CSSE 374: More Object Design with Gang of Four Design Patterns

Keywords: Abstract Factory, Singleton, Factory Method, Prototype, Builder, Composite, Flyweight, Decorator.

Top Down Design vs. Modularization

Lecture 20: Design Patterns II

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

2018/2/5 话费券企业客户接入文档 语雀

R07. IV B.Tech. II Semester Supplementary Examinations, July, 2011

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

I, J. Key-value observing (KVO), Label component, 32 text property, 39

Lecture 4: Observer Pattern, Event Library and Componentization

TDDB84: Lecture 5. Singleton, Builder, Proxy, Mediator. fredag 27 september 13

Design Patterns #3. Reid Holmes. Material and some slide content from: - GoF Design Patterns Book - Head First Design Patterns

Design Patterns! Acknowledgements!

administrivia today UML start design patterns Tuesday, September 28, 2010

COSC 3351 Software Design. Design Patterns Structural Patterns (I)

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

Transcription:

Software Design Patterns Aliaksei Syrel 1

Pattern types Creational Patterns Behavioural Patterns Structural Patterns 2

Creational Patterns Creational design patterns deal with object creation mechanisms, trying to create objects in a manner suitable to the situation.

Pattern types Abstract Factory Creational Patterns Behavioural Patterns Singleton Factory Method Structural Patterns Prototype Builder 4

Pattern types Abstract Factory Creational Patterns Behavioural Patterns Singleton Factory Method Structural Patterns Prototype Builder 5

The abstract factory pattern provides a way to encapsulate a group of individual factories with a common theme without specifying their concrete classes 6

If you want to create cars of different models from the same brand 7

you need Mercedes Factory 8

If you want another brand with different models 9

You need additional Audi Factory 10

Abstract Factory Two factories have the same available public API for: Creating a new car Delivering it to customer Developing new models some other Mercedes Factory 11 Audi Factory

Abstract Factory API can be extracted to an Interface CarFactory <<Interface>> Mercedes Factory 12 Audi Factory

Abstract Factory 13

Crossplatform GUI library for native widgets Windows OSX Android Button Checkbox

public interface Button { public class WindowsButton implements Button { public class OsxButton implements Button { public class AndroidButton implements Button {

public interface Checkbox { public class WindowsCheckbox implements Checkbox { public class OsxCheckbox implements Checkbox { public class AndroidCheckbox implements Checkbox {

Button - WindowsButton - OsxButton - AndroidButton Checkbox - WindowsCheckbox - OsxCheckbox - AndroidCheckbox

public interface WidgetFactory { public Button createbutton(); public Checkbox createcheckbox();

public interface WidgetFactory { public Button createbutton(); public Checkbox createcheckbox(); public class WindowsWidgetFactory implements WidgetFactory { @Override public Button createbutton() { return new WindowsButton(); @Override public Checkbox createcheckbox() { return new WindowsCheckbox();

public interface WidgetFactory { public Button createbutton(); public Checkbox createcheckbox(); public class OsxWidgetFactory implements WidgetFactory { @Override public Button createbutton() { return new OsxButton(); @Override public Checkbox createcheckbox() { return new OsxCheckbox();

public interface WidgetFactory { public Button createbutton(); public Checkbox createcheckbox(); public class AndroidWidgetFactory implements WidgetFactory { @Override public Button createbutton() { return new AndroidButton(); @Override public Checkbox createcheckbox() { return new AndroidCheckbox();

Button - WindowsButton - OsxButton - AndroidButton Checkbox - WindowsCheckbox - OsxCheckbox - AndroidCheckbox WidgetFactory - WindowsWidgetFactory - OsxWidgetFactory - AndroidWidgetFactory

WidgetFactory widgetfactory;

WidgetFactory widgetfactory; // pseudocode // switch(system.getproperty("os.name")) { case "Windows": widgetfactory = new WindowsWidgetFactory(); break;

WidgetFactory widgetfactory; // pseudocode // switch(system.getproperty("os.name")) { case "Windows": widgetfactory = new WindowsWidgetFactory(); break; case "OSX": widgetfactory = new OsxWidgetFactory(); break; case "Android": widgetfactory = new AndroidWidgetFactory(); break; default: widgetfactory = null; throw new Exception("Unsupported OS");

WidgetFactory widgetfactory; // pseudocode // switch(system.getproperty("os.name")) { //... // Button button = widgetfactory.createbutton(); Checkbox checkbox = widgetfactory.createcheckbox();

WidgetFactory widgetfactory; // pseudocode // switch(system.getproperty("os.name")) { case "Windows": widgetfactory = new WindowsWidgetFactory(); break; case "OSX": widgetfactory = new OsxWidgetFactory(); break; case "Android": widgetfactory = new AndroidWidgetFactory(); break; default: widgetfactory = null; throw new Exception("Unsupported OS"); Button button = widgetfactory.createbutton(); Checkbox checkbox = widgetfactory.createcheckbox();

Pattern types Abstract Factory Creational Patterns Behavioural Patterns Singleton Factory Method Structural Patterns Prototype Builder 28

public class Game { private final String name; private final Player player; private final Level level; private final Board board; private final Renderer renderer; public Game(String name, Player player, Level level, Board board, Renderer renderer) { this.name = name; this.player = player; this.level = level; this.board = board; this.renderer = renderer; public Game(String name, Player player, Level level, Board board) { this(name, player, level, board, new Renderer()); public Game(String name, Player player, Level level) { this(name, player, level, new Board()); public Game(String name, Player player) { this(name, player, new Level()); public Game(String name) { this(name, new Player()); public Game() { this("default game"); 29

public class Game { private final String name; private final Player player; private final Level level; private final Board board; private final Renderer renderer; public Game(String name, Player player, Level level, Board board, Renderer renderer) { this.name = name; this.player = player; this.level = level; this.board = board; this.renderer = renderer; public Game(String name, Player player, Level level, Board board) { this(name, player, level, board, new Renderer()); public Game(String name, Player player, Level level) { this(name, player, level, new Board()); public Game(String name, Player player) { this(name, player, new Level()); public Game(String name) { this(name, new Player()); public Game() { this("default game"); 30

The telescoping constructor anti-pattern occurs when the increase of object constructor parameter combinations leads to an exponential list of constructors 31

The intent of the Builder design pattern is to separate the construction of a complex object from its representation 32

public class Game { private final Player player; private final Level level; public Game(Player player, Level level) { this.player = player; this.level = level; 33

Static builder class public class Game { private final Player player; private final Level level; public Game(Player player, Level level) { this.player = player; this.level = level; public static Builder builder() { return new Builder(); public static class Builder { 34

Static builder class public class Game { private final Player player; private final Level level; public Game(Player player, Level level) { this.player = player; this.level = level; public static class Builder { private Player player; private Level level; public Game build() { return new Game(player, level); 35

Static builder class public class Game { private final Player player; private final Level level; public Game(Player player, Level level) { this.player = player; this.level = level; public static class Builder { private Player player; private Level level; public Builder setplayer(player player) { this.player = player; return this; public Builder setlevel(level level) { this.level = level; return this; public Game build() { return new Game(player, level); 36

Usage: public static void main(string[] args) { Game game = Game.builder().setLevel(new Level()).setPlayer(new Player()).build(); 37

Static builder class public class Game { private final Player player; private final Level level; public Game(Player player, Level level) { this.player = player; this.level = level; public static class Builder { private Player player; private Level level; Duplication public Builder setplayer(player player) { this.player = player; return this; public Builder setlevel(level level) { this.level = level; return this; public Game build() { return new Game(player, level); 38

Inner builder class public class Game { private final Player player; private final Level level; private Game() { 39

Inner builder class public class Game { private Player player; private Level level; private Game() { public static Builder builder() { return new Game().new Builder(); public class Builder { 40

Inner builder class public class Game { private Player player; private Level level; private Game() { public static Builder builder() { return new Game().new Builder(); public class Builder { private Builder() { public Builder setplayer(player player) { Game.this.player = player; return this; public Builder setlevel(level level) { Game.this.level = level; return this; public Game build() { return Game.this; 41

Inner builder class public class Game { private Player player; private Level level; private Game() { public static Builder builder() { return new Game().new Builder(); public class Builder { private Builder() { public Builder setplayer(player player) { Game.this.player = player; return this; Does not create new object public Builder setlevel(level level) { Game.this.level = level; return this; on each build() call public Game build() { return Game.this; 42

Inner builder class + Cloneable public class Game implements Cloneable { private Game() { public Game clone() { Game game; try { game = (Game) super.clone(); // clone mutable instance fields if needed catch (CloneNotSupportedException e) { e.printstacktrace(); throw new RuntimeException(); return game; 43

Inner builder class + Cloneable Before public Game build() { return Game.this; After public Game build() { return Game.this.clone(); 44

Usage: public static void main(string[] args) { Game game = Game.builder().setLevel(new Level()).setPlayer(new Player()).build(); vs. public static void main(string[] args) { Game game = new Game(new Player(), new Level()); vs. public static void main(string[] args) { Game game = new Game(); game.setplayer(new Player()); game.setlevel(new Level()); 45

Pattern types Chain of responsibility Command Interpreter Creational Patterns Behavioural Patterns Structural Patterns Iterator Mediator Momento Observer State Strategy Template Method Visitor 46

Pattern types Chain of responsibility Command Interpreter Creational Patterns Behavioural Patterns Structural Patterns Iterator Mediator Momento Observer State Strategy Template Method Visitor 47

Chain of responsibility The chain-of-responsibility is a design pattern consisting of a source of command objects and a series of processing objects. Each processing object contains logic that defines the types of command objects that it can handle; the rest are passed to the next processing object in the chain. A mechanism also exists for adding new processing objects to the end of this chain 48

Chain of responsibility The idea is to process the message by yourself or to redirect it to someone else. 49

Chain of responsibility You need to repair a car 50

Chain of responsibility Can I repair it? Please, repair my car 51

Chain of responsibility Can I repair it? Can I repair it? No Please, repair my car 52

Chain of responsibility Can I repair it? Can I repair it? No Not everything Can I repair it? Please, repair my car 53

Chain of responsibility Can I repair it? Can I repair it? No Not everything Can I repair it? Please, repair my car Almost Can I repair it? 54

Chain of responsibility Can I repair it? Can I repair it? No Not everything Can I repair it? Please, repair my car Almost Can I repair it? Done 55

Chain of responsibility 56

Pattern types Chain of responsibility Command Interpreter Creational Patterns Behavioural Patterns Structural Patterns Iterator Mediator Momento Observer State Strategy Template Method Visitor 57

The visitor pattern provides an ability to add new operations to existing object structures without modifying those structures 58

Visitor Help Darth Vader to check the dislocation of his forces. help! 59

Visitor!!! 1. Death Star accepts Darth Vader. 2. Darth Vader visits Death Star. accept visit 60

Visitor Troopers on Death Star suggest Darth Vader what to visit next: Star Destroyer. accept accept visit 61 visit

Visitor In the end he visits troopers. accept visit accept accept visit 62 visit

Visitor 63

Pattern types Adapter Bridge Creational Patterns Behavioural Patterns Structural Patterns Composite Decorator Facade Flyweight Proxy 64

Pattern types Adapter Bridge Creational Patterns Behavioural Patterns Structural Patterns Composite Decorator Facade Flyweight Proxy 65

The composite pattern lets a client to treat a group or a single instance uniformly. (to have the same interface) 66

Composite Darth Vader wants to control one trooper or a group of troopers in the same way Fight! (don t miss, please) 67

Composite or even groups of groups of troopers 68

Composite Darth Vader doesn t care how many troopers to control - one or many Composite! 69

public interface StormUnit { public void fight(); 70

public interface StormUnit { public void fight(); public class Stormtrooper implements StormUnit { 71

public interface StormUnit { public void fight(); public class Stormtrooper implements StormUnit { @Override public void fight() { System.out.println("Yes, sir!"); 72

public class Stormgroup implements StormUnit { private ArrayList<StormUnit> stormunits = new ArrayList<>(); 73

public class Stormgroup implements StormUnit { private ArrayList<StormUnit> stormunits = new ArrayList<>(); @Override public void fight() { System.out.println("Group is ready, sir!"); for (StormUnit stormunit : stormunits) { stormunit.fight(); 74

public class Stormgroup implements StormUnit { private ArrayList<StormUnit> stormunits = new ArrayList<>(); @Override public void fight() { System.out.println("Group is ready, sir!"); for (StormUnit stormunit : stormunits) { stormunit.fight(); public void addstormunit(stormunit astormunit) { stormunits.add(astormunit); public void removestormunit(stormunit astormunit) { stormunits.remove(astormunit); public void getstormunit(int index) { stormunits.get(index); 75

Composite 76

UI Components (Checkbox) Material Design Light for Web (getmdl.io)

UI Components (Checkbox) Material Design Light for Web (getmdl.io) <label for="chkbox1"> <input type="checkbox" id="chkbox1"> <span>checkbox</span> </label>

UI Components (Toggle) Bloc for Pharo (pharo.org)

UI Components (Checkbox) Bloc for Pharo (pharo.org)

The End. 81