软件设计——抽象工厂模式之人与肤色
使用抽象工厂模式,完成下述产品等级结构:
1、类图
2、源代码
2.1 Man.java
package test4;
/*
* 抽象产品类Man
*/
public interface Man {
public void makeM();
}
2.2 WhiteMan.java
package test4;
public class WhiteMan implements Man {
public void makeM() {
System.out.println("我是白种男人!");
}
}
2.3 YelloeMan.java
package test4;
public class YellowMan implements Man {
public void makeM() {
System.out.println("我是黄种男人!");
}
}
2.4 BlackMan.java
package test4;
public class BlackMan implements Man {
@Override
public void makeM() {
System.out.println("我是黑种男人!");
}
}
2.5 Woman.java
package test4;
/*
* 抽象产品类Woman
*/
public interface Woman {
public void makeW();
}
2.6 WhiteWoman.java
package test4;
public class WhiteWoman implements Woman {
@Override
public void makeW() {
System.out.println("我是白种女人!");
}
}
2.7 YellowWoman.java
package test4;
public class YellowWoman implements Woman {
public void makeW() {
System.out.println("我是黄种女人!");
}
}
2.8 BlackWoman.java
package test4;
public class BlackWoman implements Woman {
@Override
public void makeW() {
System.out.println("我是黑种女人!");
}
}
2.9 Color.java
package test4;
/*
* 抽象工厂类 (肤色)
*/
public interface Color {
public Man produceMan();
public Woman produceWoman();
}
2.10 White.java
package test4;
/*
* 具体工厂类 白色肤色
*/
public class White implements Color{
@Override
public Man produceMan() {
return new WhiteMan();
}
@Override
public Woman produceWoman() {
return new WhiteWoman();
}
}
2.11 Yellow.java
package test4;
/*
* 具体工厂类 黄色肤色
*/
public class Yellow implements Color{
@Override
public Man produceMan() {
return new YellowMan();
}
@Override
public Woman produceWoman() {
return new YellowWoman();
}
}
2.12 Black.java
package test4;
/*
* 具体工厂类 黑色肤色
*/
public class Black implements Color{
@Override
public Man produceMan() {
return new BlackMan();
}
@Override
public Woman produceWoman() {
return new BlackWoman();
}
}
2.13 XMLUtil.java
package test4;
/*
* 读取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("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(cName);
Object obj = c.newInstance();
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
2.14 Client.java
package test4;
/*
* 客户端测试类
*/
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>
运行遇到错误:ClassNotFoundException
最终找到解决办法:修改XMLUtil . java,在两处加上具体路径,问题解决。