一 为何要工厂模式
百度百科:替代new,避免代码中出现大量的new。
but,为什么要替代new呢?new又简单又方便。。。。
下面通过代码讲解,原始的代码包括三个类,一个学生类,一个铅笔类,一个橡皮类,学生需要3支铅笔和2块橡皮。
1.1 用new的代码
1.2 需求发生改变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(); } }
二 简单工厂现在学校说所有学生只能用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("制造铅笔"); } }
发现总共改了11个地方,并且从头到尾,所有类都跟着需要改,并且客户必须传入确定的参数,或者说需要了解工厂的细节。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 } }
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; } }
可以看到代码变多了,由原来的3个类变为现在的5个类。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"); } }
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.youkuaiyun.com/xiaowang627/article/details/61424082