-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTestCreatePattern.java
More file actions
96 lines (88 loc) · 2.7 KB
/
TestCreatePattern.java
File metadata and controls
96 lines (88 loc) · 2.7 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import com.Abstract.Factory.AbstractFactory;
import com.Abstract.Factory.ComputerEngineer;
import com.Abstract.Factory.IntelFactory;
import com.Builder.Pattern.Builder;
import com.Builder.Pattern.ConcreteBuilder;
import com.Builder.Pattern.Director;
import com.Builder.Pattern.Product;
import com.Builder.Pattern.case01.WelcomeBuilder;
import com.Factory.Method.ExportFactory;
import com.Factory.Method.ExportFile;
import com.Factory.Method.ExportHtmlFactory;
import com.Static.Factory.Method.Login;
import com.Static.Factory.Method.LoginManager;
import org.junit.Test;
/**
* 测试创建型模式,共五种:工厂方法模式、抽象工厂模式、单例模式、建造者模式、原型模式。
*/
public class TestCreatePattern {
/**
* 简单工厂模式 Static.Factory.Method
*/
@Test
public void test01() {
String loginType = "password";
String name = "name";
String password = "password";
Login login = LoginManager.factory(loginType);
boolean bool = login.verify(name, password);
if (bool) {
/**
* 业务逻辑
*/
} else {
/**
* 业务逻辑
*/
}
}
/**
* 工厂模式 Factory.Method
*/
@Test
public void test02() {
String data = "";
ExportFactory exportFactory = new ExportHtmlFactory();
ExportFile ef = exportFactory.factory("financial");
ef.export(data);
}
/**
* 抽象工厂模式 Abstract.Pattern
*/
@Test
public void test03() {
//创建装机工程师对象
ComputerEngineer cf = new ComputerEngineer();
//客户选择并创建需要使用的产品对象
AbstractFactory af = new IntelFactory();
//告诉装机工程师自己选择的产品,让装机工程师组装电脑
cf.makeComputer(af);
}
/**
* 单例模式 Singleton.pattern
*/
@Test
public void test04() { }
/**
* 建造模式 Builder.Pattern
*/
@Test
public void test05() {
Builder builder = new ConcreteBuilder();
Director director = new Director(builder);
director.construct();
Product product = builder.retrieveResult();
System.out.println(product.getPart1());
System.out.println(product.getPart2());
}
/**
* 建造模式 Builder.Pattern.case01
*/
@Test
public void test06() {
com.Builder.Pattern.case01.Builder builder = new WelcomeBuilder();
com.Builder.Pattern.case01.Director director =
new com.Builder.Pattern.case01.Director(builder);
director.construct("toAddress@126.com", "fromAddress@126.com");
}
}