In general – Twice in detail – References



In general – Twice in detail – References

0 0


school_design_patterns

Presentation about a few design patterns.

On Github f3dd3rn / school_design_patterns

In general. Twice in detail. References.

Meet the referee

Agenda

In general

What are design patterns? 3 basic kinds 23 design patterns Why should we use them?

Twice in detail

Singleton Factory Method

References

  • articles
  • books

In general

What are design patterns?

3 basic kinds

behavioral

creational

structural

Why should we use them?

But...

Any questions?

Twice in detail

Singleton

  • exactly 1 instance
  • global access point
  • just-in-time initialization

Implementation

public class Singleton {
  private static Singleton _instance = null;

  private Singleton(){}

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

}

Subclassing variations

Instantiation in subclasses Registry

Outsource instantiation

public class Singleton {
  private static Singleton _instance;

  private Singleton(){}

  public static Singleton getInstance(String classname) throws IllegalArgumentException {
    if(singletonName.equals("logger")) {
      _instance = Logger.getInstance();
    } else if(singletonName.equals("debugger")) {
      _instance = Debugger.getInstance();
    } else if(singletonName.equals("singleton")) {
      _instance = new Singleton();
    } else {
      throw new IllegalArgumentException("Illegal classname");
    }
    return _instance;
  }
}

Registry

public class Singleton {
   private static Map<String,Singleton> map = new HashMap<String,Singleton>();
   
   private Singleton() {}
   
   public static Singleton getInstance(String classname) throws IllegalArgumentException {
    if(classname == null) {
      throw new IllegalArgumentException("Illegal classname");
    }
      
    Singleton singleton = (Singleton)map.get(classname);
    if(singleton != null) {
       return singleton;
    }

    if(classname.equals("SingeltonSubclassOne")) {
      singleton = new SingletonSubclassOne();         
    } else if(classname.equals("SingeltonSubclassTwo")) {
      singleton = new SingletonSubclassTwo();
    } else if(classname.equals("Singelton")) {
      singleton = new Singleton();
    } else {
      throw new IllegalArgumentException("Illegal classname");
    }
    map.put(classname, singleton);
    return singleton;
  }
}

Any questions?

Factory Method

"Classic" "Popular"

"Classic" definition

outsource instantiation

Implementation variations

  • abstract vs. concrete creator class
  • parameterized factory method

Popular definition

static methods

Implementation of popular definition

public class Color { 
  public static Color make_RGB_color(int red, int green, int blue) {
    // do whatever you need to do 
  }
  
  public static Color make_HSB_color(float hue, float saturation, float brightness) {
    // do whatever you need to do 
  }
  
  public static Color make_HEX_color(String hexCode) {
    // do whatever you need to do 
  }
}

Implementation variations

Caching

Any questions?

References

SingletonFactory Method