软件设计 石家庄铁道大学信息学院
实验4:抽象工厂模式
本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:
1、理解抽象工厂模式的动机,掌握该模式的结构;
2、能够利用抽象工厂模式解决实际问题。
[实验任务一]:人与肤色
使用抽象工厂模式,完成下述产品等级结构:
实验要求:
1. 画出对应的类图;
2. 提交源代码;
Black.java
package test;
/*
* 具体工厂类 黑色肤色
*/
public class Black implements Color{
@Override
public Man produceMan() {
return new BlackMan();
}
@Override
public Woman produceWoman() {
return new BlackWoman();
}
}
BlackMan.java
package test;
public class BlackMan implements Man {
@Override
public void makeM() {
System.out.println("我是黑种男人!");
}
}
BlackWoman.java
package test;
public class BlackWoman implements Woman {
@Override
public void makeW() {
System.out.println("我是黑种女人!");
}
}
Client.java
package test;
/*
* 客户端测试类
*/
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());
}
}
}
Color.java
package test;
/*
* 抽象工厂类 (肤色)
*/
public interface Color {
public Man produceMan();
public Woman produceWoman();
}
Man.java
package test;
/*
* 抽象产品类Man
*/
public interface Man {
public void makeM();
}
White.java
package test;
/*
* 具体工厂类 白色肤色
*/
public class White implements Color{
@Override
public Man produceMan() {
return new WhiteMan();
}
@Override
public Woman produceWoman() {
return new WhiteWoman();
}
}
WhiteMan.java
package test;
public class WhiteMan implements Man {
public void makeM() {
System.out.println("我是白种男人!");
}
}
whiteWoman.java
package test;
public class WhiteWoman implements Woman {
@Override
public void makeW() {
System.out.println("我是白种女人!");
}
}
Woman.java
package test;
/*
* 抽象产品类Woman
*/
public interface Woman {
public void makeW();
}
XMLUtil.java
package test;
/*
* 读取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("test."+cName);
Object obj = c.newInstance();
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Yellow.java
package test;
/*
* 具体工厂类 黄色肤色
*/
public class Yellow implements Color{
@Override
public Man produceMan() {
return new YellowMan();
}
@Override
public Woman produceWoman() {
return new YellowWoman();
}
}
YellowMan.java
package test;
public class YellowMan implements Man {
public void makeM() {
System.out.println("我是黄种男人!");
}
}
YellowWoan.java
package test;
public class YellowWoman implements Woman {
public void makeW() {
System.out.println("我是黄种女人!");
}
}
Config.xml
<?xml version="1.0" encoding="UTF-8"?>
<config>
<className>Black</className>