Welcome to DEVDAY
Da Nang 2016
I’m Tuan
• Live in Da Nang.
• Working in Axon Active
Vietnam.
• 6 years in development
• Scrum Master, Team Leader,
Software Engineer.
• Java, Javascript, Code
Generator. tuan.do@axonactive.com
Questions
• Who joined Devday Da Nang 2015 ?
• Who knows about refactoring ?
• Who knows about clean code ?
Design Pattern 

at a glance
–Do Minh Tuan
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”
- Christopher Wolfgang
Alexander (1936)
Elements of Pattern
The pattern name The problem
The consequences The solution
PATTERN
Advantages of Pattern
• Enhances code readability.
• Robust, Scalable.
• Enhances software development.
• Well-structured system.
• Well-tested system.
Ready for change
Gift Question
Organize the catalog
Design pattern relationship
How to select a design
pattern
How design patterns solve
design problems
• Finding appropriate objects.
• Determining Object Granularity.
• Specifying Object Interfaces.
• Specifying Object Implementations.
How to use a design pattern
(1 of 2)
• Read the pattern.
• Study the structure, participants and
collaborations sections.
• Sample code of pattern
• Choose names for pattern participants that are
meaningful in the application context.
How to use a design pattern
(2 of 2)
• Define the classes.
• Define application-specific names for operations
in the pattern.
• Implement the operations to carry out the
responsibillities and collaborations in the
pattern.
“Famous” Patterns
• Creational
• Abstract Factory
• Factory Method
• Structural
• Adapter
• Composite
• Decorator
• Behavioral
• Observer
• Strategy
• Template Method
“Famous” Patterns
• Creational
• Abstract Factory
• Factory Method
• Structural
• Adapter
• Composite
• Decorator
• Behavioral
• Observer
• Strategy
• Template Method
Abstract Factory
“Provide an interface for creating families of
related or dependent objects without specifying
their concrete classes.”
“Famous” Patterns
• Creational
• Abstract Factory
• Factory Method
• Structural
• Adapter
• Composite
• Decorator
• Behavioral
• Observer
• Strategy
• Template Method
Factory Method
“Define an interface for creating an object, but let
subclasses decide which class to instantance.
Factory Method lets a class defer instantiation to
subclasses.”
Demo
Abstract Factory
• It isolates concrete classes.
• It makes exchanging product famillies easy.
• It promotes consistency among products.
• Supporting new kinds of products is difficult.
Factory Method
• Provides hooks for subclasses.
• Connects parallel class hierarchies.
“Famous” Patterns
• Creational
• Abstract Factory
• Factory Method
• Structural
• Adapter
• Composite
• Decorator
• Behavioral
• Observer
• Strategy
• Template Method
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.”
Demo
Adapter
• How much adapting does Adapter do ?
• Pluggable adapters.
• Using two-way adapters to provide transparent.
“Famous” Patterns
• Creational
• Abstract Factory
• Factory Method
• Structural
• Adapter
• Composite
• Decorator
• Behavioral
• Observer
• Strategy
• Template Method
Composite
“Compose objects into tree structures to represent
part-whole hierarchies. Composite lets clients treat
individual objects and compositions of objects
uniformly.”
Demo
Composite
• Defines class hierarchies consisting of primitive
objects and composite objects.
• Make the client simple.
• Make it easier to add new kinds of components.
• Can make your design overly general.
“Famous” Patterns
• Creational
• Abstract Factory
• Factory Method
• Structural
• Adapter
• Composite
• Decorator
• Behavioral Pattern
• Observer
• Strategy
• Template Method
Decorator
“Attach additional responsibilities to an object
dynamically. Decorators provide a flexible
alternative to subclassing for extending
functionality.”
Demo
Decorator
• More flexibility than static inheritance.
• Avoids feature-laden classes high up in the
hierarchy.
• A decorator and it’s component aren’t critical.
• Lost of little objects.
“Famous” Patterns
• Creational
• Abstract Factory
• Factory Method
• Structural
• Adapter
• Composite
• Decorator
• Behavioral
• Observer
• Strategy
• Template Method
Observer
“Define a one-to-many dependency between
objects so that when one object changes state, all
its dependents are notified and updated
automatically.”
Demo
Observer
• Abstract coupling between Subject and
Observer.
• Support for broadcast communication.
• Unexpected updates.
• Lost of little objects.
“Famous” Patterns
• Creational
• Abstract Factory
• Factory Method
• Structural
• Adapter
• Composite
• Decorator
• Behavioral
• Observer
• Strategy
• Template Method
Strategy
“Define a family of algorithms, encapsulate each
one, and make them interchangeable. Strategy
lets the algorithm vary independently from clients
that use it.”
Demo
Strategy
• Families of related algorithms.
• An alternative to subclassing.
• Strategies eliminates conditional statements.
• Clients must be aware of different Strategies.
• Communication overhead between Strategy and
Context.
• Increased number of objects.
“Famous” Patterns
• Creational
• Abstract Factory
• Factory Method
• Structural
• Adapter
• Composite
• Decorator
• Behavioral
• Observer
• Strategy
• Template Method
Template Method
“Define the skeleton of an algorithm in an
operation, deferring some steps to subclasses.
Template Method lets subclasses redefine certain
steps of an algorithm without changing the
algorithm’s structure.”
Demo
Question and Answer
Conclusion
The gang of Four(GoF)

Raph Johnson, Erich Gamma,
Richard Helm, John Vlissides
tuan.do@axonactive.com
https://vn.linkedin.com/in/dominhtuan
Thank you !

