一.简单工厂模式
使用简单工厂模式模拟女娲(Nvwa)造人(Person),如果传入参数M,则返回一个Man对象,如果传入参数W,则返回一个Woman对象,请实现该场景。现需要增加一个新的Robot类,如果传入参数R,则返回一个Robot对象,对代码进行修改并注意女娲的变化。
package 女娲造人;
public interface People {
public void makepeople();
}
public class Man implements
People {
public void makepeople() {
System.out.println("出现了男人");
}
}
public class Woman implements
People{
public void makepeople() {
System.out.println("出现了女人");
}
}
public class Robot implements
People{
public void makepeople() {
System.out.println("出现了机器人");
}
}
public class Nvzr {
public static People
getPeople(String arg) {
People people=null;
if(arg.equalsIgnoreCase("M")) {
people=new Man();
System.out.println("女娲造了男人");
}
else if(arg.equalsIgnoreCase("W"))
{
people=new Woman();
System.out.println("女娲造了女人");
}
else if(arg.equalsIgnoreCase("R"))
{
people=new Robot();
System.out.println("女娲造了机器人");
}
return people;
}
}
public class Client {
public static void main(String[] args) {
People p;
p=Nvzr.getPeople("M");
p.makepeople();
People p1;
p1=Nvzr.getPeople("W");
p1.makepeople();
People p2;
p2=Nvzr.getPeople("R");
p2.makepeople();
}
}
二.工厂方法模式
海尔工厂(Haier)生产海尔空调(HaierAirCondition),美的工厂(Midea)生产美的空调(MideaAirCondition) 。使用工厂方法模式描述该场景,绘制类图并编程实现。
package 工厂方法;
public interface AirCondition {
void work();
}
public interface
AirConditionFactory {
AirCondition produceAirCondition();
}
public class HaierAirConditionFactory implements AirConditionFactory{
public HaierAirCondition
produceAirCondition() {
System.out.println("美的工厂生产美的空调");
return new HaierAirCondition();
}
}
public class HaierAirCondition implements AirCondition {
public void work() {
System.out.println("海尔空调工作中");
}
}
public class MideaAirConditionFactory implements AirConditionFactory{
public
MideaAirCondition produceAirCondition() {
System.out.println("美的工厂生产美的空调");
return new MideaAirCondition();
}
}
public class MideaAirCondition implements AirCondition {
public void work() {
System.out.println("美的空调工作中");
}
}
public class AirConditionClient {
public static void main(String[] args) {
AirCondition haierCondition,mideaCondition;
AirConditionFactory haierConditionFactor = new HaierAirConditionFactory();
AirConditionFactory mideaConditionFactor = new MideaAirConditionFactory();
haierCondition=haierConditionFactor.produceAirCondition();
mideaCondition=mideaConditionFactor.produceAirCondition();
haierCondition.work();
mideaCondition.work();
}
}
三. 抽象工程模式
电脑配件生产工厂生产内存、CPU等硬件设备,这些内存、CPU的品牌、型号并不一定相同,根据下面的“产品等级结构-产品族”示意图,使用抽象工厂模式实现电脑配件生产过程并绘制相应的类图,绘制类图并编程实现。
package AbstractFactory;
public interface
ComputerPartsFactory {
CPU
produceCPU();
RAM
produceRAM();
}
public interface CPU {
public void discribe();
}
public class MacCPU implements
CPU{
public void discribe() {
System.out.println("MacCPU");
}
}
public class PcCPU implements
CPU{
public void discribe() {
System.out.println("PcCPU");
}
}
public interface RAM {
public void discribe();
}
public class PcRAM implements
RAM{
public void discribe() {
System.out.println("PcRAM");
}
}
public class MacRAM implements
RAM{
public void discribe() {
System.out.println("MacRAM");
}
}
public class Macfactory implements
ComputerPartsFactory{
public MacCPU produceCPU() {
System.out.println("MacCPU produced!");
return new MacCPU();
}
public RAM produceRAM() {
System.out.println("MacRAM produced!");
return new MacRAM();
}
}
public class PcFactory implements ComputerPartsFactory {
public PcCPU produceCPU() {
System.out.println("PcCPU produced!");
return new PcCPU();
}
public PcRAM produceRAM() {
System.out.println("PcRAM produced!");
return new PcRAM();
}
}
public class ComputerPartsClient {
public static void main(String args[]) {
try {
ComputerPartsFactory factory;
CPU cpu;
RAM ram;
factory=(ComputerPartsFactory)XMLUtil.getBean();
cpu=factory.produceCPU();
cpu.discribe();
ram=factory.produceRAM();
ram.discribe();
}
catch(Exception e) {
System.out.println(e.getMessage());
}
}
}
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(“AbstractFactoryconfig.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;
}
}
}