Design Pattern


1. Design Pattern

  • Design Pattern is a repeatable-solution/ template for a commanly recurring problem in software design.
  • The developement paradigms have been tested and proven, so by using it we could: 1) Speed up the development process. 2) Prevent subtle issues and improve coding readability.

2. Creational Patterns

  • Creational Patterns are desin patterns that deal with object creation mechanisms, trying to create objects in a suitable manner under specific situation.

    2.1 Sigleton Pattern

  • Singleton pattern ensures, one class only has one instance, and provides a global point of access to it.
  • Examples
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    // not thread safe
    class Singleton {
    private static Singleton singleInstance = null;

    private Singleton() {
    }

    public static Singleton getInstance() {
    if (singleInstance == null) {
    singleInstance = new Singleton();
    }

    return singleInstance;
    }
    }

    // thread safe
    class Singleton{
    private static Singleton singleInstance = new Singleton();

    private Singleton() {
    }

    public static Singleton getInstance(){
    return singleInstance;
    }
    }

2.2 Factory Pattern

  • Factory Pattern defines an interface for creating an object, but let subclasses decide which class to instantiate. Factory method pattern lets a class defer instantiation to subclasses.

3. Structural Patterns

4. Behavioral Patterns


Author: Luchen
Reprint policy: All articles in this blog are used except for special statements CC BY 4.0 reprint polocy. If reproduced, please indicate source Luchen !
  TOC