`
wangyanlong0107
  • 浏览: 482092 次
  • 性别: Icon_minigender_1
  • 来自: 沈阳
社区版块
存档分类
最新评论

【转】工厂方法factory method和抽象工厂模式abstract factory

 
阅读更多

1. 概念

    工厂方法:一抽象产品类派生出多个具体产品类;一抽象工厂类派生出多个具体工厂类;每个具体工厂类只能创建一个具体产品类的实例。

    即定义一个创建对象的接口(即抽象工厂类),让其子类(具体工厂类)决定实例化哪一个类(具体产品类)。“一对一”的关系。

 

    抽象工厂:多个抽象产品类,派生出多个具体产品类;一个抽象工厂类,派生出多个具体工厂类;每个具体工厂类可创建多个具体产品类的实例。

    即提供一个创建一系列相关或相互依赖对象的接口,而无需指定他们的具体的类。“一对多”的关系。

2. UML

 工厂方法:

 

 

抽象工厂:

3. 代码

工厂方法:

 

public interface Product
{       
}

public interface Creator
{
       public Product factory();
}

public class ConcreteProduct1 implements Product
{
       public ConcreteProduct1()
       {
              System.out.println("ConcreteProduct1被创建");
       }
}

public class ConcreteProduct2 implements Product
{
       public ConcreteProduct2()
       {
              System.out.println("ConcreteProduct2被创建");
       }
 
}

public class ConcreteCreator1 implements Creator
{
       public Product factory()
       {
              return new ConcreteProduct1();
       }
}

public class ConcreteCreator2 implements Creator
{
       public Product factory()
       {
              return new ConcreteProduct2();
       }
}
 
public class Client
{
       private static Creator creator1, creator2;
       private static Product prod1, prod2;

       public static void main(String[] args)
       {
              creator1 = new ConcreteCreator1();
              prod1 = creator1.factory();
              System.out.println("----------------------------");
              creator2 = new ConcreteCreator2();
              prod2 = creator2.factory();
       }
}

抽象工厂:

 

  • //抽象产品  
  • UpperClothes.java  
  • public abstract class UpperClothes {  
  •     public abstract int getChestSize();  
  •     public abstract int getHeight();  
  •     public abstract String getName();  
  • }  
  • Trousers.java  
  • public abstract class Trousers {  
  •     public abstract int getWaistSize();  
  •     public abstract int getHeight();  
  •     public abstract String getName();  
  • }  
  • //具体产品  
  • WesternUpperClothes.java  
  • public class WesternUpperClothes extends UpperClothes {  
  •     private int chestSize;  
  •     private int height;  
  •     private String name;  
  •     WesternUpperClothes(String name,int chestSize,int height){  
  •         this.name=name;  
  •         this.chestSize=chestSize;  
  •         this.height=height;  
  •     }  
  •     public int getChestSize() {  
  •         return chestSize;  
  •     }  
  •     public int getHeight() {  
  •         return height;  
  •     }  
  •     public String getName() {  
  •         return name;  
  •     }  
  • }  
  • CowboyUpperClothes.java  
  • public class CowboyUpperClothes extends UpperClothes {  
  •     private int chestSize;  
  •     private int height;  
  •     private String name;  
  •     CowboyUpperClothes(String name,int chestSize,int height){  
  •         this.name=name;  
  •         this.chestSize=chestSize;  
  •         this.height=height;  
  •     }  
  •     public int getChestSize() {  
  •         return chestSize;  
  •     }  
  •     public int getHeight() {  
  •         return height;  
  •     }  
  •     public String getName () {  
  •         return name;  
  •     }  
  • }  
  • WesternTrousers.java  
  • public class WesternTrousers extends Trousers {  
  •     private int waistSize;  
  •     private int height;  
  •     private String name;  
  •     WesternTrousers(String name,int waistSize,int height){  
  •         this.name=name;  
  •         this.waistSize=waistSize;  
  •         this.height=height;  
  •     }  
  •     public int getHeight() {  
  •         return height;  
  •     }  
  •     public String getName() {  
  •         return name;  
  •     }  
  •     public int getWaistSize() {  
  •         return waistSize;  
  •     }  
  • }  
  • CowboyTrousers.java  
  • public class CowboyTrousers extends Trousers {  
  •     private int waistSize;  
  •     private int height;  
  •     private String name;  
  •     CowboyTrousers(String name,int waistSize,int height){  
  •         this.name=name;  
  •         this.waistSize=waistSize;  
  •         this.height=height;  
  •     }  
  •     public int getHeight() {  
  •         return height;  
  •     }  
  •     public String getName() {  
  •         return name;  
  •     }  
  •     public int getWaistSize() {  
  •         return waistSize;  
  •     }  
  • }  
  • //抽象工厂  
  • ClothesFactory.java  
  • public abstract class ClothesFactory {  
  •     public abstract UpperClothes createUpperClothes(int chestSize,int height);  
  •     public abstract Trousers createTrousers(int waistSize,int height);  
  • }  
  • //具体工厂  
  • BeijingClothesFactory.java  
  • public class BeijingClothesFactory extends ClothesFactory {  
  •     public Trousers createTrousers(int waistSize, int height) {  
  •         return new WesternTrousers("北京牌裤子",waistSize,height);  
  •     }  
  •     public UpperClothes createUpperClothes(int chestSize, int height) {  
  •         return new WesternUpperClothes("北京牌上衣",chestSize,height);  
  •     }  
  • }  
  • ShanghaiClothesFactory.java  
  • public class ShanghaiClothesFactory extends ClothesFactory {  
  •     public Trousers createTrousers(int waistSize, int height) {  
  •         return new WesternTrousers("上海牌裤子",waistSize,height);  
  •     }  
  •     public UpperClothes createUpperClothes(int chestSize, int height) {  
  •         return new WesternUpperClothes("上海牌上衣",chestSize,height);  
  •     }  
  • }
//定义不同的产品之间的一定具备的标准,用interface实现 
//其中的method()方法可看作提取出不同产品的共性,如手机都有类似的功能 
interface IProductA{ 
  public void method(); 
} 

interface IProductB{ 
  public void method(); 
} 

//实现了产品标准实现的一系列具体产品 
//由于已经设计好A1由厂商1生产,故以下输出代码有“厂商x” 
class ProductA1 implements IProductA{ 
  public void method() { 
    System.out.println("厂商1    生产ProductA1 ..."); 
  } 
} 

class ProductA2 implements IProductA{ 
  public void method() { 
    System.out.println("厂商2    生产ProductA2 ..."); 
  } 
} 

class ProductB1 implements IProductB{ 
  public void method() { 
    System.out.println("厂商1    生产ProductB1 ..."); 
  } 
} 

class ProductB2 implements IProductB{ 
  public void method() { 
    System.out.println("厂商2    生产ProductB2 ..."); 
  } 
} 

//每一种牌子的产品生产工厂,即不同的厂商负责自己牌子产品的生产 
abstract class Factory1{ 
  abstract IProductA getProductA1(); 
  abstract IProductB getProductB1(); 
} 

abstract class Factory2{ 
  abstract IProductA getProductA2(); 
  abstract IProductB getProductB2(); 
} 

//具体的工厂用来生产相关的产品 
class ConcreteFactory1 extends Factory1{ 
  public IProductA getProductA1() { 
    return new ProductA1(); 
  } 
  public IProductB getProductB1() { 
    return new ProductB1(); 
  } 
} 

class ConcreteFactoryB extends Factory2{ 
  public IProductA getProductA2() { 
    return new ProductA2(); 
  } 
  public IProductB getProductB2() { 
    return new ProductB2(); 
  } 
} 

//测试类 
public class Client { 
  public static void main(String[] args) { 
    //厂商1负责生产产品A1、B1 
    Factory1 factory1 = new ConcreteFactory1(); 
    IProductA productA1 = factory1.getProductA1(); 
    IProductB productB1 = factory1.getProductB1(); 
     
    productA1.method(); 
    productB1.method(); 
     
    //厂商2负责生产产品A2、B2 
    Factory2 factory2 = new ConcreteFactoryB(); 
    IProductA productA2 = factory2.getProductA2(); 
    IProductB productB2 = factory2.getProductB2(); 
     
    productA2.method(); 
    productB2.method(); 
  } 
}

4. 应用场景

工厂方法:

在以下情况下,适用于工厂方法模式:

(1) 当一个类不知道它所必须创建的对象的类的时候。

(2) 当一个类希望由它的子类来指定它所创建的对象的时候。

(3) 当类将创建对象的职责委托给多个帮助子类中的某一个,并且你希望将哪一个帮助子类是代理者这一信息局部化的时候。

抽象工厂:

(1) 一个系统不应当依赖于产品类实例如何被创建、组合和表达的细节,这对于所有形态的工厂模式都是重要的。

(2) 这个系统有多于一个的产品族,而系统只消费其中某一产品族。

(3) 同属于同一个产品族的产品是在一起使用的,这一约束必须在系统的设计中体现出来。

(4) 系统提供一个产品类的库,所有的产品以同样的接口出现,从而使客户端不依赖于实现。

分享到:
评论

相关推荐

    工厂方法和抽象工厂——Factory Method & Abstract Factory

    NULL 博文链接:https://chuanwang66.iteye.com/blog/1335230

    抽象工厂模式(Abstract Factory Pattern)

    理解头绪,然后接合简单工厂模式、工厂方法模式对工厂家族的了解,再加上抽象工厂模式的意图,头脑中差不多有一个雏型了吧。好了,咱们一起来分析一下。。 先把各个角色揪出来。 抽象工厂:虚拟的衣柜,它只是个概念...

    JAVA工厂模式

    GOF在《设计模式》一书中将工厂模式分为两类:工厂方法模式(Factory Method)与抽象工厂模式(Abstract Factory)。将简单工厂模式(Simple Factory)看为工厂方法模式的一种特例,两者归为一类。 两者皆可,在...

    Java设计模式之工厂模式(Factory)

    2. 工厂方法模式(Factory Method) 3. 抽象工厂模式(Abstract Factory) 这三种模式从上到下逐步抽象,并且更具一般性。还有一种分类法,就是将简单工厂模式看为工厂方法模式的一种特例,两个归为一类。下面是使用...

    24种设计模式与6大设计原则

    工厂方法模式[FACTORY METHOD PATTERN] 抽象工厂模式[ABSTRACT FACTORY PATTERN] 门面模式[FACADE PATTERN] 适配器模式[ADAPTER PATTERN] 模板方法模式[TEMPLATE METHOD PATTERN] 建造者模式[BUILDER PATTERN] 策略...

    浅析Python 简单工厂模式和工厂方法模式的优缺点

    前言 在《设计模式》一书中...接下来会通过例子对比简单工厂模式和工厂方法模式。 工厂意图 定义一个用于创建对象的接口,让子类决定实例化哪一个类。Factory Method 使一个类的实例化延迟到其子类。 别名 虚构造器(V

    01-制造工具的工厂模式(1).html

    工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ) 建造者模式( Builder ) 原型模式( Prototype ) 结构型模式包含了: 适配器模式( Adapter ) 装饰器模式( ...

    Java24种设计模式,Java24种设计模式,24种设计模式,学会了这24种设计模式,可以打遍天下无敌手,设计模式非常重要

    5、工厂方法模式FACTORY METHOD PATTERN 6、抽象工厂模式ABSTRACT FACTORY PATTERN 7、门面模式FACADE PATTERN 8、适配器模式ADAPTER PATTERN 9、模板方法模式TEMPLATE METHOD PATTERN 10、建造者模式BUILDER ...

    00-初探 Laravel 和其中的设计模式(3).html

    工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ) 建造者模式( Builder ) 原型模式( Prototype ) 结构型模式包含了: 适配器模式( Adapter ) 装饰器模式( ...

    java设计模式教程+源代码

    AbstractFactory ( 抽象工厂 ) FactoryMethod ( 工厂方法 ) Singleton ( 单态模式 ) Builder ( 建造者模式 ) Prototype ( 原型模式 ) Adapter ( 适配器模式 ) Bridge ( 桥接模式 ) Composite ( 组合模式 ) ...

    09-通过容器实现的外观模式(2).html

    工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ) 建造者模式( Builder ) 原型模式( Prototype ) 结构型模式包含了: 适配器模式( Adapter ) 装饰器模式( ...

    12-附录 1 设计模式的七大原则(1).html

    工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ) 建造者模式( Builder ) 原型模式( Prototype ) 结构型模式包含了: 适配器模式( Adapter ) 装饰器模式( ...

    C#版 24种设计模式

    工厂方法模式(Factory Method Pattern) 观察者模式(Observer Pattern) 建造者模式(Builder Pattern) 解释器模式(Interpreter Pattern) 命令模式(Command Pattern) 模板方法模式(Template Method Pattern) 桥接模式...

    java版本二十三种设计模式.zip

    - 工厂方法模式(Factory Method) - 抽象工厂模式(Abstract Factory) - 单例模式(Singleton) - 建造者模式(Builder) - 原型模式(Prototype) - 代理模式(Proxy) - 适配器模式(Adapter) - 装饰器模式(Decorator...

    设计模式之创建型模式

    本次代码只提供了创建型模式的应用代码和说明,包括:Singleton 单件,Abstract Factory 抽象工厂,Builder 生成器,Factory Method 工厂方法。 Singleton模式解决的是实体对象个数的问题。除了Singleton...

    11-回顾和总结(1).html

    工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ) 建造者模式( Builder ) 原型模式( Prototype ) 结构型模式包含了: 适配器模式( Adapter ) 装饰器模式( ...

    02-控制反转和服务容器(1).html

    工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ) 建造者模式( Builder ) 原型模式( Prototype ) 结构型模式包含了: 适配器模式( Adapter ) 装饰器模式( ...

    08-责任链和管道的协作(1).html

    工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ) 建造者模式( Builder ) 原型模式( Prototype ) 结构型模式包含了: 适配器模式( Adapter ) 装饰器模式( ...

    C#设计模式_设计模式_C#_

    工厂方法模式(Factory Method) 5. 原型模式(Prototype)结构型: 6. 适配器模式(Adapter Pattern) 7. 桥接模式(Bridge Pattern) 8. 装饰模式(Decorator Pattern) 9. 组合模式(Composite Pattern) 10. 外观模式...

    07-使用代理快速接入第三方库(1).html

    工厂方法模式( Factory Method ) 抽象工厂模式( Abstract Factory ) 单例模式( Singleton ) 建造者模式( Builder ) 原型模式( Prototype ) 结构型模式包含了: 适配器模式( Adapter ) 装饰器模式( ...

Global site tag (gtag.js) - Google Analytics