Design Pattern

  • Design Patterns Outline
    • Creational Patterns
    • Structural Patterns
    • Behavioral Design Patterns
  • Factory Method Design Pattern
    • Implementation
    • When to use?
    • Design Principle
    • Simple factory

Design Patterns Outline

《Design Patterns elements of reusable object-oriented software》——A bible for object-oriented design patterns

writtern by Gang of four: Erich Gamma, John Vlissides, Ralph Johnson and Richard Helm. Published Oct 21, 1994

Design Patterns are devided into three categories:creational, structural and beahavioral, and consist of 23 different design pattern.

Creational Patterns

Deals with object creation. Complexity and coupling associated with new object creation and address it.

  • Abstract Factory
  • Factory
  • Builder
  • Singleton
  • Object Pool
  • Prototype

Structural Patterns

About class and object composition. Use of inheritance and associations to compose functionality

  • Adapter
  • Bridge
  • Composite
  • Decorator
  • Facade
  • Flyweight
  • Private Class Data
  • Proxy

Behavioral Design Patterns

  • Chain Of Responsibility
  • Command
  • Interpreter
  • Iterator
  • Mediator
  • Memento
  • Null Object
  • Observer
  • State
  • Strategy
  • Template Method
  • Vistor

Factory Method Design Pattern

Creational Pattern Define an interface for creating an object, but let subclasses decide which object to instantiate. Factory Method lets a class defer instantiation to subclasses.

FactoryMethodDesignPattern

Implementation

  • Provide an interface for creating an object - Here do not consider the word interface as Java interface. It simply means to provide a constract/method to create an object.
  • Let the subclass decide what exact object to instantiate - This pattern involves “inheritance”
  • Open-closed principle(OCP) - it states a class should be open for extension and close for modifications.

When to use?

  • If there is an inheritance hierarchy, where a polymorphic creation is needed.
  • When you are modifying a class to accommodate varying requirements(violating open-closed principle)

Design Principle

  • Depend on abstractions - Do not depend on concrete class.
  • The dependency inversion principle.

Simple factory

  • Providing a static creational method. It is static factory, does not cater to factory method design pattern defined by GoF. ex: Calendar.getInstance()

To be continued…