1、理解抽象工厂模式的动机,掌握该模式的结构;
2、能够利用抽象工厂模式解决实际问题。
[实验任务一]:人与肤色
使用抽象工厂模式,完成下述产品等级结构:
实验要求:
1. 画出对应的类图;
2. 提交源代码;
Black.java
package com.a004;
public class Black implements Color{
@Override
public Man produceMan() {
return new BlackMan();
}
@Override
public Woman produceWoman() {
return new BlackWoman();
}
}
BlackMan.java
package com.a004;
public class BlackMan implements Man{
@Override
public void play(){
System.out.println("我是黑种男人!");
}
}
BlackWoman.java
package com.a004;
public class BlackWoman implements Woman {
@Override
public void play() {
System.out.println("我是黑种女人!");
}
}
Color.java
package com.a004;
public interface Color {
public Man produceMan();
public Woman produceWoman();
}
Yellow.java
package com.a004;
public class Yellow implements Color{
@Override
public Man produceMan() {
return new YellowMan();
}
@Override
public Woman produceWoman() {
return new YellowWoman();
}
}
White.java
package com.a004;
public class White implements Color {
@Override
public Man produceMan() {
return new WhiteMan();
}
@Override
public Woman produceWoman() {
return new WhiteWoman();
}
}
Man.java
1 package com.a004;
2
3 public interface Man {
4 public void play();
5 }
WhiteMan.java
package com.a004;
public class WhiteMan implements Man {
@Override
public void play() {
System.out.println("我是白种男人!");
}
}
YellowMan.java
package com.a004;
public class YellowMan implements Man{
@Override
public void play() {
System.out.println("我是黄种男人!");
}
}
Woman.java
package com.a004;
public interface Woman {
public void play() ;
}
WhiteWoman.java
package com.a004;
public class WhiteWoman implements Woman {
@Override
public void play() {
System.out.println("我是白种女人!");
}
}
YellowWoman.java
package com.a004;
public class YellowWoman implements Woman {
@Override
public void play() {
System.out.println("我是黄种女人!");
}
}
XMLUtil.java
package com.a004;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.File;
public class XMLUtil {
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); //输出类名
//通过类名生成实例对象返回
Object obj = null;
try {
Class c =Class.forName("com.a004."+cName);
obj = c.newInstance();
} catch (ClassNotFoundException e) {
System.out.println("ClassNotFoundException");
e.printStackTrace();
} catch (InstantiationException e) {
System.out.println("InstantiationException");
e.printStackTrace();
} catch (IllegalAccessException e) {
System.out.println("IllegalAccessException");
e.printStackTrace();
}
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
config.xml
1 <?xml version = "1.0"?>
2 <config>
3 <className>Black</className>
4 </config>
Client.java
package com.a004;
public class Client {
public static void main(String[] args) {
Color color;
Man man;
Woman woman;
color=(Color) XMLUtil.getBean();
// color=new White();
woman=color.produceWoman();
woman.play();
man=color.produceMan();
man.play();
}
}
3.注意编程规范。