Java设计模式(2) -- 工厂方法
2009-08-17 21:00
555 查看
Factory Method
英文简单描述
Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Factory methods eliminate the need to bind application-specific classes into your code.The code only deals with interfaces; therefore it can work with any user-defined classes that implement the right interface.
How to
Product
defines the interface of objects the factory method creates.
ConcreteProduct
implements the Product interface.
Creator
declares the factory method, which returns an object of type Product.
ConcreteCreator
overrides the factory method to return an instance of a ConcreteProduct.
Known Cases
Used in Abstract Factory
Document Application
UML
工厂方法有三种变体:
(1)并行的类层次结构
(2)根据类型id,创建不同子类对象
(3)利用泛型来去除Creator子类
英文简单描述
Intent
Define an interface for creating an object, but let subclasses decide which class to instantiate.
Factory methods eliminate the need to bind application-specific classes into your code.The code only deals with interfaces; therefore it can work with any user-defined classes that implement the right interface.
How to
Product
defines the interface of objects the factory method creates.
ConcreteProduct
implements the Product interface.
Creator
declares the factory method, which returns an object of type Product.
ConcreteCreator
overrides the factory method to return an instance of a ConcreteProduct.
Known Cases
Used in Abstract Factory
Document Application
UML
工厂方法有三种变体:
(1)并行的类层次结构
public class Creator { public Product createProduct() { // factory method // default implement return new Product(); } } public class ConcreteCreator extends Creator { public Product createProduct() { return new ConcreteProduct(); } } public class Product { public void doSomething() { System.out.println("Default Implement"); } } public class ConcreteProduct extends Product { public void doSomething() { System.out.println("Concrete Implement"); } } public class TestFactoryMethod { public static void main(String args[]) { //根据不同条件,初始化不同的Creator Creator creator = new ConcreteCreator(); Product product = creator.createProduct(); product.doSomething(); } }
(2)根据类型id,创建不同子类对象
public interface IDBConnector { Connection createConnection(Map params); } public class MysqlConnector implements IDBConnector { public Connection createConnection(Map params) { System.out.println("Create Mysql Connection"); return null; } } ublic class OracleConnector implements IDBConnector { public Connection createConnection(Map params) { System.out.println("Create Mysql Connection"); return null; } } public class DBConnectorCreator { public final static String MYSQL = "mysql"; public final static String ORACLE = "oracle"; private static DBConnectorCreator instance = new DBConnectorCreator(); public static DBConnectorCreator getInstance() { return instance; } private DBConnectorCreator() {} public IDBConnector createDBConnector(String type) { IDBConnector connector = null; if (type.equals(MYSQL)) { connector = new MysqlConnector(); } else if (type.equals(ORACLE)) { connector = new OracleConnector(); } else { throw new RuntimeException("Unsupport type"); } return connector; }
(3)利用泛型来去除Creator子类
public class GenericCreator { public Product createProduct(Class<? extends Product> type) { Product product = null; try { product = type.newInstance(); } catch (InstantiationException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } return product; } } class TestGeneric { public static void main(String[] args) { GenericCreator creator = new GenericCreator(); Product product = creator.createProduct(ConcreteProduct.class); product.doSomething(); } }
相关文章推荐
- Java设计模式之二 —— 工厂方法
- java_23种设计模式之工厂方法模式
- Java设计模式之工厂方法(01)
- Java设计模式之工厂方法模式
- Java工厂方法的设计模式
- java设计模式之工厂方法模式
- java设计模式之工厂方法设计模式
- java设计模式之工厂方法模式
- Java设计模式圣经连载(02)-工厂方法(Factory Method)模式
- Java 23种设计模式之工厂方法模式
- JAVA设计模式创建型模式之工厂方法
- 设计模式(一)----- 工厂方法(FactoryMethod)----(JAVA版)
- java之设计模式工厂三兄弟之工厂方法模式
- 3.23种设计模式的Java版--Factory Method(工厂方法)
- Java设计模式之工厂方法模式
- Java设计模式之工厂方法模式
- Java设计模式之工厂方法、抽象工厂模式 程序 总结
- Java设计模式之工厂模式(简单工厂、工厂方法、抽象工厂)
- java 设计模式-创建模式之工厂方法模式1
- 浅谈JAVA设计模式之——工厂方法(FactoryMethod)