Java实现软件设计模式---抽象工厂模式(性别产品等级结构肤色产品族)
一、题目要求
二、画出对应的类图
三、文件目录结构
四、具体实现代码
Black.java
1 package com.a004; 2 3 public class Black implements Color{ 4 @Override 5 public Man produceMan() { 6 return new BlackMan(); 7 } 8 9 @Override 10 public Woman produceWoman() { 11 return new BlackWoman(); 12 } 13 }
BlackMan.java
1 package com.a004; 2 3 public class BlackMan implements Man{ 4 @Override 5 public void play(){ 6 System.out.println("我是黑种男人!"); 7 } 8 }
BlackWoman.java
1 package com.a004; 2 3 public class BlackWoman implements Woman { 4 5 @Override 6 public void play() { 7 System.out.println("我是黑种女人!"); 8 } 9 }
Color.java
1 package com.a004; 2 3 public interface Color { 4 public Man produceMan(); 5 public Woman produceWoman(); 6 }
Yellow.java
1 package com.a004; 2 3 public class Yellow implements Color{ 4 @Override 5 public Man produceMan() { 6 return new YellowMan(); 7 } 8 9 @Override 10 public Woman produceWoman() { 11 return new YellowWoman(); 12 } 13 }
White.java
1 package com.a004; 2 3 public class White implements Color { 4 @Override 5 public Man produceMan() { 6 return new WhiteMan(); 7 } 8 9 @Override 10 public Woman produceWoman() { 11 return new WhiteWoman(); 12 } 13 }
Man.java
1 package com.a004; 2 3 public interface Man { 4 public void play(); 5 }
WhiteMan.java
1 package com.a004; 2 3 public class WhiteMan implements Man { 4 @Override 5 public void play() { 6 System.out.println("我是白种男人!"); 7 } 8 }
YellowMan.java
1 package com.a004; 2 3 public class YellowMan implements Man{ 4 @Override 5 public void play() { 6 System.out.println("我是黄种男人!"); 7 } 8 }
Woman.java
1 package com.a004; 2 3 public interface Woman { 4 5 public void play() ; 6 }
WhiteWoman.java
1 package com.a004; 2 3 public class WhiteWoman implements Woman { 4 @Override 5 public void play() { 6 System.out.println("我是白种女人!"); 7 } 8 }
YellowWoman.java
1 package com.a004; 2 3 public class YellowWoman implements Woman { 4 @Override 5 public void play() { 6 System.out.println("我是黄种女人!"); 7 } 8 }
五、测试代码以及配置代码
XMLUtil.java
1 package com.a004; 2 import javax.xml.parsers.*; 3 import org.w3c.dom.*; 4 import org.xml.sax.SAXException; 5 import java.io.File; 6 public class XMLUtil { 7 public static Object getBean() { 8 try { 9 //创建DOM文档对象 10 DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance(); 11 DocumentBuilder builder = dFactory.newDocumentBuilder(); 12 Document doc; 13 doc = builder.parse(new File("config.xml")); 14 //获取包含类名的文本节点 15 NodeList nl = doc.getElementsByTagName("className"); 16 Node classNode = nl.item(0).getFirstChild(); 17 String cName = classNode.getNodeValue(); 18 System.out.println("类名: "+ cName); //输出类名 19 //通过类名生成实例对象返回 20 21 Object obj = null; 22 try { 23 Class c =Class.forName("com.a004."+cName); 24 obj = c.newInstance(); 25 } catch (ClassNotFoundException e) { 26 System.out.println("ClassNotFoundException"); 27 e.printStackTrace(); 28 } catch (InstantiationException e) { 29 System.out.println("InstantiationException"); 30 e.printStackTrace(); 31 } catch (IllegalAccessException e) { 32 System.out.println("IllegalAccessException"); 33 e.printStackTrace(); 34 } 35 return obj; 36 } catch (Exception e) { 37 e.printStackTrace(); 38 return null; 39 } 40 } 41 }
config.xml
1 <?xml version = "1.0"?> 2 <config> 3 <className>Black</className> 4 </config>
Client.java
1 package com.a004; 2 public class Client { 3 public static void main(String[] args) { 4 Color color; 5 Man man; 6 Woman woman; 7 color=(Color) XMLUtil.getBean(); 8 // color=new White(); 9 woman=color.produceWoman(); 10 woman.play(); 11 man=color.produceMan(); 12 man.play(); 13 } 14 }
测试结果
好看请赞,养成习惯:) 本文来自博客园,作者:靠谱杨, 转载请注明原文链接:https://www.cnblogs.com/rainbow-1/p/15327896.html
欢迎来我的51CTO博客主页踩一踩 我的51CTO博客
文章中的公众号名称可能有误,请统一搜索:靠谱杨的秘密基地