工厂模式(二)
来源:互联网 发布:电气控制图绘制软件 编辑:程序博客网 时间:2024/04/30 13:17
说一下工厂模式,先看一下图:
简单来说就是我需要一个对象,new出来,具体创建过程在子类
我这里模仿了一下建造手机的过程
首先有手机这个概念,然后工厂进行具体品牌进行创建
概念:
package com.fanyafeng.factory.factory;/** * Created by fanyafeng on 16/7/1. */public interface MobilePhone { public void create();}再看一下三种手机
package com.fanyafeng.factory.mobilephone;import com.fanyafeng.factory.factory.MobilePhone;/** * Created by fanyafeng on 16/7/1. */public class HuaWei implements MobilePhone{ @Override public void create() { System.out.println("手机名称:华为"); System.out.println("--系统:ios"); System.out.println("--材料:塑料+金属"); System.out.println("--UI:EUI"); System.out.println("--存储:内部,外部"); }}
package com.fanyafeng.factory.mobilephone;import com.fanyafeng.factory.factory.MobilePhone;/** * Created by fanyafeng on 16/7/1. */public class Iphone implements MobilePhone { @Override public void create() { System.out.println("手机名称:苹果"); System.out.println("--系统:ios"); System.out.println("--材料:塑料+金属"); System.out.println("--屏幕:视网膜屏"); }}
package com.fanyafeng.factory.mobilephone;import com.fanyafeng.factory.factory.MobilePhone;/** * Created by fanyafeng on 16/7/1. */public class Mi implements MobilePhone{ @Override public void create() { System.out.println("手机名称:小米"); System.out.println("--系统:ios"); System.out.println("--材料:塑料+金属"); System.out.println("--UI:MIUI"); System.out.println("--Remote:红外线"); }}
看一下工厂
package com.fanyafeng.factory.factory;import com.fanyafeng.factory.mobilephone.HuaWei;import com.fanyafeng.factory.mobilephone.Iphone;import com.fanyafeng.factory.mobilephone.Mi;/** * Created by fanyafeng on 16/7/1. */public class MobilePhoneFactory { public MobilePhone getMobilePhone(String mobilePhone) { switch (mobilePhone) { case "Iphone": return new Iphone(); case "HuaWei": return new HuaWei(); case "Mi": return new Mi(); default: return null; } }}我感觉这个return null不是很好,感觉这个null不是很受欢迎
package com.fanyafeng.factory;import com.fanyafeng.factory.factory.MobilePhone;import com.fanyafeng.factory.factory.MobilePhoneFactory;import com.fanyafeng.factory.mobilephone.HuaWei;import com.fanyafeng.factory.mobilephone.Iphone;/** * Created by fanyafeng on 16/7/1. */public class Main { public static void main(String[] args) { MobilePhoneFactory mobilePhoneFactory=new MobilePhoneFactory(); MobilePhone iphone=mobilePhoneFactory.getMobilePhone("Iphone"); iphone.create(); MobilePhone huawei=mobilePhoneFactory.getMobilePhone("HuaWei"); huawei.create(); MobilePhone mi=mobilePhoneFactory.getMobilePhone("Mi"); mi.create(); }}
看一下打印:
0 0
- 工厂模式(二)
- 工厂模式(二)
- 工厂模式(二)--工厂方法模式
- 简单工厂模式(二)
- 简单工厂模式(二)
- 简单工厂模式(二)
- 工厂模式(简单工厂模式, 工厂方法模式, 抽象工厂模式)二
- 设计模式(二)工厂方法模式+抽象工厂模式
- 设计模式(二)工厂模式:简单工厂模式
- [设计模式](二):工厂模式(简单工厂|静态工程、工厂方法|多工厂、抽象工厂)
- 模式二(工厂方法模式)
- 设计模式(二)---工厂模式
- 设计模式-工厂模式(二)
- 设计模式(二)---工厂方法模式
- 设计模式(二):抽象工厂模式
- Java设计模式----工厂模式(二)
- 设计模式实践(二)工厂模式
- 工厂模式---设计模式(二)
- Could not load the "" image referenced from a nib in the bundle with identifier "com.****.***
- android-----事件分发机制测试系列(四)
- Keystore was tampered with, or password was incorrect
- Unity3D IDE安装执行过程
- MySql 远程导出数据
- 工厂模式(二)
- hibernate示例-保存对象
- Android之四大组件之一-Activity(三)
- windows下PHP+Mysql+Apache环境搭建及软件下载
- Swift使用SnapKit给控件添加约束时出现的三个错误
- Git问题总结
- aop 切入点表达式示例
- java 使用enum(枚举)
- java网络编程:HttpClient 4.x版本的使用