Strategy pattern is useful when we want our application to be flexible. Made up of strategy and context.

Source code : https://github.com/Tonyzorz/DesignPatterns/tree/master/bin/behavior/strategy

'디자인 패턴' 카테고리의 다른 글

Builder Design Pattern  (0) 2020.02.25
Abstract Factory Design Pattern  (0) 2020.02.24
Factory Design Pattern  (0) 2020.02.22
Singleton Pattern  (0) 2020.02.19

When instantiating a class in client program, passing many arguments to a constructor can cause errors in many situations and bothersome. ex. having to remember constructor's parameter sequence. 

 

Builder design pattern solves this issue by building step by step, allowing client to designate only needed variables. 

 

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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package creational.builder;
 
public class Phone {
    
    private int gb;
    private int ram;
    private boolean graphicsCardEnabled;
    
    
    
    public Phone(PhoneBuilder builder) {
        super();
        this.gb = builder.gb;
        this.ram = builder.ram;
        this.graphicsCardEnabled = builder.graphicsCardEnabled;
    }
    
    public int getGb() {
        return gb;
    }
    public int getRam() {
        return ram;
    }
    public boolean isGraphicsCardEnabled() {
        return graphicsCardEnabled;
    }
    
    @Override
    public String toString() {
        return "Phone [gb=" + gb + ", ram=" + ram + ", graphicsCardEnabled=" + graphicsCardEnabled + "]";
    }
 
    public static class PhoneBuilder{
        
        private int gb;
        private int ram;
        private boolean graphicsCardEnabled;
        
        
        public PhoneBuilder(int gb) {
            super();
            this.gb = gb;
        }
        
        public PhoneBuilder setGb(int gb) {
            this.gb = gb;
            return this;
        }
        public PhoneBuilder setRam(int ram) {
            this.ram = ram;
            return this;
        }
        public PhoneBuilder setGraphicsCardEnabled(boolean graphicsCardEnabled) {
            this.graphicsCardEnabled = graphicsCardEnabled;
            return this;
        }
        
        public Phone build() {
            return new Phone(this);
        }
        
        
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 
1
2
3
4
5
6
7
8
public class MainClass {
 
    public static void main(String[] args) {
        Phone phone = new PhoneBuilder(2).setGraphicsCardEnabled(true).build();
        System.out.println(phone);
    }
}
 
http://colorscripter.com/info#e" target="_blank" style="color:#e5e5e5text-decoration:none">Colored by Color Scripter
 

 

 

 

'디자인 패턴' 카테고리의 다른 글

Strategy Pattern  (0) 2020.02.28
Abstract Factory Design Pattern  (0) 2020.02.24
Factory Design Pattern  (0) 2020.02.22
Singleton Pattern  (0) 2020.02.19

Using Factory design pattern within factory design pattern. 

 

Great for predefined objects but if new object has to be added, whole code needs to be adjusted. 

'디자인 패턴' 카테고리의 다른 글

Strategy Pattern  (0) 2020.02.28
Builder Design Pattern  (0) 2020.02.25
Factory Design Pattern  (0) 2020.02.22
Singleton Pattern  (0) 2020.02.19

Using a super class with multiple sub classes, and depending on input returns one sub class. Factory pattern allows no actual instantiation on client code. 

 

 

 

source : https://www.journaldev.com/1392/factory-design-pattern-in-java

 

'디자인 패턴' 카테고리의 다른 글

Strategy Pattern  (0) 2020.02.28
Builder Design Pattern  (0) 2020.02.25
Abstract Factory Design Pattern  (0) 2020.02.24
Singleton Pattern  (0) 2020.02.19

Singleton pattern only allows for one instance of the class to be created and exist in JVM. 

 

- Gof Creational Pattern

- Only one instance of class

- Must have global access point to create the instance

- Singleton pattern is used for logging, drivers objects and caching and thread pool.

 

Singleton pattern could be implemented in different ways but must have the following:

1. private constructor to restrict instantiation of the class from other classes.

2. private static variable of the same class that is the only instance of the class

3. public static class that returns the instance of the class.

 

1. Eager initialization

instance is created at the time of class loading, easiest to code but has many drawbacks.

- instance is created regardless w/o considering the possibility of not using the class.

- doesnt provide any exception handling

2. Static block intialization

similar to eager initialization, but adds static when creating instance for exception handling. Eager intialization and Static block intialization instances are created when class is being loaded so it is not the best method to create a singleton pattern. 

3. Lazy Initialization

Creates instance inside the global access method. It works fine in single-threaded environment but when working with multiple threads, if both threads are created at the same time, the singleton pattern will not working causing it to create two different instances of the class. 

4. Thread Safe Singleton

applying synchronized to a global access method will allow thread wait for other working threads when using the method. But applying synchronized directly to the method will cause unnecessary time costs so for this, we can add a synchronized block inside another if statement, a double checked locking principle.

5. Bill Pugh Singleton Implementation

Bill Pugh created a pattern where using inner static helper class to create a thread safe singleton instance.

SingletonHelper class is not loaded into the memory until getInstance is called, which saves unnecessary memory costs. This is most widely used singleton pattern. 

source: https://www.journaldev.com/1827/java-design-patterns-example-tutorial#creational-patterns

'디자인 패턴' 카테고리의 다른 글

Strategy Pattern  (0) 2020.02.28
Builder Design Pattern  (0) 2020.02.25
Abstract Factory Design Pattern  (0) 2020.02.24
Factory Design Pattern  (0) 2020.02.22

+ Recent posts