说道设计模式,最先讲到的设计模式必然是工厂系列的设计模式。下面我们就来看下工厂系列设计模式:
简单工厂模式
简单工厂模式严格来说都不是一种设计模式,只是工厂方法模式的一个简单实现,但是我们平时运用最多就是这个简单工厂模式。
简单工厂模式通常用在固定且不大会改变的类族的创建。举个简单的例子,我们现在有个生化实验室来克隆人,所以我们要有一个人的模具
人是一个父类:
public class Ren {}
Ren这个父类有两个子类:男人和女人
public class NanRen extends Ren {}
public class NvRen extends Ren {}
所以我们可以有一个人工厂来分别造出男人和女人(具体怎么造请自行想象,我只是一个宅男程序猿,不是H文作者)。
class RenFactory { public Ren createRen(String gender) { Ren r = null; if (gender == "male") { r = new NanRen(); } else if (gender == "female") { r = new NvRen(); } return r; }}
这是我们的实验室就能很轻松的制造出男人或者女人了
public class Laboratory { public static void main(String[] args) { RenFactory rf = new RenFactory(); Ren nan = rf.createRen("male"); Ren nv = rf.createRen("female"); }}
上面就是一个简单工厂模式的简单例子。以下是具体全部代码
class Ren {}class NanRen extends Ren {}class NvRen extends Ren {}class RenFactory { public Ren createRen(String gender) { Ren r = null; if (gender == "male") { r = new NanRen(); } else if (gender == "female") { r = new NvRen(); } return r; }}public class Laboratory { public static void main(String[] args) { RenFactory rf = new RenFactory(); Ren nan = rf.createRen("male"); Ren nv = rf.createRen("female"); }}
工厂方法模式
上面说到了简单工厂方法,下面我们继续我们的例子来说明工厂方法模式。
现在我们的实验室不仅仅要男人和女人,我们还兼并了泰国的某个不知名实验室,我们还生产人妖,所以我们要安装新的人妖生产线,还需要更新我们的工厂系统。以下是我们的一个更新方案。
class RenYao extends Ren { }class RenFactory { public Ren createRen(String gender) { Ren r = null; if (gender == "male") { r = new NanRen(); } else if (gender == "female") { r = new NvRen(); } else if (gender == "renyao") { r = new RenYao(); } return r; }}public class Laboratory { public static void main(String[] args) { RenFactory rf = new RenFactory(); Ren nan = rf.createRen("male"); Ren nv = rf.createRen("female"); Ren ry = rf.createRen("renyao"); } }
但是此时我们的资深顾问提出了不同意见,现在我们兼并泰国的实验室,我们需要一个人妖的生产线可以从泰国实验室直接拿来用,但是我们的工厂系统却也要更改,万一那一部分错误,我们以前能制造的男人和女人也会出问题阿。况且听说上层正在谈收购太阳实验室的事情,那么到时候我们岂不是又得更新我们的工厂系统了! 我们何不再多造几个工厂来造不同的人,于是我们实验室又提出了以下的方案:
创建一个工厂的样板间以便其他工厂有个参照物
interface Factory { public abstract Ren createRen(String gender);}
保留我们以前生产正常人的工厂
class RenFactory implements Factory{ public Ren createRen(String gender) { Ren r = null; if (gender == "male") { r = new NanRen(); } else if (gender == "female") { r = new NvRen(); } return r; }}
再新建一座可以生产人妖的人妖工厂
class RenYaoFactory implements Factory{ public Ren createRen(String gender) { Ren r = new RenYao(); //人妖是不在乎性别的 return r; }}
所以当我们实验室就可以很顺利的经行生产了
public class Laboratory { public static void main(String[] args) { Factory rf = new RenFactory(); Ren nan = rf.createRen("male"); Ren nv = rf.createRen("female"); Factory ryf = new RenYaoFactory(); Ren ry = ryf.createRen(null); }}
并且在一段时候后我们实验室成功兼并了太阳实验室,并在太阳当地设置了我们的兽人工厂。
class Ren {}class NanRen extends Ren {}class NvRen extends Ren {}class RenYao extends Ren {}class ShouRen extends Ren {}interface Factory { public abstract Ren createRen(String gender);}class RenFactory implements Factory { public Ren createRen(String gender) { Ren r = null; if (gender == "male") { r = new NanRen(); } else if (gender == "female") { r = new NvRen(); } return r; }}class RenYaoFactory implements Factory { public Ren createRen(String gender) { Ren r = new RenYao(); // 人妖是不在乎性别的 return r; }}class ShouRenFactory implements Factory { public Ren createRen(String gender) { Ren r = new ShouRen(); // 兽人是虽然是有性别的,但是我们分不清~ return r; }}public class Laboratory { public static void main(String[] args) { Factory rf = new RenFactory(); Ren nan = rf.createRen("male"); Ren nv = rf.createRen("female"); Factory ryf = new RenYaoFactory(); Ren ry = ryf.createRen(null); Factory srf = new ShouRenFactory(); Ren sr = srf.createRen(null); }}
抽象工厂模式
自从我们成功兼并泰国和太阳两家实验室,我们实验室反而陷入了亏损的泥潭,这只能说这是一个疯狂的市场,因为我们的客户不满足我们出售的各种赤果果的人,竟然还想让他们穿上衣服。为了我们伟大的市场,我们实验室只能再次收购了衣服加工厂,并且根据生产人的经验,我们分别为正常人,人妖,兽人建立了不同的衣服厂。
class YiFu {}class NanRenYiFu extends YiFu {}class NvRenYiFu extends YiFu {}class RenYaoYiFu extends YiFu {}class ShouRenYiFu extends YiFu {}interface YiFuFactory { public abstract YiFu createYiFu(String gender);}class RenYiFuFactory implements YiFuFactory { public YiFu createYiFu(String gender) { YiFu yf = null; if (gender == "male") { yf = new NanRenYiFu(); } else if (gender == "female") { yf = new NvRenYiFu(); } return yf; }}class RenYaoYiFuFactory implements YiFuFactory { public YiFu createYiFu(String gender) { YiFu yf = new RenYaoYiFu(); return yf; }}class ShouRenYiFuFactory implements YiFuFactory { public YiFu createYiFu(String gender) { YiFu yf = new ShouRenYiFu(); return yf; }}
我们以为有着以前的经验,加个衣服厂肯定是简单的事情,但是当我们将我们的产品投放市场的时候,发现常常将人妖穿的衣服给兽人穿上了,兽人的衣服却给人妖穿上了。我们的特殊爱好客户完全受不了穿着齐X小短裙的兽人。
public class Laboratory { public static void main(String[] args) { Factory rf = new RenFactory(); Ren nan = rf.createRen("male"); Ren nv = rf.createRen("female"); //人妖穿上了兽人的露胸皮坎肩 Factory ryf = new RenYaoFactory(); Ren ry = ryf.createRen(null); YiFuFactory sryff = new ShouRenYiFuFactory(); YiFu ryyf = sryff.createYiFu(null); //兽人穿上了人妖的齐X小短裙 Factory srf = new ShouRenFactory(); Ren sr = srf.createRen(null); YiFuFactory ryyff = new RenYaoYiFuFactory(); YiFu sryf = ryyff.createYiFu(null); }}
为了解决问题,我们的资深顾问提出了新的建议:我们再新建几座组装工厂。
interface ZuZhuangFactory { public abstract Factory createFactory(); public abstract YiFuFactory createYiFuFactory();}class RenZuZhuangFactory implements ZuZhuangFactory { public Factory createFactory() { return new RenFactory(); } public YiFuFactory createYiFuFactory() { return new RenYiFuFactory(); }}class RenYaoZuZhuangFactory implements ZuZhuangFactory { public Factory createFactory() { return new RenYaoFactory(); } public YiFuFactory createYiFuFactory() { return new RenYaoYiFuFactory(); }}class ShouRenZuZhuangFactory implements ZuZhuangFactory { public Factory createFactory() { return new ShouRenFactory(); } public YiFuFactory createYiFuFactory() { return new ShouRenYiFuFactory(); }}
现在实验室就能正确的生产人和衣服也套件了
public class Laboratory { public static void main(String[] args) { ZuZhuangFactory zzf = new RenZuZhuangFactory(); Factory rf = zzf.createFactory(); YiFuFactory ryff = zzf.createYiFuFactory(); Ren nan = rf.createRen("male"); Ren nv = rf.createRen("female"); YiFu nanyf = ryff.createYiFu("male"); YiFu nvyf = ryff.createYiFu("female"); // 人妖组装工厂里拿不到兽人的衣服 ZuZhuangFactory ryzzf = new RenYaoZuZhuangFactory(); Factory ryf = ryzzf.createFactory(); YiFuFactory ryyff = ryzzf.createYiFuFactory(); Ren ry = ryf.createRen(null); YiFu ryyf = ryyff.createYiFu(null); // 兽人组装工厂里拿不到人妖的齐X小短裙 ZuZhuangFactory srzzf = new ShouRenZuZhuangFactory(); Factory srf = srzzf.createFactory(); YiFuFactory sryff = srzzf.createYiFuFactory(); Ren sr = srf.createRen(null); YiFu sryf = sryff.createYiFu(null); }}
以下是全部源码
class Ren {}class NanRen extends Ren {}class NvRen extends Ren {}class RenYao extends Ren {}class ShouRen extends Ren {}interface Factory { public abstract Ren createRen(String gender);}class RenFactory implements Factory { public Ren createRen(String gender) { Ren r = null; if (gender == "male") { r = new NanRen(); } else if (gender == "female") { r = new NvRen(); } return r; }}class RenYaoFactory implements Factory { public Ren createRen(String gender) { Ren r = new RenYao(); // 人妖是不在乎性别的 return r; }}class ShouRenFactory implements Factory { public Ren createRen(String gender) { Ren r = new ShouRen(); // 兽人是虽然是有性别的,但是我们分不清~ return r; }}class YiFu {}class NanRenYiFu extends YiFu {}class NvRenYiFu extends YiFu {}class RenYaoYiFu extends YiFu {}class ShouRenYiFu extends YiFu {}interface YiFuFactory { public abstract YiFu createYiFu(String gender);}class RenYiFuFactory implements YiFuFactory { public YiFu createYiFu(String gender) { YiFu yf = null; if (gender == "male") { yf = new NanRenYiFu(); } else if (gender == "female") { yf = new NvRenYiFu(); } return yf; }}class RenYaoYiFuFactory implements YiFuFactory { public YiFu createYiFu(String gender) { YiFu yf = new RenYaoYiFu(); return yf; }}class ShouRenYiFuFactory implements YiFuFactory { public YiFu createYiFu(String gender) { YiFu yf = new ShouRenYiFu(); return yf; }}interface ZuZhuangFactory { public abstract Factory createFactory(); public abstract YiFuFactory createYiFuFactory();}class RenZuZhuangFactory implements ZuZhuangFactory { public Factory createFactory() { return new RenFactory(); } public YiFuFactory createYiFuFactory() { return new RenYiFuFactory(); }}class RenYaoZuZhuangFactory implements ZuZhuangFactory { public Factory createFactory() { return new RenYaoFactory(); } public YiFuFactory createYiFuFactory() { return new RenYaoYiFuFactory(); }}class ShouRenZuZhuangFactory implements ZuZhuangFactory { public Factory createFactory() { return new ShouRenFactory(); } public YiFuFactory createYiFuFactory() { return new ShouRenYiFuFactory(); }}public class Laboratory { public static void main(String[] args) { ZuZhuangFactory zzf = new RenZuZhuangFactory(); Factory rf = zzf.createFactory(); YiFuFactory ryff = zzf.createYiFuFactory(); Ren nan = rf.createRen("male"); Ren nv = rf.createRen("female"); YiFu nanyf = ryff.createYiFu("male"); YiFu nvyf = ryff.createYiFu("female"); // 人妖组装工厂里拿不到兽人的衣服 ZuZhuangFactory ryzzf = new RenYaoZuZhuangFactory(); Factory ryf = ryzzf.createFactory(); YiFuFactory ryyff = ryzzf.createYiFuFactory(); Ren ry = ryf.createRen(null); YiFu ryyf = ryyff.createYiFu(null); // 兽人组装工厂里拿不到人妖的齐X小短裙 ZuZhuangFactory srzzf = new ShouRenZuZhuangFactory(); Factory srf = srzzf.createFactory(); YiFuFactory sryff = srzzf.createYiFuFactory(); Ren sr = srf.createRen(null); YiFu sryf = sryff.createYiFu(null); }}
到这一步我们的市场基本就能稳定下来了,也需你也可以给我们的产品附送一顶帽子,试试看吧!
总结
简单工厂模式最简单,我们只有一个工厂类,根据不同的需求返回不同的产品实例。
工厂方法模式是我们有一系列类似的工厂,客户可以根据不同的需求构建不同的工厂来生产产品。
抽象工厂模式是工厂方法模式的进一步抽象:我们有一系列类似的工厂,这些工厂的产品是一些工厂族。