题目分析
(1)、 简单工厂模式
使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,请实现该场景。现需要增加一个新的Robot类,如果传入参数R,则返回一个Robot对象,对代码进行修改并注意女娲的变化。
(2)、 工厂方法模式
海尔工厂(Haier)生产海尔空调(HaierAirCondition),美的工厂(Midea)生产美的空调(MideaAirCondition) 。使用工厂方法模式描述该场景,绘制类图并编程实现。
(3)、 抽象工程模式
电脑配件生产工厂生产内存、CPU等硬件设备,这些内存、CPU的品牌、型号并不一定相同,根据下面的“产品等级结构-产品族”示意图,使用抽象工厂模式实现电脑配件生产过程并绘制相应的类图,绘制类图并编程实现。
(1)女娲简单工厂
Nvwa接口
public interface Nvwa {
public abstract void Person();
}
Nvwa实现类Woman
package Factory;
public class Woman implements Nvwa {
@Override
public void Person() {
System.out.println("创造一个女人");
}
}
实现类Man
package Factory;
public class Man implements Nvwa {
@Override
public void Person() {
System.out.println("创造一个男人");
}
}
实现类Robot
package Factory;
public class Robot implements Nvwa {
@Override
public void Person() {
System.out.println("创造一个机器人");
}
}
简单工厂实现Person类
package Factory;
import java.util.Scanner;
public class Person {
public static Nvwa person(String p) {
;
if ("W" == p) {
Woman woman = new Woman();
woman.Person();
return woman;
}
if ("M" == p) {
Man man = new Man();
man.Person();
return man;
}
if ("R" == p) {
Robot robot = new Robot();
robot.Person();
return robot;
}
return null;
}
}
main函数
package Factory;
public class main {
public static void main(String[] args) {
Nvwa man = Person.person("M");
Nvwa woman = Person.person("W");
Nvwa robot = Person.person("R");
}
}
(2)工厂方法
AirConditon接口
package AirCondition;
public interface AirCondition {
public abstract void Temper();
}
Factory接口
package AirCondition;
public interface Factory {
public abstract AirCondition Produce();
}
AirConditon实现类HaierAirConditon
package AirCondition;
public class HaierAirCondition implements AirCondition {
@Override
public void Temper() {
System.out.println("海尔空调调节温度...");
}
}
实现类MideaAirConditon
package AirCondition;
public class MideaAirCondition implements AirCondition {
@Override
public void Temper() {
System.out.println("美的空调调节温度");
}
}
Factory实现类HaierFactory
package AirCondition;
public class HaierFactory implements Factory{
@Override
public AirCondition Produce() {
System.out.println("海尔工厂生产海尔空调");
return new HaierAirCondition();
}
}
实现类MideaFactory
package AirCondition;
public class MideaFactory implements Factory {
@Override
public AirCondition Produce() {
System.out.println("美的工厂生产美的空调");
return new MideaAirCondition();
}
}
XMLUtil调用文档
package AirCondition;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.SAXException;
import java.io.*;
public class XMLUtil
{
//该方法用于从XML配置文件中提取具体类类名,并返回一个实例对象
public static Object getBean()
{
try
{
//创建文档对象
DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = dFactory.newDocumentBuilder();
Document doc;
doc = builder.parse(new File("FactoryMethodconfig.xml"));
//获取包含类名的文本节点
NodeList nl = doc.getElementsByTagName("className");
Node classNode=nl.item(0).getFirstChild();
String cName=classNode.getNodeValue();
//通过类名生成实例对象并将其返回
Class c=Class.forName(cName);
Object obj=c.newInstance();
return obj;
}
catch(Exception e)
{
e.printStackTrace();
return null;
}
}
}
config文件
<?xml version="1.0"?>
<config>
<className>AirCondition.HaierFactory</className>
</config>
main函数
package AirCondition;
public class main {
public static void main(String[] args) {
AirCondition airCondition;
Factory factory;
factory=(Factory) XMLUtil.getBean();
airCondition = factory.Produce();
airCondition.Temper();
}
}
(3)抽象程序模式
CPU接口
package Computer;
public interface CPU {
public abstract void CPUAdapter();
}
RAM接口
package Computer;
public interface RAM {
public abstract void RAMAdapter();
}
Factory接口
package Computer;
public interface AccessoriesFactory {
public abstract CPU BuiltCPU();
public abstract RAM BUiltRAM();
}
CPU实现类
package Computer;
public class MacCPU implements CPU {
@Override
public void CPUAdapter() {
System.out.println("适用Mac的CPU");
}
}
package Computer;
public class PcCPU implements CPU {
@Override
public void CPUAdapter() {
System.out.println("适用PC的CPU");
}
}
RAM实现类
package Computer;
public class PcRAM implements RAM {
@Override
public void RAMAdapter() {
System.out.println("适用PC的ARM");
}
}
package Computer;
public class MacRAM implements RAM {
@Override
public void RAMAdapter() {
System.out.println("适用Mac的ARM" );
}
}
Factory实现类
package Computer;
public class PcFactory implements AccessoriesFactory {
@Override
public PcCPU BuiltCPU() {
System.out.println("生产适用PC CPU");
return new PcCPU();
}
@Override
public PcRAM BUiltRAM() {
System.out.println("生成适用PC RAM");
return new PcRAM();
}
}
package Computer;
public class MacFactory implements AccessoriesFactory {
@Override
public MacCPU BuiltCPU() {
System.out.println("生产适用Mac CPU");
return new MacCPU();
}
@Override
public MacRAM BUiltRAM() {
System.out.println("生产适用Mac RAM");
return new MacRAM();
}
}
main函数
package Computer;
public class main {
public static void main(String[] args) {
AccessoriesFactory pc = new PcFactory(); //多态
AccessoriesFactory mac = new MacFactory();
CPU cpu = pc.BuiltCPU();
RAM ram = mac.BUiltRAM();
cpu.CPUAdapter();
ram.RAMAdapter();
}
}