1700457640
1700457641
}
1700457642
1700457643
黄色人种的创建工厂如代码清单8-17所示。
1700457644
1700457645
代码清单8-17 黄色人种的创建类
1700457646
1700457647
public class YellowHumanFactory extends AbstractHumanFactory{
1700457648
1700457649
public Human createHuman(){
1700457650
1700457651
return new YellowHuman();
1700457652
1700457653
}
1700457654
1700457655
}
1700457656
1700457657
白色人种的创建工厂如代码清单8-18所示。
1700457658
1700457659
代码清单8-18 白色人种的创建类
1700457660
1700457661
public class YellowHumanFactory extends AbstractHumanFactory{
1700457662
1700457663
public Human createHuman(){
1700457664
1700457665
return new WhiteHuman();
1700457666
1700457667
}
1700457668
1700457669
}
1700457670
1700457671
三个具体的创建工厂都非常简单,但是,如果一个系统比较复杂时工厂类也会相应地变复杂。场景类NvWa修改后的代码如代码清单8-19所示。
1700457672
1700457673
代码清单8-19 场景类NvWa
1700457674
1700457675
public class NvWa{
1700457676
1700457677
public static void main(String[]args){
1700457678
1700457679
//女娲第一次造人,火候不足,于是白色人种产生了
1700457680
1700457681
System.out.println(”—造出的第一批人是白色人种—”);
1700457682
1700457683
Human whiteHuman=(new WhiteHumanFactory()).createHuman();
1700457684
1700457685
whiteHuman.getColor();
1700457686
1700457687
whiteHuman.talk();
1700457688
1700457689
//女娲第二次造人,火候过足,于是黑色人种产生了
[
上一页 ]
[ :1.70045764e+09 ]
[
下一页 ]