实验4:抽象工厂模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解抽象工厂模式的动机,掌握该模式的结构;
2、能够利用抽象工厂模式解决实际问题。
使用抽象工厂模式,完成下述产品等级结
类图
代码:
AbstractFactory.java
public interface AbstractFactory { public Man produceMan(); public Woman produceWoman(); }
BlackFactory.java
public class BlackFactory implements AbstractFactory{ public Man produceMan() { return new BlackMan(); } public Woman produceWoman() { return new BlackWoman(); } }
BlackMan.java
public class BlackMan implements Man{ public void show() { System.out.println("黑皮肤的男性"); } }
BlackWoman.java
public class BlackWoman implements Woman { public void show() { System.out.println("黑皮肤的女性"); } }
main.java
public class main { public static void main(String args[]) { try { AbstractFactory fac; Man m; Woman w; fac=new WhiteFactory(); m=fac.produceMan(); m.show(); w=fac.produceWoman(); w.show(); } catch(Exception e) { System.out.println(e.getMessage()); } } }
Man.java
public interface Man { public void show(); }
WhiteFactory.java
public class WhiteFactory implements AbstractFactory{ public Man produceMan() { return new WhiteMan(); } public Woman produceWoman() { return new WhiteWoman(); } }
WhiteMan.java
public class WhiteMan implements Man{ public void show() { System.out.println("白皮肤的男性"); } }
WhiteWoman.java
public class WhiteWoman implements Woman { public void show() { System.out.println("白皮肤的女性"); } }
Woman.java
public interface Woman { public void show(); }
YellowFactory.java
public class YellowFactory implements AbstractFactory{ public Man produceMan() { return new YellowMan(); } public Woman produceWoman() { return new YellowWoman(); } }
YellowMan.java
public class YellowMan implements Man{ public void show() { System.out.println("黄皮肤的男性"); } }
YellowWoman.java
public class YellowWoman implements Woman { public void show() { System.out.println("黄皮肤的女性"); } }
运行截图
详情https://www.cnblogs.com/15132949hao/p/16372839.html
标签:Woman,java,show,报告,class,今日,public,Man From: https://www.cnblogs.com/zjq164/p/17797096.html