software design patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-design-patte… ·...

83
Software Design Patterns 1 Aliaksei Syrel

Upload: others

Post on 21-Jul-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Software Design Patterns

1

Aliaksei Syrel

Page 2: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Pattern types

Creational Patterns

Behavioural Patterns

Structural Patterns

2

Page 3: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Creational design patterns deal with object creation mechanisms, trying to create objects

in a manner suitable to the situation.

Creational Patterns

Page 4: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Behavioral design patterns identify and realise common communication patterns among

objects. By doing so, these patterns increase flexibility in carrying out this communication.

Behavioural Patterns

Page 5: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Structural design patterns ease the design by identifying a simple way to realise

relationships among entities.

Structural Patterns

Page 6: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Pattern types

Creational Patterns

Behavioural Patterns

Structural Patterns

Abstract Factory

Builder

Factory Method

Prototype

Singleton

6

Page 7: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Pattern types

Creational Patterns

Behavioural Patterns

Structural Patterns

Abstract Factory

Builder

Factory Method

Prototype

Singleton

7

Page 8: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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

8

Page 9: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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

9

Page 10: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

you need Mercedes Factory

10

Page 11: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

If you want another brand with different models

11

Page 12: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

You need additional Audi Factory

12

Page 13: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Mercedes Factory Audi Factory

Two factories have the same available public API for:Creating a new carDelivering it to customerDeveloping new modelssome other…

Abstract Factory

13

Page 14: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Mercedes Factory Audi Factory

API can be extracted to an Interface

CarFactory <<Interface>>

Abstract Factory

14

Page 15: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Abstract Factory

15

Page 16: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Crossplatform GUI library for native widgets

Windows OSX Android

Button

Checkbox

Page 17: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public interface Button {

}

public class WindowsButton implements Button {

}

public class OsxButton implements Button {

}

public class AndroidButton implements Button {

}

Page 18: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public interface Checkbox {

}

public class WindowsCheckbox implements Checkbox {

}

public class OsxCheckbox implements Checkbox {

}

public class AndroidCheckbox implements Checkbox {

}

Page 19: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Button - WindowsButton - OsxButton - AndroidButton

Checkbox - WindowsCheckbox - OsxCheckbox - AndroidCheckbox

Page 20: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public interface WidgetFactory {public Button createButton();public Checkbox createCheckbox();

}

Page 21: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public interface WidgetFactory {public Button createButton();public Checkbox createCheckbox();

}

public class WindowsWidgetFactory implements WidgetFactory {@Overridepublic Button createButton() {

return new WindowsButton();}

@Overridepublic Checkbox createCheckbox() {

return new WindowsCheckbox();}

}

Page 22: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public interface WidgetFactory {public Button createButton();public Checkbox createCheckbox();

}

public class OsxWidgetFactory implements WidgetFactory {@Overridepublic Button createButton() {

return new OsxButton();}

@Overridepublic Checkbox createCheckbox() {

return new OsxCheckbox();}

}

Page 23: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public class AndroidWidgetFactory implements WidgetFactory {@Overridepublic Button createButton() {

return new AndroidButton();}

@Overridepublic Checkbox createCheckbox() {

return new AndroidCheckbox();}

}

public interface WidgetFactory {public Button createButton();public Checkbox createCheckbox();

}

Page 24: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Button - WindowsButton - OsxButton - AndroidButton

Checkbox - WindowsCheckbox - OsxCheckbox - AndroidCheckbox

WidgetFactory - WindowsWidgetFactory - OsxWidgetFactory - AndroidWidgetFactory

Page 25: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

WidgetFactory widgetFactory;

Page 26: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

WidgetFactory widgetFactory;

// “pseudocode” //switch(System.getProperty("os.name")) {

case "Windows":widgetFactory = new WindowsWidgetFactory();break;

}

Page 27: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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");

}

Page 28: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

WidgetFactory widgetFactory;

// “pseudocode” //switch(System.getProperty("os.name")) {

// ........ //

}

Button button = widgetFactory.createButton();Checkbox checkbox = widgetFactory.createCheckbox();

Page 29: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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();

Page 30: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Pattern types

Creational Patterns

Behavioural Patterns

Structural Patterns

Abstract Factory

Builder

Factory Method

Prototype

Singleton

30

Page 31: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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");

}}

31

Page 32: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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");

}}

32

Page 33: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

The telescoping constructor anti-pattern occurs when the increase of object constructor

parameter combinations leads to an exponential list of constructors

33

Page 34: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

The intent of the Builder design pattern is to separate the

construction of a complex object from its representation

34

Page 35: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public class Game {private final Player player;private final Level level;

public Game(Player player, Level level) {this.player = player;this.level = level;

}}

35

Page 36: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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 {

}}

Static builder class

36

Page 37: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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);

}}

}

Static builder class

37

Page 38: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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);}

}}

Static builder class

38

Page 39: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Usage:

public static void main(String[] args) {Game game = Game.builder()

.setLevel(new Level())

.setPlayer(new Player())

.build();}

39

Page 40: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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);}

}}

Duplication

Static builder class

40

Page 41: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Inner builder class

public class Game {private final Player player;private final Level level;

private Game() {}}

