26种设计模式之工厂模式
来源:互联网 发布:php查询mysql数据库 编辑:程序博客网 时间:2024/05/29 08:43
今天整理的工厂模式,借鉴与Java与模式一书
以一个农场为例,来实现一个简单的Demo
1.简单工厂模式(SimpleFactory)
假如说现在有一家农场,该农场先外提供水果,包括苹果和香蕉;
因为业务比较小,农场提供的苹果和香蕉都是装在一模一样的水果箱里的;
同时我有专门提供水果的工厂,把苹果和香蕉生产出来以后,直接装进水果箱,所以外界只知道是水果但是不知道是什么水果;
图解分析
水果箱
package com.lizhenbo.simplefactory;/** * 可以理解为水果箱 * 水果接口,所有的水果必须实现该接口(这里可以定义为抽象类也合适) * @author LIZHENBO */public interface IFruit { //收获 void harvest();}
苹果
package com.lizhenbo.simplefactory;/** * 具体的苹果类 * @author LIZHENBO */public class Apple implements IFruit { @Override public void harvest() { System.out.println("收获一个苹果!"); }}
香蕉
package com.lizhenbo.simplefactory;/** * 具体的水果类,香蕉 * @author LIZHENBO * */public class Banana implements IFruit { @Override public void harvest() { System.out.println("收获一根香蕉!"); }}
水果工厂
package com.lizhenbo.simplefactory;/** * 水果工厂,用来创建所有的水果 * @author LIZHENBO */public class FruitFactory { public static IFruit createFruit(int whichFruit){ IFruit fruit = null; switch (whichFruit) { case 0: fruit= new Apple();//创建苹果 break; case 1: fruit= new Banana();//创建香蕉 break; } return fruit; }}
测试类
package com.lizhenbo.simplefactory;public class SimpleFactoryTest { public static void main(String[] args) { IFruit fruit = FruitFactory.createFruit(0); IFruit fruit2 = FruitFactory.createFruit(1); fruit.harvest(); fruit2.harvest(); }}
运行结果:
2.工厂方法模式
后来农场的业务做大了,想把工厂的任务细分化,创建两个工厂;
苹果工厂专门生成苹果;
香蕉工厂专门生成香蕉
图解分析
水果箱同上
苹果同上
香蕉同上
水果工厂接口
package com.lizhenbo.factorymethod;/** * 水果工厂接口,所有的水果工厂必须实现该接口 * @author LIZHENBO */public interface IFruitFactory { IFruit createFruit();}
苹果工厂
package com.lizhenbo.factorymethod;/** * 苹果工厂来专门创建苹果的工厂 * @author LIZHENBO */public class AppleFactory implements IFruitFactory{ @Override public IFruit createFruit() { return new Apple(); }}
香蕉工厂
package com.lizhenbo.factorymethod;/** * 香蕉工厂,专门创建香蕉的工厂 * @author LIZHENBO * */public class BananaFactory implements IFruitFactory{ @Override public IFruit createFruit() { return new Banana(); }}
测试类
package com.lizhenbo.factorymethod;public class FactoryMethodTest { public static void main(String[] args) { IFruit fruit = new AppleFactory().createFruit(); IFruit fruit2 = new BananaFactory().createFruit(); fruit.harvest(); fruit2.harvest(); }}
运行结果
3.抽象工厂模式
假如老板想扩展业务,
在北方创建一个工厂生产北方的水果和蔬菜;
在南方创建一个工厂生产南方的水果和蔬菜;
图解分析
水果箱同上
苹果同上
香蕉同上
蔬菜箱
package com.lizhenbo.abstractfactory;/** * 蔬菜接口,所有的蔬菜都要实现该接口 * @author LIZHENBO * */public interface IVegetable { void harvest_Vegetable();}
北方蔬菜
package com.lizhenbo.abstractfactory;public class NorthVegetable implements IVegetable{ @Override public void harvest_Vegetable() { System.out.println("收获北方蔬菜!"); }}
南方蔬菜
package com.lizhenbo.abstractfactory;public class SouthVegetable implements IVegetable{ @Override public void harvest_Vegetable() { System.out.println("收获南方蔬菜!"); }}
工厂接口
package com.lizhenbo.abstractfactory;/** * 工厂接口,所有的工厂必须实现该接口 * @author LIZHENBO */public interface IFactory { IFruit createFruit(); IVegetable createVegetable();}
北方工厂
package com.lizhenbo.abstractfactory;/** * 北方工厂来专门创建苹果和北方蔬菜的工厂 * @author LIZHENBO */public class NorthFactory implements IFactory{ @Override public IFruit createFruit() { return new Apple(); } @Override public IVegetable createVegetable() { return new NorthVegetable(); }}
南方工厂
package com.lizhenbo.abstractfactory;/** * 南方水果蔬菜工厂,专门创建香蕉和南方蔬菜的工厂 * @author LIZHENBO * */public class SouthFactory implements IFactory{ @Override public IFruit createFruit() { return new Banana(); } @Override public IVegetable createVegetable() { return new SouthVegetable(); }}
测试类
package com.lizhenbo.abstractfactory;public class AbstractFactoryTest { public static void main(String[] args) { IFruit fruit = new NorthFactory().createFruit(); IFruit fruit2 = new SouthFactory().createFruit(); IVegetable vegetable = new NorthFactory().createVegetable(); IVegetable vegetable2 = new SouthFactory().createVegetable(); fruit.harvest_Fruit(); fruit2.harvest_Fruit(); vegetable.harvest_Vegetable(); vegetable2.harvest_Vegetable(); }}
运行结果
最后GitHub地址:https://github.com/zhenbo929/FactoryDemo
0 0
- 26种设计模式之工厂模式
- 26设计模式之工厂模式
- 设计模式之三种工厂模式
- 设计模式之三种工厂模式
- 设计模式之三种工厂模式
- 设计模式之---三种工厂模式
- 23种设计模式之工厂模式
- 23种设计模式之-工厂模式
- 23种设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 设计模式之---工厂模式
- 设计模式之工厂模式
- 设计模式之工厂模式
- 生成随机数方法解析
- MSP430F149小系统开发板实现RS232串口通信
- 查看类簇中的类(17)
- arm-linux交叉编译器制作
- 如何创建一个oracle新用户?
- 26种设计模式之工厂模式
- 阶乘因式分解(一)
- POJ2559 HDU1506 ZOJ1985 Largest Rectangle in a Histogram【堆栈】
- Windows下Redis的安装使用
- (IL2CPP下篇) Unity将来时:IL2CPP怎么用?
- 设计师都知道的网站(二)
- 31.整数中1出现的次数(从1到n整数中1出现的次数
- ajax学习笔记
- 终端属性结构体各变量的功能