首页 > 其他 > 详细

设计模式之四 抽象工厂模式

时间:2014-04-02 03:18:06      阅读:618      评论:0      收藏:0      [点我收藏+]

大家好,现在咱们来讨论一下抽象工厂模式吧。还是以我之前给的例子(详情可以看之前的简单工厂)但是现在要做出一点假设:

假设 苹果公司和小米公司都有生产 Phone,MP3之类的...(纯粹是为了方便说明)

好了,现在说说产品等级结构和产品族的概念

所谓的产品等级结构:就是指某一类产品的继承结构,比如说电话,它包括IPhone电话和XiaoMi电话,他们就是属于同一个产品等级结构

所谓的产品族:就是指处于不同产品等级结构的一组产品,且是由同一个工厂产生的。比如说 Apple公司既可以生产IPhone,也可以生产IPOD,而小米公司既可以生产XiaoMi手机,也可以生产XiaoMiPod(假设...)

好了,现在让我们来看看UML图吧:


bubuko.com,布布扣

现在看看代码吧

package edu.fjnu.cs.hwb.abstractfactory;

public interface Phone
{
	void call(); 
}

package edu.fjnu.cs.hwb.abstractfactory;

public interface Pod 
{
	void play();
}

package edu.fjnu.cs.hwb.abstractfactory;

public interface AbstractFactory
{
	Phone getPhone(String name);
	Pod getPod(String name);
}

现在看看具体的实现类吧

首先是Apple公司的产品

package edu.fjnu.cs.hwb.abstractfactory;

public class IPhone4 implements Phone 
{
	@Override
	public void call()
	{
		System.out.println("this is IPhone4");
	}
}
package edu.fjnu.cs.hwb.abstractfactory;

public class IPhone4s implements Phone
{
	@Override
	public void call()
	{
		System.out.println("this is IPhone4s");
	}
}
package edu.fjnu.cs.hwb.abstractfactory;

public class IPod1 implements Pod 
{
	@Override
	public void play() 
	{
		System.out.println("this is IPod1");
	}
}
package edu.fjnu.cs.hwb.abstractfactory;

public class IPod2 implements Pod 
{
	@Override
	public void play() 
	{
		System.out.println("this is IPod2");
	}
}

现在看看小米公司的:

package edu.fjnu.cs.hwb.abstractfactory; 

public class XiaoMi2 implements Phone
{
	@Override
	public void call()
	{
		System.out.println("this is XiaoMi2");
	}
}
package edu.fjnu.cs.hwb.abstractfactory;

public class XiaoMi2S implements Phone 
{
	@Override
	public void call() 
	{
		System.out.println("this is XiaoMi2S");
	}
}
package edu.fjnu.cs.hwb.abstractfactory;

public class XiaoMiPod1 implements Pod
{
	@Override
	public void play()
	{
		System.out.println("this is XiaoMiPod1");
	}
}
package edu.fjnu.cs.hwb.abstractfactory;

public class XiaoMiPod2 implements Pod 
{
	@Override
	public void play() 
	{
		System.out.println("this is XiaoMiPod2");
	}
}


好了,看看对应的工厂吧。首先是苹果公司的:

package edu.fjnu.cs.hwb.abstractfactory;

public class AppleFactory implements AbstractFactory 
{
	@Override
	public Phone getPhone(String name) 
	{		
		if ("IPhone4".equalsIgnoreCase(name))
			return new IPhone4();
		else if ("IPhone4s".equalsIgnoreCase(name))
			return new IPhone4s();
		return null;
	}

	@Override
	public Pod getPod(String name) 
	{
		if ("IPod1".equalsIgnoreCase(name))
			return new IPod1();
		else if ("IPod2".equalsIgnoreCase(name))
			return new IPod2();
		return null;
	}

}

之后是小米公司的:

package edu.fjnu.cs.hwb.abstractfactory;

public class XiaoMiFactory implements AbstractFactory 
{
	@Override
	public Phone getPhone(String name)
	{		
		if ("XiaoMi2".equalsIgnoreCase(name))
			return new XiaoMi2();
		else if ("XiaoMi2s".equalsIgnoreCase(name))
			return new XiaoMi2S();
		return null;
	}

	@Override
	public Pod getPod(String name)
	{
		
		if ("XiaoMiPod1".equalsIgnoreCase(name))
			return new XiaoMiPod1();
		else if ("XiaoMiPod2".equalsIgnoreCase(name))
			return new XiaoMiPod2();
		return null;
	}
}
看看客户端程序员怎么使用:

package edu.fjnu.cs.hwb.abstractfactory;

public class Client 
{
	public static void main(String[] args)
	{
		AbstractFactory abstractFactory = new AppleFactory();
		Phone phone = abstractFactory.getPhone("Iphone4");
		phone.call();
		phone = abstractFactory.getPhone("Iphone4s");
		phone.call();
		Pod pod = abstractFactory.getPod("Ipod1");
		pod.play();
		pod = abstractFactory.getPod("Ipod2");
		pod.play();
		
		System.out.println();		
		
		abstractFactory = new XiaoMiFactory();
		phone = abstractFactory.getPhone("XiaoMi2");
		phone.call();
		phone = abstractFactory.getPhone("XiaoMi2s");
		phone.call();
		pod = abstractFactory.getPod("XiaoMiPod1");
		pod.play();
		pod = abstractFactory.getPod("XiaoMiPod2");
		pod.play();
	}
}
结果如下:


bubuko.com,布布扣


好了..代码虽然多,但是很简单的..

如果有错误,希望大家指正出来


设计模式之四 抽象工厂模式,布布扣,bubuko.com

设计模式之四 抽象工厂模式

原文:http://blog.csdn.net/hwb1992/article/details/22760185

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!