设计模式——简单工厂模式

一 为何要工厂模式

百度百科:替代new,避免代码中出现大量的new

but,为什么要替代new呢?new又简单又方便。。。。

下面通过代码讲解,原始的代码包括三个类,一个学生类,一个铅笔类,一个橡皮类,学生需要3支铅笔和2块橡皮。

1.1 用new的代码

public class Eraser {
	public Eraser(){
		System.out.println("制造橡皮");
	}
}

public class Pencil {
	public Pencil(){
		System.out.println("制造铅笔");
	}
}

public class Student {
	public static void main(String[] args){
		Pencil pencil1=new Pencil();
		Pencil pencil2=new Pencil();
		Pencil pencil3=new Pencil();
		
		Eraser eraser1=new Eraser();
		Eraser eraser2=new Eraser();
	}
}
上述为我们平时写的代码,看上去“简单”,“方便”。

1.2 需求发生改变

现在学校说所有学生只能用20cm的铅笔,5cm的橡皮,于是我们去改代码:

public class Eraser {
	int length;                  //step1
	public Eraser(int length){   //step2
		this.length=length;  //step3
		System.out.println("制造橡皮");
	}
}
public class Pencil {
	int length;                  //step4
	public Pencil(int length){   //step5
		this.length=length;  //step6
		System.out.println("制造铅笔");
	}
}

public class Student {
	public static void main(String[] args){
		Pencil pencil1=new Pencil(20);//step7
		Pencil pencil2=new Pencil(20);//step8
		Pencil pencil3=new Pencil(20);//step9

		Eraser eraser1=new Eraser(5);//step10
		Eraser eraser2=new Eraser(5);//step11
	}
}
发现总共改了11个地方,并且从头到尾,所有类都跟着需要改,并且客户必须传入确定的参数,或者说需要了解工厂的细节。

二 简单工厂

2.1 简单工厂实现上述逻辑

public abstract class Stationery {
	public Stationery(){
		
	}
}
public class Eraser extends Stationery {
	public Eraser(){
		System.out.println("制造橡皮");
	}
}
public class Pencil extends Stationery {
	public Pencil(){
		System.out.println("制造铅笔");
	}
}
public class Factory {
	public Stationery create Stationery(String type){
		switch (type) {
		case "pencil":
			return new Pencil();
		case "eraser":
			return new Eraser();
		default:
			break;
		}
		return null;
	}
}
public class Student {
	public static void main(String[] args){
		Factory factory=new Factory();
		Stationery pencil1=factory.createStationery("pencil");
		Stationery pencil2=factory.createStationery("pencil");
		Stationery pencil3=factory.createStationery("pencil");
		
		Stationery eraser1=factory.createStationery("eraser");
		Stationery eraser1=factory.createStationery("eraser");
	}
}
可以看到代码变多了,由原来的3个类变为现在的5个类。

2.2  需求发生了改变

还是和上面一样的改变,学校说所有学生只能用20cm的铅笔,5cm的橡皮,然后我们去改代码:

public abstract class Stationery {
	public Stationery(){
		
	}
}
public class Eraser extends Stationery {
	int length;                         //step1
	public Eraser(int length){          //step2
		this.length=length;         //step3
		System.out.println("制造橡皮");
	}
}
public class Pencil extends Stationery {
	int length;                  //step4
	public Pencil(int length){   //step5
		this.length=length;  //step6
		System.out.println("制造铅笔");
	}
}
public class Factory {
	public Stationery createStationery(String type){
		switch (type) {
		case "pencil":
			return new Pencil(20);//step7
		case "eraser":
			return new Eraser(5);//step8
		default:
			break;
		}
		return null;
	}
}

public class Student {
	public static void main(String[] args){
		Factory factory=new Factory();
		Stationery pencil1=factory.createStationery("pencil");
		Stationery pencil2=factory.createStationery("pencil");
		Stationery pencil3=factory.createStationery("pencil");
		
		Stationery eraser1=factory.createStationery("eraser");
		Stationery eraser2=factory.createStationery("eraser");
	}
}

总共修改8个地方,而上面没用简单工厂的代码需要修改11个地方,条件是现在我们只需要3支铅笔盒2块橡皮,假如我们需要300支铅笔,200支橡皮呢?没用简单工厂时,修改次数为6+300+200=506次,而用了简单工厂修改次数依然为8次!并且用了简单工厂后,需求发生改变时,只需要修改具体的类和工厂类,与客户无关以上可以看出简单工厂对代码响应需求改变的性能所带来的好处,简单总结,一方面,降低耦合性,另一方面能更好的应对改变


三 简单工厂分析

3.1 概念

简单工厂模式属于类的创新型模式,又叫静态工厂方法模式,是通过专门定义一个类来负责创建其他类的实例,被创建的实例通常都具有共同的父类。它的核心是有一个专门的类来负责创建实例的过程。

3.2 UML图

3.3  总结

工厂类是整个模式的关键所在。它包含必要的判断逻辑,能够根据外界给定的信息,决定究竟应该创建哪个具体类的对象。用户在使用时可以直接根据工厂类去创建所需的实例,而无需了解这些对象是如何创建以及如何组织的。有利于整个软件体系结构的优化。既然简单工厂如此强大,为何要有工厂方法?关于简单工厂模式的缺点,以及为何需要工厂方法见下一篇博客:http://blog.csdn.net/xiaowang627/article/details/61424082














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值