标签:img try log get 业务逻辑 一个 sdn catch 应用
工厂方法模式 Factory Method :(虚拟构造函数模式 Virtual Constructor,多态性工厂模式 Ploymorphic Facoty)
Define an interface for creating an object,but let subclasses decide which class to instantiate.Factory Method lets a class defer instantiation to subclasses.
定义一个创建产品对象的工厂接口。将实际创建性工作推迟到子类中。
抽象工厂角色(Creator):该角色是工厂方法模式的核心。与应用系统无关,不论什么在创建对象的工厂类必须实现这个接口。
package com.DesignPattern.Creational.FactoryMethod; public interface Creator { //工厂方法 //创建一个产品对象,其输入參数类型能够自行设置 public <T extends Product> T factory(Class<T> c); }
package com.DesignPattern.Creational.FactoryMethod; public class ConcreteCreator implements Creator { @Override public <T extends Product> T factory(Class<T> c) { Product product = null; try { product = (Product) Class.forName(c.getName()).newInstance(); } catch (Exception e) { e.printStackTrace(); } return (T) product; } }
package com.DesignPattern.Creational.FactoryMethod; public interface Product { //产品类的公共方法 public void method1(); public void method2(); }
package com.DesignPattern.Creational.FactoryMethod; public class ConcreteProduct implements Product { @Override public void method1() { System.out.println("handler ..."); //业务逻辑处理代码 } @Override public void method2() { System.out.println("handler ..."); //业务逻辑处理代码 } }
package com.DesignPattern.Creational.FactoryMethod; public class FactoryMethodDemo { public static void main(String[] args) { Creator creator = new ConcreteCreator(); Product product = creator.factory(ConcreteProduct.class); /* * 继续业务处理 */ } }
抽象工厂FruitGardener.java
package com.DesignPattern.Creational.FactoryMethod; public interface FruitGardener { public Fruit factory(); }
package com.DesignPattern.Creational.FactoryMethod; public interface Fruit { //生长 public void grow(); //收获 public void harvest(); //栽种 public void plant(); }
package com.DesignPattern.Creational.FactoryMethod; public class FruitAppleGardener implements FruitGardener { @Override public Fruit factory() { return new FruitApple(); } }
package com.DesignPattern.Creational.FactoryMethod; public class FruitGrapeGardener implements FruitGardener { @Override public Fruit factory() { return new FruitGrape(); } }
详细产品FruitApple.java
package com.DesignPattern.Creational.FactoryMethod; public class FruitApple implements Fruit { private int treeAge; public int getTreeAge() { return treeAge; } public void setTreeAge(int treeAge) { this.treeAge = treeAge; } @Override public void grow() { System.out.println("apple grow..."); } @Override public void harvest() { System.out.println("apple harvest..."); } @Override public void plant() { System.out.println("apple plant..."); } }
详细产品FruitGrape.java
package com.DesignPattern.Creational.FactoryMethod; public class FruitGrape implements Fruit { private boolean seedless; public boolean isSeedless() { return seedless; } public void setSeedless(boolean seedless) { this.seedless = seedless; } @Override public void grow() { System.out.println("Grape grow..."); } @Override public void harvest() { System.out.println("Grape harvest..."); } @Override public void plant() { System.out.println("Grape plant..."); } }
package com.DesignPattern.Creational.FactoryMethod; public class FruitClientDemo { public static void main(String[] args) { // 苹果园丁工厂 FruitGardener fruitGardener = new FruitAppleGardener(); // 通过工厂生产苹果 Fruit apple = fruitGardener.factory(); apple.plant(); apple.grow(); apple.harvest(); // 葡萄园丁工厂 fruitGardener = new FruitGrapeGardener(); // 通过工厂生产葡萄 Fruit grape = fruitGardener.factory(); grape.plant(); grape.grow(); grape.harvest(); } }
DesignPattern_Java:Factory Method Pattern
标签:img try log get 业务逻辑 一个 sdn catch 应用
原文地址:http://www.cnblogs.com/yxysuanfa/p/7071454.html