1 of 34

6. Design: Design Patterns

Choi, Kwanghoon

Chonnam National University

2 of 34

Table of Contents

  • Design Pattern

2

3 of 34

6.5 Design Patterns

  • A description of problem and the essence of its solution reusable in different settings
  • Design Patterns: Elements of Reusable Object-Oriented Software by GoF (Gang of Four)
    • 23 Design Patterns in 3 types:
      • Creational Pattern types
      • Structural Pattern types
      • Behavioral Pattern types

3

http://www.uml.org.cn/c++/pdf/DesignPatterns.pdf

4 of 34

Design Pattern Types

  • Creational patterns
    • Creation and composition of objects, mechanism to instantiate objects easier, and constraints on the type and number of objects
  • Structural patterns
    • How classes and objects are organized and integrated to build a larger structure
  • Behavioral patterns
    • The assignment of responsibility between objects and the manner in which communication is effected between objects

4

5 of 34

23 Design Patterns

5

Creational Patterns

Structural Patterns

Behavioral Patterns

  • Abstract Factory
  • Factory method
  • Builder
  • Prototype
  • Singleton
  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Façade
  • Flyweight
  • Proxy
  • Chain of Responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Observer
  • State
  • Strategy
  • Template Method
  • Visitor

6 of 34

Creational Patterns

6

  • Abstract factory pattern groups object factories that have a common theme.
  • Factory method pattern creates an object without specifying the exact class to create.
  • Builder pattern constructs complex objects by separating construction and representation.
  • Prototype pattern creates objects by cloning an existing object.
  • Singleton pattern restricts object creation for a class to only one instance.

7 of 34

Structural Patterns

7

  • Adapter pattern allows classes with incompatible interfaces to work together by wrapping its own interface around that of an already existing class.
  • Bridge pattern decouples an abstraction from its implementation so that the two can vary independently.
  • Composite pattern composes zero-or-more similar objects so that they can be manipulated as one object.
  • Decorator pattern dynamically adds/overrides behavior in an existing method of an object.
  • Façade pattern provides a simplified interface to a large bod of code.
  • Flyweight pattern reduces the cost of creating and manipulating a large number of similar objects.
  • Proxy pattern provides a placeholder for another object to control access, reduce cost, and reduce complexity.

8 of 34

Behavioral Patterns

8

  • Chain of Responsibility pattern delegates commands to a chain of processing objects.
  • Command pattern creates objects which encapsulate actions and parameters.
  • Interpreter pattern implements a specialized language.
  • Iterator pattern accesses the elements of an object sequentially without exposing its underlying representation.
  • Mediator pattern allows loose coupling between classes by being the only class that has detailed knowledge of their methods.
  • Memento pattern provides the ability to restore an object to its previous state (undo).
  • Observer pattern is a publish/subscribe pattern which allows a number of observer objects to see an event.
  • State pattern allows an object to alter its behavior when its internal state changes.
  • Strategy pattern allows one of a family of algorithms to be selected on-the-fly at runtime.
  • Template Method pattern defines the skeleton of an algorithm as an abstract class, allowing its subclasses to provide concrete behavior.
  • Visitor pattern separates an algorithm from an object structure by moving the hierarchy of methods into one object.

9 of 34

Factory Method Pattern

  • Creates an object without specifying the exact class (name) to create

9

참고) GoF 디자인 패턴

10 of 34

Factory Method Pattern

  • Creates an object without specifying the exact class to create

10

Example) CreateDocument()

참고) GoF 디자인 패턴

11 of 34

11

Application(추상클래스)

CreateDocument()

NewDocument()

PDF Viewer

Picture Viewer

HWP Viewer

12 of 34

12

factory method pattern을 사용하도록

리팩토링(refactoring)

참고) Joshua Kerievsky, Refactoring to design patterns

13 of 34

Abstract Factory Pattern

  • groups object factories that have a common theme

13

참고) GoF 디자인 패턴

14 of 34

Abstract Factory Pattern

  • groups object factories that have a common theme

14

참고) 최은만 교재

EIBBulb

EIBBlind