41

Page 42: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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 {

}}

42

Page 43: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Inner builder classpublic 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;

}}

} 43

Page 44: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Inner builder classpublic 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;

}}

}

Does not create new object on each build() call

44

Page 45: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Inner builder class + Cloneablepublic 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;

}}

45

Page 46: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Inner builder class + Cloneable

public Game build() {return Game.this.clone();

}

public Game build() {return Game.this;

}

Before

After

46

Page 47: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Usage:public static void main(String[] args) {

Game game = Game.builder().setLevel(new Level()).setPlayer(new Player()).build();

}

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());

}

vs.

47

Page 48: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Pattern types

Creational Patterns

Behavioural Patterns

Structural Patterns

Chain of responsibility

Memento

InterpreterIterator

Command

State

Template Method

Observer

Mediator

Strategy

Visitor 48

Page 49: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Pattern types

Creational Patterns

Behavioural Patterns

Structural Patterns

Chain of responsibility

Memento

InterpreterIterator

Command

State

Template Method

Observer

Mediator

Strategy

Visitor 49

Page 50: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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

50

Page 51: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Chain of responsibility

The idea is to process the message by yourself or to redirect it to someone else.

51

Page 52: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

You need to repair a car

Chain of responsibility

52

Page 53: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Can I repair it?

Please, repair my car

Chain of responsibility

53

Page 54: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Can I repair it?

No

Can I repair it?

Please, repair my car

Chain of responsibility

54

Page 55: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Can I repair it?

No Not everything

Can I repair it?

Can I repair it?

Please, repair my car

Chain of responsibility

55

Page 56: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Can I repair it?

No Not everything

Almost

Can I repair it?

Can I repair it?

Can I repair it?

Please, repair my car

Chain of responsibility

56

Page 57: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Can I repair it?

No Not everything

Almost

Done

Can I repair it?

Can I repair it?

Can I repair it?

Please, repair my car

Chain of responsibility

57

Page 58: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Chain of responsibility

58

Page 59: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Pattern types

Creational Patterns

Behavioural Patterns

Structural Patterns

Chain of responsibility

Memento

InterpreterIterator

Command

State

Template Method

Observer

Mediator

Strategy

Visitor 59

Page 60: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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

60

Page 61: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Visitor

Help Darth Vader to check the dislocation of his forces.

help!

61

Page 62: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

visit

1. Death Star accepts Darth Vader.

2. Darth Vader visits Death Star.

accept

Visitor

!!!

62

Page 63: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

visit

accept

visitaccept

Troopers on Death Star suggest Darth Vader what to visit next:

Star Destroyer.

Visitor

63

Page 64: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

visit

accept

visitacceptvisit

acceptIn the end he visits troopers.

Visitor

64

Page 65: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Visitor

65

Page 66: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Pattern types

Creational Patterns

Behavioural Patterns

Structural Patterns

Adapter

Facade

Composite

Decorator

Bridge

Flyweight

Proxy

66

Page 67: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Pattern types

Creational Patterns

Behavioural Patterns

Structural Patterns

Adapter

Facade

Composite

Decorator

Bridge

Flyweight

Proxy

67

Page 68: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

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

68

Page 69: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Composite

Fight!(don’t miss, please)

Darth Vader wants to control one trooper or a group of troopers in the same way

69

Page 70: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

… or even groups of groups of troopers

Composite

70

Page 71: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Darth Vader doesn’t care how many troopers to control - one or many

Composite!

Composite

71

Page 72: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public interface StormUnit {public void fight();

}

72

Page 73: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public class Stormtrooper implements StormUnit {

}

public interface StormUnit {public void fight();

}

73

Page 74: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public interface StormUnit {public void fight();

}

public class Stormtrooper implements StormUnit {@Overridepublic void fight() {

System.out.println("Yes, sir!");}

}

74

Page 75: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public class StormGroup implements StormUnit {private ArrayList<StormUnit> stormUnits = new ArrayList<>();

} 75

Page 76: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public class StormGroup implements StormUnit {private ArrayList<StormUnit> stormUnits = new ArrayList<>();

@Overridepublic void fight() {

System.out.println("Group is ready, sir!");for (StormUnit stormUnit : stormUnits) {

stormUnit.fight();}

}

} 76

Page 77: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

public class StormGroup implements StormUnit {private ArrayList<StormUnit> stormUnits = new ArrayList<>();

@Overridepublic 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);

}} 77

Page 78: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

Composite

78

Important!

Page 79: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

UI Components (Checkbox)

Material Design Light for Web (getmdl.io)

Page 80: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

UI Components (Checkbox)

<label for="chkbox1"> <input type="checkbox" id="chkbox1"> <span>Checkbox</span></label>

Material Design Light for Web (getmdl.io)

Page 81: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

UI Components (Toggle)

Bloc for Pharo(pharo.org)

Page 82: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

UI Components (Checkbox)

Bloc for Pharo(pharo.org)

Page 83: Software Design Patternsscg.unibe.ch/download/lectures/p2lab/20-05-01-week_9-Design-Patte… · Prototype Singleton 6. Pattern types Creational Patterns Behavioural Patterns Structural

The End.

83