尊重原创 http://write.blog.csdn.net/postedit/26062579
本文代码:http://download.csdn.net/detail/yuanzeyao2008/7360653
工厂模式主要是用来生成具有相同接口的类
工厂模式主要包括:3、抽象工厂
我们首先来学习一下简单工厂的原理:
学习背景:
我需要这样一个智能程序,我对它讲话,它能够为我制造一台能够满足我需求的电器
如:我要看电视,它给我制造一台电视,我要洗衣服,它给我制造一台洗衣机...
首先我使用面向过程的方法来实现这个程序
public static void main(String[] args) throws IOException
{
//从控制台获取用户需求
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String input=br.readLine();
//根据用户需求,制造相应的电器为用户服务
if(input.equals("洗衣服"))
{
Washer washer=new Washer();
washer.execute();
}else if(input.equals("看电视"))
{
Televisor tv=new Televisor();
tv.execute();
}else if(input.equals("冰冻食物"))
{
Refrigerator ref=new Refrigerator();
ref.execute();
}
}简单工厂的类图如下:
步骤:
1、建立一个电器的公用接口Machine
/**
* 电器的公用接口,没一种使用工厂创建的都必须实现它
* com.design.factory.simple.Machine
* @author yuanzeyao <br/>
* create at 2014年5月17日 上午10:44:58
*/
public interface Machine
{
public void execute();
}2、每一种电器都实现接口,具体见代码/**
* 简单工厂类,用于生产实现了Machine的各种电器
* com.design.factory.simple.SimpleFactory
* @author yuanzeyao <br/>
* create at 2014年5月17日 上午10:34:41
*/
public class SimpleFactory
{
private static final String TAG = "SimpleFactory";
public static Machine createMachine(String demand)
{
//根据用户需求,制造相应的电器为用户服务
if(demand.equals("洗衣服"))
{
return new Washer();
}else if(demand.equals("看电视"))
{
return new Televisor();
}else if(demand.equals("看电视"))
{
return new Refrigerator();
}else
return null;
}
}
4、客户端
public class Simple2
{
private static final String TAG = "Simple2";
public static void main(String[] args) throws IOException
{
//从控制台获取用户需求
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String input=br.readLine();
Machine machine=SimpleFactory.createMachine(input);
if(machine!=null)
{
machine.execute();
}
}
}工厂方法的类图如下:
实现方式:
1、编写一个抽象工厂类
/**
* 工厂抽象出来的接口
* com.design.factory.method.IFactory
* @author yuanzeyao <br/>
* create at 2014年5月17日 上午11:34:03
*/
public interface IFactory
{
public static final String TAG = "IFactory";
public Machine createMachine();
}2、各个机器的具体工厂类,这里我只贴出洗衣机的,其他的大家可以下载代码查看
/**
* 洗衣机具体工厂类
* com.design.factory.method.WasherFactory
* @author yuanzeyao <br/>
* create at 2014年5月17日 上午11:35:43
*/
public class WasherFactory implements IFactory
{
private static final String TAG = "WasherFactory";
@Override
public Machine createMachine()
{
return new Washer();
}
}
public class Sample3
{
private static final String TAG = "Sample";
public static void main(String[] args) throws IOException
{
IFactory factory=null;
//从控制台获取用户需求
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String input=br.readLine();
//根据用户需求,制造相应的电器为用户服务
if(input.equals("洗衣服"))
{
factory=new WasherFactory();
}else if(input.equals("看电视"))
{
factory=new TelevisorFactory();
}else if(input.equals("冰冻食物"))
{
factory=new RefrigeratorFactory();
}
Machine machine=factory.createMachine();
machine.execute();
}
}
我们使用代码来实现抽象工厂
1、实现一个工厂的抽象类
/**
*抽象工厂类,里面生产多件产品,这些产品有着某种关联,比如都是该厂家生产
* com.design.factory.abstractfactory.IFactory
* @author yuanzeyao <br/>
* create at 2014年5月17日 下午12:31:44
*/
public interface IFactory
{
public Machine createTelevisor();
public Machine createWasher();
public Machine createRefrigerator();
}/**
* 生产美的电器的工厂
* com.design.factory.abstractfactory.MediaFactory
* @author yuanzeyao <br/>
* create at 2014年5月17日 下午12:39:41
*/
public class MediaFactory implements IFactory
{
private static final String TAG = "MediaFactory";
@Override
public Machine createTelevisor()
{
return new MediaTv();
}
@Override
public Machine createWasher()
{
return new MediaWasher();
}
@Override
public Machine createRefrigerator()
{
return new MediaRefrigerator();
}
}
/**
* 生产康佳电器的工厂
* com.design.factory.abstractfactory.KanjiaFactory
* @author yuanzeyao <br/>
* create at 2014年5月17日 下午12:40:00
*/
public class KanjiaFactory implements IFactory
{
private static final String TAG = "MediaFactory";
@Override
public Machine createTelevisor()
{
return new KanjiaTv();
}
@Override
public Machine createWasher()
{
return new KanjiaWasher();
}
@Override
public Machine createRefrigerator()
{
return new KanjiaRefrigerator();
}
}public class Sample4
{
private static final String TAG = "Sample4";
public static void main(String[] args) throws IOException
{
//从控制台获取用户需求
//数据输入格式:厂家首字母大写_需求 如:M_洗衣服,就给你返回美的洗衣机
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
String input=br.readLine();
String[]inputs=input.split("_");
IFactory factory=null;
if(inputs[0].equals("M"))
{
factory=new MediaFactory();
}else
{
factory=new KanjiaFactory();
}
if(inputs[1].equals("洗衣服"))
{
factory.createWasher().execute();
}else if(inputs[1].equals("看电视"))
{
factory.createTelevisor().execute();
}else if(inputs[1].equals("冰冻食物"))
{
factory.createRefrigerator().execute();
}
}
}原文:http://blog.csdn.net/yuanzeyao/article/details/26062579