Java设计模式(一)普通工场模式 抽象工场模式

设计模式

设计模式是前人总结的,为了解决一类问题而总结的代码设计经验。

最初可能为了使用而使用,后面就会发现。非常多没想到的问题由于使用了正确的设计模式已经为你考虑到了。《design patterns设计模式》这本书是程序猿进阶必学。

(一)工厂模式  

工厂模式的意义在于定义一个用于创建对象的接口,并控制返回哪个类的实例。网上比較流行的一个普通工厂模式的样例。

interface Sender{
	public void send();
}
class MainSender implements Sender{
	public void send (){
		System.out.println("MainSender");
	}
}
class SnsSender implements Sender{
	public void send(){
		System.out.println("SnsSender");
	}
}
class SendFactory{
	public Sender product(String type){
		if("Main".equals(type)){
			return new MainSender();
		}else if("Sns".equals(type)){
			return new SnsSender();
		}else{
			System.out.println("请输入正确类型");
			return null;
		}
	}
}
public class Factory {
	public static void main(String[] args){
		SendFactory factory = new SendFactory();
		Sender sender = factory.product("Main");
		sender.send();
	}
}
可是个人感觉这个没啥用处。

在《java设计模式》中对于工厂模式的定义感觉比較好,工厂模式使得client代码无须关心使用哪个类的实例,可是上面的样例须要调用者决定返回哪个类的实例。工厂模式不仅要求有一个可以创建新对象的方法,还要让客户代码无须了解详细实例化的类。返回的同样的抽象类型,但实际实例化了不同的类,由哪个类实例化取决于工厂对象接收创建请求时的行为。

所以我的理解是,工厂模式的意图是让服务的提供者确定实例化哪个类。而不是客户代码。改动下上面的代码。

interface Sender{
	public void send();
}
class MainSender implements Sender{
	public void send (){
		System.out.println("MainSender");
	}
}
class SnsSender implements Sender{
	public void send(){
		System.out.println("SnsSender");
	}
}
class SendFactory{
	public int day = 0;
	public SendFactory(){
		day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
	}
	public Sender product(){
		if(day%2 == 0){
			return new MainSender();
		}else{
			return new SnsSender();
		}
	}
}
public class Factory {
	public static void main(String[] args){
		SendFactory factory = new SendFactory();
		Sender sender = factory.product();
		sender.send();
	}
}
(二)抽象工厂模式

抽象工厂相比于上面的工厂模式。多了一个抽象接口就是,上面的工厂类是无法扩展的,假设要扩展必须改动工厂类。违背闭包原则。

假设将工厂也抽象出来就是一个抽象工厂模式,须要扩展时候直接扩展一个新的工厂。

长处:1、封装性好,产品实现类高层模块不关系。仅仅要找到合适的工厂类。

2、产品族内约束为非公开。

缺点:扩展困难,加入一个新工厂。并且新工厂须要一个新接口,那么须要改动抽象类接口。并且前面的每一个工厂也要对应的改动。

所以对于抽象工厂模式横向扩展简单,纵向扩展困难。

在使用的时候可以注意。

interface Sender{
	public void send();
}
interface Provider{
	public Sender product();
}
class MainSender implements Sender{
	public void send (){
		System.out.println("MainSender");
	}
}
class SnsSender implements Sender{
	public void send(){
		System.out.println("SnsSender");
	}
}
class SendMainFactory implements Provider{
	public int day = 0;
	public SendMainFactory(){
		day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
	}
	public Sender product(){
		if(day%2 == 0){
			return new MainSender();
		}
		return null;
	}
}
class SendSnsFactory implements Provider{
	public int day = 0;
	public SendSnsFactory(){
		day = Calendar.getInstance().get(Calendar.DAY_OF_YEAR);
	}
	public Sender product(){
		if(day%2 == 0){
			return new SnsSender();
		}
		return null;
	}
}
public class Factory {
	public static void main(String[] args){
		Provider factory = new SendSnsFactory();
		Sender sender = factory.product();
		sender.send();
	}
}
上面的样例,假设添加一个新工厂仅仅要新建一个工厂继承Provider 。扩展非常easy。可是假设须要添加Provider的接口。那么
SendMainFactory
SendSnsFactory
也要对应改动。






【推广】 免费学中医,健康全家人
原文地址:https://www.cnblogs.com/ldxsuanfa/p/10770715.html