[实验任务一]:人与肤色
使用抽象工厂模式,完成下述产品等级结构:
- 画出对应的类图;
- 提交源代码;
Black.java
package PeopleAndSkinColor;
public class Black implements Color{
@Override
public Man produceMan() {
return new BlackMan();
}
@Override
public Woman produceWoman() {
return new BlackWoman();
}
}
BlackMan.java
package PeopleAndSkinColor;
public class BlackMan implements Man {
@Override
public void makeM() {
System.out.println("我是黑种男人!");
}
}
BlackWoman.java
package PeopleAndSkinColor;
public class BlackWoman implements Woman {
@Override
public void makeW() {
System.out.println("我是黑种女人!");
}
}
Client.java
package PeopleAndSkinColor;
/*
* 客户端测试类
*/
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 PeopleAndSkinColor;
/*
* 抽象工厂类 (肤色)
*/
public interface Color {
public Man produceMan();
public Woman produceWoman();
}
Man.java
package PeopleAndSkinColor;
/*
* 抽象产品类Man
*/
public interface Man {
public void makeM();
}
White.java
package PeopleAndSkinColor;
/*
- 具体工厂类 白色肤色
*/
public class White implements Color{
@Override
public Man produceMan() {
return new WhiteMan();
}
@Override
public Woman produceWoman() {
return new WhiteWoman();
}
}
WhiteMan.java
package PeopleAndSkinColor;
public class WhiteMan implements Man {
public void makeM() {
System.out.println("我是白种男人!");
}
}
WhiteWoman.java
package PeopleAndSkinColor;
public class WhiteWoman implements Woman {
@Override
public void makeW() {
System.out.println("我是白种女人!");
}
}
Woman.java
package PeopleAndSkinColor;
/*
-
抽象产品类Woman
*/
public interface Woman {public void makeW();
}
XMLUtil.java
package PeopleAndSkinColor;
/*
- 读取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/resources/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("test4."+cName);
Object obj = c.newInstance();
return obj;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
Yellow.java
package PeopleAndSkinColor;
/*
- 具体工厂类 黄色肤色
*/
public class Yellow implements Color{
@Override
public Man produceMan() {
return new YellowMan();
}
@Override
public Woman produceWoman() {
return new YellowWoman();
}
}
YellowMan.java
package PeopleAndSkinColor;
public class YellowMan implements Man {
public void makeM() {
System.out.println("我是黄种男人!");
}
}
YellowWoman.java
package PeopleAndSkinColor;
public class YellowWoman implements Woman {
public void makeW() {
System.out.println("我是黄种女人!");
}
}
标签:Woman,java,package,模式,工厂,抽象,PeopleAndSkinColor,public,Man From: https://www.cnblogs.com/yzx-sir/p/17844330.html