设计模式--简单工厂
来源:互联网 发布:r语言mac版区别 编辑:程序博客网 时间:2024/05/13 14:24
设计模式--简单工厂
设计模式--简单工厂
创建Apple产品:
package SimpleFactory;/** * 类说明 * * <pre> * Modify Information: * Author Date Description * ============ =========== ============================ * DELL 2017年2月4日 Create this file * </pre> * */public class Apple implements Fruit{ /* * 简单工程模式 */ public void get() { System.out.println("get Apple"); }}
创建Banana:
package SimpleFactory;/** * 类说明 * * <pre> * Modify Information: * Author Date Description * ============ =========== ============================ * DELL 2017年2月4日 Create this file * </pre> * */public class Banana implements Fruit { public void get() { System.out.println("get Banana"); }}
水果接口:
package SimpleFactory;/** * 类说明 * * <pre> * Modify Information: * Author Date Description * ============ =========== ============================ * DELL 2017年2月4日 Create this file * </pre> * */public interface Fruit { /* * 采集方法 */ public void get();}
水果工厂:
package SimpleFactory;/** * 类说明 * * <pre> * Modify Information: * Author Date Description * ============ =========== ============================ * DELL 2017年2月4日 Create this file * </pre> * */public class FruitFactory { /* * 获得Apple实例 */ public static Fruit getApple() { return new Apple(); } public static Fruit getBanana() { return new Banana(); } public static Fruit getFruit(String type) throws InstantiationException, IllegalAccessException, ClassNotFoundException {// if (type.equalsIgnoreCase("apple")) {// return Apple.class.newInstance();// } else if (type.equalsIgnoreCase("banana")) {// return Banana.class.newInstance();// } else {// System.out.println("找不到对应都实体类");// }// return null; Class fruit = Class.forName(type); return (Fruit)fruit.newInstance(); }}
测试类:
package SimpleFactory;/** * 类说明 * * <pre> * Modify Information: * Author Date Description * ============ =========== ============================ * DELL 2017年2月4日 Create this file * </pre> * */public class MainClass { /* * 多态的形式 接口进行引用 */ public static void main(String[] args) throws InstantiationException, IllegalAccessException, ClassNotFoundException { /* Fruit apple = new Apple(); Fruit banana = new Banana(); apple.get(); banana.get();*/ /* * 简单工厂方法 * 可以根据工厂去创建自己需要的产品 * 下面都getFruit 就是生产水果产品用的,但是在某种程度上是不好的,增加一个产品就要增加getFruit方法 */ /* Fruit apple = new FruitFactory().getApple(); Fruit banana = new FruitFactory().getBanana();*/// Fruit apple = FruitFactory.getApple();// Fruit banana = FruitFactory.getBanana();// Fruit apple = FruitFactory.getFruit("apple");// apple.get();// Fruit apple = FruitFactory.getFruit("Test.SimpleFactory.Apple");// apple.get(); } }
0 0
- 简单工厂设计模式
- 设计模式-----简单工厂
- 简单工厂设计模式
- 设计模式:简单工厂
- 设计模式 - 简单工厂
- 设计模式 简单工厂
- 【设计模式】【简单工厂】
- 简单工厂设计模式
- 简单工厂设计模式
- 设计模式--简单工厂
- 简单工厂设计模式
- 简单工厂设计模式
- 设计模式------------简单工厂
- 设计模式--简单工厂
- 简单工厂设计模式
- 设计模式---------简单工厂
- 设计模式--简单工厂
- 简单工厂设计模式
- Unity之在UI界面上显示3D模型
- 好久不见 HELLOWORLD
- 嵌入式Linux学习:u-boot源码分析(6)--AM335X系列的2014.10版
- 带动效的环形进度图
- c#内存分配
- 设计模式--简单工厂
- MessageQueue 队列设计(待续)
- 按钮 自定义圆角 按下效果
- HDU3236:Gift Hunting(类01背包加强版)
- Codeforces Round #395 (Div. 2) D. Timofey and rectangles
- P3374 树状数组 1
- python编辑器对比和推荐
- double类型数据做加和操作时会丢失精度问题处理
- eclipse修改android SDK路径