Software Design Patterns - Creational Patterns Deals with object creation mechanisms
Singleton
Builder
Prototype
Factory
Abstract Factory
Singleton Pattern A class of which only a single instance can exist Application needs one, and only one, instance of an object.
Applicable when
Ownership of the single instance cannot be reasonably assigned
Lazy initialization is desirable
Global access is not otherwise provided for
Basic version 1 2 3 4 5 6 7 8 9 public final class singleton { private static final Singleton INSTANCE = new Singleton (); private Singleton () {} public static Singleton getInstance () { return INSTANCE; } }
Lazy initialization version(Thread-Safe)
Pros: Initialised on first call to avoid memory wastage.
Cons: locks must be added to ensure thread safety, and adding locks will affect performance.
1 2 3 4 5 6 7 8 9 10 11 12 13 public class Singleton { private static Singleton instance = null ; private Singleton () { } public static Singleton getInstance () { if (instance == null ) { instance = new Singleton (); } return instance; } }
Initialiation-on-demand Holder Idiom 1 2 3 4 5 6 7 8 9 10 11 12 public class Singleton { private Singleton () {} private static class LazyHolder { static final Singleton INSTANCE = new Singleton (); } public static Singleton getInstance () { return LazyHolder.INSTANCE; } }
Builder Pattern Complex objects can be created directly without the user knowing the construction process and details of the object. Solves the Telescoping Problem.
CSDN Bloger「Carson_Ho」
Main parts of GoF’s Builder Pattern
Product object
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 public class Computer { private List<String> parts = new ArrayList <String>(); public void Add (String part) { parts.add(part); } public void Show () { for (int i = 0 ;i<parts.size();i++){ System.out.println(“组件”+parts.get(i)+“装好了”); } System.out.println(“电脑组装完成,请验收”); } }
A Builder(interface or abstract class)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 public abstract class Builder { public abstract void BuildCPU () ; public abstract void BuildMainboard(); public abstract void BuildHD(); public abstract Computer GetComputer(); }
Concrete Builder(extend the Builder)
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 28 public class ConcreteBuilder extend Builder{ Computer computer = new Computer (); @Override public void BuildCPU () { computer.Add("组装CPU" ) } @Override public void BuildMainboard(){ computer.Add("组装主板" ) } @Override public void BuildHD(){ computer.Add("组装主板" ) } @Override public Computer GetComputer(){ return computer } }
Director object
1 2 3 4 5 6 7 8 9 10 public class Director { public void Construct (Builder builder) { builder.BuildCPU(); builder.BuildMainboard(); builder.BuildHD(); } }
Prototype Pattern Creating duplicate object while keeping performance in mind. The operation will directly clone a object in the ram. RAM’s I/O is much faster than storage.
Creator
Prototype
ConcretePrototype
Factory Pattern In the factory pattern, we create objects without exposing the creation logic to the public and by using a common interface to point to the newly created object.
There’re two type of Factory Pattern
Simple Factory
Simple Factory create products(object)
1 2 3 4 5 6 7 8 9 10 public interface Dog { public void speak () ; }; public class Poodle implements Dog { public void speak () {System.out.println("Poodle says \"arf\"" )}; } public class Rottweiler implements Dog { public void speak () {System.out.println("Poodle says \"arf\"" )}; }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 public class DogFactory { public static Dog getDog (String criteria) { switch (criteria.toLowerCase()){ case "small" : return new Poodle (); case "big" : return new Rottweiler (); ... default : throw new RuntimeException ("Unsupported dog type: " + criteria); } } } public class TestFactory { public static void main (String[] args) { Dog dog = Dogfactory.getDog("small" ;); dog.speak(); ... } }
Abstract Factory Pattern
Abstract Factory create Factory
…write later…