LuxmaterBlind

15 of 34

Abstract Factory Pattern

  • Q. Draw a class diagram for the following Java programs
    • DesignPatternExamples/src/com/example/designpattern/abstractfactory

  • Q. Explain the role of FactoryFactory
    • The ElevatorFactoryFactory class

15

16 of 34

Singleton Pattern

  • restricts object creation for a class to only one instance.

16

public class Singleton {

private static Singleton instance = null;

private Singleton() { … }

public static Singleton getInstance() {

if (instance == null)

instance = new Singleton();

return instance;

}

public void doSomething() {

}

}

참고) 정인상, 채흥석, Java로 배우는 디자인패턴

17 of 34

Adapter Pattern

  • allows classes with incompatible interfaces to work together by wrapping its own interface around a legacy system.

17

참고) 최은만 교재

[Problem]

18 of 34

Adapter Pattern

  • allows classes with incompatible interfaces to work together by wrapping its own interface around a legacy system.

18

참고) 최은만 교재

[Solution]

19 of 34

Adapter Pattern

  • Q. Draw a class diagram for the following Java programs
    • DesignPatternExamples/src/com/example/designpattern/adapter
    • Explain how Client can call specificRequest() in ClassB.

  • Q. How is it different from the class diagram in the previous slide?
    • Explain each role of the two inheritance relationships in the previous slide. (Target <- Adapter, Adaptee <- Adapter)

    • Draw a new class diagram for the adapter pattern based on the answer to the second question.

19

20 of 34

Composite Pattern

  • composes zero-or-more similar objects so that they can be manipulated as one object.

20

참고) GoF 디자인 패턴

21 of 34

Composite Pattern

  • Draw a class diagram for Computer, ComputerDevice, Client, etc. using this pattern

21

참고) 정인상, 채흥석, Java로 배우는 디자인패턴

22 of 34

Façade Pattern

  • provides a simplified interface to a large body of code.

22

참고) 최은만 교재

23 of 34

Façade Pattern

  • Compiler subsystem classes

23

참고) GoF 디자인 패턴

24 of 34

Façade Pattern

  • Virtual memory framework

24

참고) GoF 디자인 패턴

25 of 34

Iterator Pattern

  • accesses the elements of an object sequentially without exposing its underlying representation.

  • cf. Iterator, Iterable, for each statement in Java
    • java.lang.Iterator<E>
    • java.util.Iterable<E>

25

참고) GoF 디자인 패턴

26 of 34

Observer Pattern

  • a publish/subscribe pattern which allows a number of observer objects to see an event.

26

참고) 최은만 교재

27 of 34

Observer Pattern

  • a publish/subscribe pattern which allows a number of observer objects to see an event.

27

참고) 최은만 교재

28 of 34

State Pattern

  • allows an object to alter its behavior when its internal state changes (a.k.a. objects for states)

28

참고) GoF 디자인 패턴

29 of 34

State Pattern

  • Light

29

OFF

ON

on button

off button

off button

on button

[The initial design]

OFF

ON

on button

off button

off button

on button

[A new design]

SLEEP

on button

off button

참고) 정인상, 채흥석, Java로 배우는 디자인패턴

30 of 34

State Pattern

  • allows an object to alter its behavior when its internal state changes (a.k.a. objects for states)

30

참고) 최은만 교재

31 of 34

Decorator Pattern

  • dynamically adds/overrides behavior in an existing method of an object.

31

32 of 34

Decorator Pattern

  • Various combinations of road displays

32

  • new RoadyDisplay().draw()

// 기본 도로 표시

  • new LaneDecorator(new RoadDisplay()).draw()

// 기본 도로 표시 + 차선 표시

  • new TrafficDecorator(new RoadDisplay()).draw()

// 기본 도로 표시 + 교통량 표시

  • new TrafficDecorator(new LaneDecorator(new RoadDisplay())).draw()

// 기본 도로 표시 + 차선 표시 + 교통량 표시

33 of 34

Decorator Pattern

  • Sequence diagram (roadWithLane.draw)

33

34 of 34

34

참고) GoF 디자인 패턴