Software Design Patterns - Creational Patterns

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

  1. 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(“电脑组装完成,请验收”);


}
}

  1. 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 {  

//第一步:装CPU
//声明为抽象方法,具体由子类实现
public abstract void BuildCPU()

//第二步:装主板
//声明为抽象方法,具体由子类实现
public abstract void BuildMainboard();

//第三步:装硬盘
//声明为抽象方法,具体由子类实现
public abstract void BuildHD();

//返回产品的方法:获得组装好的电脑
public abstract Computer GetComputer();
}

  1. 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
//装机人员1
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
}
}

  1. 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.

  1. Creator
  2. Prototype
  3. 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

  1. 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) {
//create a small dog
Dog dog = Dogfactory.getDog("small";);
dog.speak();

...
}
}

  1. Abstract Factory Pattern
    • Abstract Factory create Factory

…write later…

Author

Elliot

Posted on

2021-03-28

Updated on

2025-04-18

Licensed under