设计模式-----Simple Factory(简单工厂模式)
2012-09-09 00:00
645 查看
UML:
Console:
I am ProductA.
I am ProductB.
/** * Product Interface * @author MC * */ public interface Product { /** * Get Product Name */ public String getName(); } /** * Concrete Product A * @author MC * */ public class ProductA implements Product{ public String getName() { // TODO Auto-generated method stub return "I am ProductA."; } } /** * Concrete Product B * @author MC * */ public class ProductB implements Product{ public String getName() { // TODO Auto-generated method stub return "I am ProductB."; } } /** * Factory Class * @author MC * */ public class ProductCreatorFactory { public static Product createProduct(String type) { if(type.equalsIgnoreCase("A")){ return new ProductA(); } if(type.equalsIgnoreCase("B")){ return new ProductB(); } return null; } } public class SimpleFactoryTest { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub Product productA=ProductCreatorFactory.createProduct("A"); System.out.println(productA.getName()); Product productB=ProductCreatorFactory.createProduct("B"); System.out.println(productB.getName()); } }
Console:
I am ProductA.
I am ProductB.
相关文章推荐
- 设计模式之 简单工厂模式
- Java研究之学习设计模式-简单工厂模式详解
- 23种设计模式(2)-简单工厂模式
- c++设计模式之简单工厂模式、工厂方法模式、抽象工厂模式
- 【设计模式】简单工厂模式
- Java设计模式(一) 简单工厂模式不简单
- C#设计模式之简单工厂模式(Simple Factory)
- 设计模式—简单工厂模式
- Java设计模式(三):工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)
- 设计模式之简单工厂模式
- java设计模式(1):简单工厂模式(Simple Factory)
- php设计模式——简单工厂模式
- 设计模式-简单工厂模式
- 设计模式学习--------3.简单工厂模式学习
- 设计模式之Simple Factory(简单工厂模式)
- 设计模式-简单工厂模式
- 设计模式之简单工厂模式学习案例代码
- Java设计模式(一) 简单工厂模式不简单
- java设计模式(创建型)之简单工厂模式
- C++设计模式——简单工厂模式