-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDS03_AbstractFactory.java
More file actions
93 lines (64 loc) · 1.55 KB
/
DS03_AbstractFactory.java
File metadata and controls
93 lines (64 loc) · 1.55 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
//抽象产品
interface ICat {
void eat();
}
interface IDog {
void eat();
}
//具体产品
class BlackCat implements ICat {
public void eat() {
System.out.println("The black cat is eating!");
}
}
class WhiteCat implements ICat {
public void eat() {
System.out.println("The white cat is eating! ");
}
}
class BlackDog implements IDog {
public void eat() {
System.out.println("The black dog is eating");
}
}
class WhiteDog implements IDog {
public void eat() {
System.out.println("The white dog is eating!");
}
}
// 抽象工厂
interface IAnimalFactory{
ICat createCat();
IDog createDog();
}
//具体工厂
class BlackAnimalFactory implements IAnimalFactory {
public ICat createCat() {
return new BlackCat();
}
public IDog createDog() {
return new BlackDog();
}
}
class WhiteAnimalFactory implements IAnimalFactory {
public ICat createCat() {
return new WhiteCat();
}
public IDog createDog() {
return new WhiteDog();
}
}
public class DS03_AbstractFactory{
public static void main(String[] args) {
IAnimalFactory blackAnimalFactory = new BlackAnimalFactory();
ICat blackCat = blackAnimalFactory.createCat();
blackCat.eat();
IDog blackDog = blackAnimalFactory.createDog();
blackDog.eat();
IAnimalFactory whiteAnimalFactory = new WhiteAnimalFactory();
ICat whiteCat = whiteAnimalFactory.createCat();
whiteCat.eat();
IDog whiteDog = whiteAnimalFactory.createDog();
whiteDog.eat();
}
}