1 of 29

2 of 29

Last week

  • SOLID principles
    • Single responsibility
    • Open/closed
    • Liskov substitution
    • Interface segregation
    • Dependency inversion
  • Creational patterns
    • Factory
    • Builder
    • Prototype
    • Singleton

3 of 29

Previous question: SRP vs ISP in SOLID

SRP (Single Responsibility Principle)

  • you should only have a single responsibility in a module. (code)

ISP (Interface Segregation Principle)

  • you should not be forced to be confronted with more than you actually need. (client)

4 of 29

Last week

5 of 29

Last week

  • SOLID principle
  • What does the O stand for ?

6 of 29

Last week

  • When we have a class with the sole responsibility of creating a model class, which design pattern would that illustrate?

7 of 29

Today’s agenda

Structural design patterns

8 of 29

Structural patterns

  • Patterns that ease the design by identifying a simple way to realize relationships between entities.

9 of 29

Structural patterns

  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Proxy

10 of 29

Adapter

  • Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces.

11 of 29

Adapter

12 of 29

Adapter - real world example

  • AC power adapters.

  • Say you're visiting Europe from the US, with your laptop, which expects a US power supply.

  • To get your laptop plugged in, you're going to need to get a power adapter that accepts your US plug and allows it to plug in the European power outlet.

  • The AC adapter knows how to deal with both sides, acting as a middleman - this is the adapter pattern.

13 of 29

Bridge

  • Decouple an abstraction from its implementation so that the two can vary independently.

14 of 29

Bridge

15 of 29

Differences between Adapter and Bridge

  • In the Bridge pattern, abstractions are expected and the code is designed around the abstractions.
  • In the Adapter pattern, the abstractions are added to be able to make the code run with a different implementation
  • Adapter makes things work after they're designed; Bridge makes them work before they are.

16 of 29

Composite

  • Compose objects into tree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

17 of 29

Composite

18 of 29

Composite example

Although the example is abstract, arithmetic expressions are Composites.

An arithmetic expression consists of an operand, an operator (+ - * /), and another operand.

The operand can be a number, or another arithmetic expression. Thus, 2 + 3 and (2 + 3) + (4 * 6) are both valid expressions.

19 of 29

Decorator

  • Attach additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

20 of 29

Decorator

21 of 29

Decorator

22 of 29

Decorator

23 of 29

Decorator

24 of 29

Differences between Composite and Decorator

  • Decorator is designed to let you add responsibilities to objects without subclassing.
  • Composite's focus is not on embellishment but on representation.
  • These intents are distinct but complementary.
  • Composite and Decorator are often used in concert.

25 of 29

Facade

  • Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use.

26 of 29

Facade

27 of 29

Proxy

  • Provide a surrogate or placeholder for another object to control access to it.

28 of 29

Proxy

29 of 29

Thank you for your attention!