[实验任务一]:人与肤色
使用抽象工厂模式,完成下述产品等级结构:
2.1 Man.java package chouxiang; /* * 抽象产品类Man */ public interface Man { public void makeM(); } 2.2 WhiteMan.java package chouxiang; public class WhiteMan implements Man { public void makeM() { System.out.println("我是白种男人!"); } } 2.3 YelloeMan.java package chouxiang; public class YellowMan implements Man { public void makeM() { System.out.println("我是黄种男人!"); } } 2.4 BlackMan.java package chouxiang; public class BlackMan implements Man { @Override public void makeM() { System.out.println("我是黑种男人!"); } } 2.5 Woman.java package chouxiang; /* * 抽象产品类Woman */ public interface Woman { public void makeW(); } 2.6 WhiteWoman.java package chouxiang; public class WhiteWoman implements Woman { @Override public void makeW() { System.out.println("我是白种女人!"); } } 2.7 YellowWoman.java package chouxiang; public class YellowWoman implements Woman { public void makeW() { System.out.println("我是黄种女人!"); } } 2.8 BlackWoman.java package chouxiang; public class BlackWoman implements Woman { @Override public void makeW() { System.out.println("我是黑种女人!"); } } 2.9 Color.java package chouxiang; /* * 抽象工厂类 (肤色) */ public interface Color { public Man produceMan(); public Woman produceWoman(); } 2.10 White.java package chouxiang; /* * 具体工厂类 白色肤色 */ public class White implements Color{ @Override public Man produceMan() { return new WhiteMan(); } @Override public Woman produceWoman() { return new WhiteWoman(); } } 2.11 Yellow.java package chouxiang; /* * 具体工厂类 黄色肤色 */ public class Yellow implements Color{ @Override public Man produceMan() { return new YellowMan(); } @Override public Woman produceWoman() { return new YellowWoman(); } } 2.12 Black.java package chouxiang; /* * 具体工厂类 黑色肤色 */ public class Black implements Color{ @Override public Man produceMan() { return new BlackMan(); } @Override public Woman produceWoman() { return new BlackWoman(); } } 2.13 XMLUtil.java package chouxiang; /* * 读取XML文件并根据存储在XML文件中的类名获取对应的对象 */ import javax.xml.parsers.*; import org.w3c.dom.*; import java.io.File; public class XMLUtil { @SuppressWarnings("deprecation") public static Object getBean() { try { //创建DOM文档对象 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dFactory.newDocumentBuilder(); Document doc; doc = builder.parse(new File("src/main/java/chouxiang/config.xml")); //获取包含类名的文本节点 NodeList nl = doc.getElementsByTagName("className"); Node classNode = nl.item(0).getFirstChild(); String cName = classNode.getNodeValue(); System.out.println("类名: "+ cName); //输出类名 //通过类名生成实例对象返回 Class<?> c = Class.forName("chouxiang."+cName); Object obj = c.newInstance(); return obj; } catch (Exception e) { e.printStackTrace(); return null; } } } 2.14 Client.java package chouxiang; /* * 客户端测试类 */ public class Client { public static void main(String[] args) { try { Color color; Man man; Woman woman; color=(Color)XMLUtil.getBean(); System.out.println(color); woman=color.produceWoman(); woman.makeW(); man=color.produceMan(); man.makeM(); }catch(Exception e){ System.out.print(e.getMessage()); } } } 2.15 config.xml <?xml version="1.0" encoding="UTF-8"?> <config> <className>Black</className> </config>
标签:chouxiang,Woman,java,package,工厂,抽象,public,Tutorial,Man From: https://www.cnblogs.com/manmmm/p/16798527.html