Template Pattern

Template Pattern

Template Pattern được sử dụng để tạo ra một mẫu phương thức và trì hoãn một số bước thực hiện ở các lớp con. Template Pattern quy định các bước thực thi một thuật toán và nó còn có thể cung cấp thực thi mặc định mà có thể phổ biến cho hầu hết hoặc một số các lớp con.

Giả sử chúng ta đưa ra 1 thuật toán xây nhà. Các bước cần thực hiện để xây nhà gồm: xây móng, xây cột, xây tường và cửa sổ. Điểm quan trọng là chúng ta không thể thay đổi thứ tự thực hiện, bởi chúng ta không thể xây cửa sổ trước khi làm móng. Như vậy trong trường hợp này, chúng ta có thể một phương thức mẫu (template method) mà sẽ sử dụng các phương thức khác nhau để xây nhà.

Để đảm bảo các lớp con không thể override phương thức template thì phương thức này cần được xác định là final.

1. Template Method Abstract Class
HouseTemplate.java
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
package com.journaldev.design.template;
 
public abstract class HouseTemplate {
 
    //template method, final so subclasses can't override
    public final void buildHouse(){
        buildFoundation();
        buildPillars();
        buildWalls();
        buildWindows();
        System.out.println("House is built.");
    }
 
    //default implementation
    private void buildWindows() {
        System.out.println("Building Glass Windows");
    }
 
    //methods to be implemented by subclasses
    public abstract void buildWalls();
    public abstract void buildPillars();
 
    private void buildFoundation() {
        System.out.println("Building foundation with cement,iron rods and sand");
    }
}

2. Template Method Concrete Classes
Giả sử chúng ta có 2 loại nhà là nhà gỗ và nhà kính.
WoodenHouse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.journaldev.design.template;
 
public class WoodenHouse extends HouseTemplate {
 
    @Override
    public void buildWalls() {
        System.out.println("Building Wooden Walls");
    }
 
    @Override
    public void buildPillars() {
        System.out.println("Building Pillars with Wood coating");
    }
 
}

GlassHouse.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
package com.journaldev.design.template;
 
public class GlassHouse extends HouseTemplate {
 
    @Override
    public void buildWalls() {
        System.out.println("Building Glass Walls");
    }
 
    @Override
    public void buildPillars() {
        System.out.println("Building Pillars with glass coating");
    }
 
}


3. Template Method Pattern Client
HousingClient.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
package com.journaldev.design.template;
 
public class HousingClient {
 
    public static void main(String[] args) {
         
        HouseTemplate houseType = new WoodenHouse();
         
        //using template method
        houseType.buildHouse();
        System.out.println("************");
         
        houseType = new GlassHouse();
         
        houseType.buildHouse();
    }
 
}

Kết quả hiển thị
1
2
3
4
5
6
7
8
9
10
11
Building foundation with cement,iron rods and sand
Building Pillars with Wood coating
Building Wooden Walls
Building Glass Windows
House is built.
************
Building foundation with cement,iron rods and sand
Building Pillars with glass coating
Building Glass Walls
Building Glass Windows
House is built.

Sơ đồ UML như sau
template-method-pattern






No comments :

Post a Comment