[DevDay 2016] Design Pattern at a glance - Speaker: Tuan Do – Scrum Master at Axon Active Vietnam

  • 2.
  • 3.
    I’m Tuan • Livein Da Nang. • Working in Axon Active Vietnam. • 6 years in development • Scrum Master, Team Leader, Software Engineer. • Java, Javascript, Code Generator. tuan.do@axonactive.com
  • 4.
    Questions • Who joinedDevday Da Nang 2015 ? • Who knows about refactoring ? • Who knows about clean code ?
  • 5.
    Design Pattern 
 ata glance –Do Minh Tuan
  • 6.
    What is adesign 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” - Christopher Wolfgang Alexander (1936)
  • 7.
    Elements of Pattern Thepattern name The problem The consequences The solution PATTERN
  • 8.
    Advantages of Pattern •Enhances code readability. • Robust, Scalable. • Enhances software development. • Well-structured system. • Well-tested system.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
    How to selecta design pattern
  • 14.
    How design patternssolve design problems • Finding appropriate objects. • Determining Object Granularity. • Specifying Object Interfaces. • Specifying Object Implementations.
  • 15.
    How to usea design pattern (1 of 2) • Read the pattern. • Study the structure, participants and collaborations sections. • Sample code of pattern • Choose names for pattern participants that are meaningful in the application context.
  • 16.
    How to usea design pattern (2 of 2) • Define the classes. • Define application-specific names for operations in the pattern. • Implement the operations to carry out the responsibillities and collaborations in the pattern.
  • 18.
    “Famous” Patterns • Creational •Abstract Factory • Factory Method • Structural • Adapter • Composite • Decorator • Behavioral • Observer • Strategy • Template Method
  • 19.
    “Famous” Patterns • Creational •Abstract Factory • Factory Method • Structural • Adapter • Composite • Decorator • Behavioral • Observer • Strategy • Template Method
  • 20.
    Abstract Factory “Provide aninterface for creating families of related or dependent objects without specifying their concrete classes.”
  • 22.
    “Famous” Patterns • Creational •Abstract Factory • Factory Method • Structural • Adapter • Composite • Decorator • Behavioral • Observer • Strategy • Template Method
  • 23.
    Factory Method “Define aninterface for creating an object, but let subclasses decide which class to instantance. Factory Method lets a class defer instantiation to subclasses.”
  • 25.
  • 26.
    Abstract Factory • Itisolates concrete classes. • It makes exchanging product famillies easy. • It promotes consistency among products. • Supporting new kinds of products is difficult.
  • 27.
    Factory Method • Provideshooks for subclasses. • Connects parallel class hierarchies.
  • 28.
    “Famous” Patterns • Creational •Abstract Factory • Factory Method • Structural • Adapter • Composite • Decorator • Behavioral • Observer • Strategy • Template Method
  • 29.
    Adapter “Convert the interfaceof a class into another interface clients expect. Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces.”
  • 31.
  • 32.
    Adapter • How muchadapting does Adapter do ? • Pluggable adapters. • Using two-way adapters to provide transparent.
  • 33.
    “Famous” Patterns • Creational •Abstract Factory • Factory Method • Structural • Adapter • Composite • Decorator • Behavioral • Observer • Strategy • Template Method
  • 34.
    Composite “Compose objects intotree structures to represent part-whole hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.”
  • 36.
  • 37.
    Composite • Defines classhierarchies consisting of primitive objects and composite objects. • Make the client simple. • Make it easier to add new kinds of components. • Can make your design overly general.
  • 38.
    “Famous” Patterns • Creational •Abstract Factory • Factory Method • Structural • Adapter • Composite • Decorator • Behavioral Pattern • Observer • Strategy • Template Method
  • 39.
    Decorator “Attach additional responsibilitiesto an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.”
  • 41.
  • 42.
    Decorator • More flexibilitythan static inheritance. • Avoids feature-laden classes high up in the hierarchy. • A decorator and it’s component aren’t critical. • Lost of little objects.
  • 43.
    “Famous” Patterns • Creational •Abstract Factory • Factory Method • Structural • Adapter • Composite • Decorator • Behavioral • Observer • Strategy • Template Method
  • 44.
    Observer “Define a one-to-manydependency between objects so that when one object changes state, all its dependents are notified and updated automatically.”
  • 46.
  • 47.
    Observer • Abstract couplingbetween Subject and Observer. • Support for broadcast communication. • Unexpected updates. • Lost of little objects.
  • 48.
    “Famous” Patterns • Creational •Abstract Factory • Factory Method • Structural • Adapter • Composite • Decorator • Behavioral • Observer • Strategy • Template Method
  • 49.
    Strategy “Define a familyof algorithms, encapsulate each one, and make them interchangeable. Strategy lets the algorithm vary independently from clients that use it.”
  • 51.
  • 52.
    Strategy • Families ofrelated algorithms. • An alternative to subclassing. • Strategies eliminates conditional statements. • Clients must be aware of different Strategies. • Communication overhead between Strategy and Context. • Increased number of objects.
  • 53.
    “Famous” Patterns • Creational •Abstract Factory • Factory Method • Structural • Adapter • Composite • Decorator • Behavioral • Observer • Strategy • Template Method
  • 54.
    Template Method “Define theskeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm’s structure.”
  • 56.
  • 57.
  • 58.
  • 59.
    The gang ofFour(GoF)
 Raph Johnson, Erich Gamma, Richard Helm, John Vlissides
  • 61.