第七个设计模式:抽象工厂

第七个设计模式:抽象工厂

工厂方法解决了一个产品等级结构的创建,那抽象工厂则是解决多个产品等级结构的创建方案.是更为抽象的方案.

UML示意图:

第七个设计模式:抽象工厂_第1张图片

组成部分:

1.       抽象工厂

2.       具体工厂

3.       抽象产品

4.       具体产品

代码例子:

1.       抽象工厂

package com.eekq.creator.abstractfactory;

 

public interface Creator {

    public ProductA factoryA();

    public ProductB factoryB();

}

2.       抽象产品

package com.eekq.creator.abstractfactory;

 

public interface ProductA {

    //业务方法

    public void doWorkA();

}

 

package com.eekq.creator.abstractfactory;

 

public interface ProductB {

    //业务方法

    public void doWorkB();

}

 

3.       具体工厂

package com.eekq.creator.abstractfactory.impl;

 

import com.eekq.creator.abstractfactory.Creator;

import com.eekq.creator.abstractfactory.ProductA;

import com.eekq.creator.abstractfactory.ProductB;

 

public class ConcreteCreator1 implements Creator {

 

    public ProductA factoryA() {

       return new ProductA1();

    }

 

    public ProductB factoryB() {

       return new ProductB1();

    }

 

}

package com.eekq.creator.abstractfactory.impl;

 

import com.eekq.creator.abstractfactory.Creator;

import com.eekq.creator.abstractfactory.ProductA;

import com.eekq.creator.abstractfactory.ProductB;

 

public class ConcreteCreator2 implements Creator {

 

    public ProductA factoryA() {

       return new ProductA2();

    }

 

    public ProductB factoryB() {

       return new ProductB2();

    }

 

}

4.       具体产品

package com.eekq.creator.abstractfactory.impl;

 

import com.eekq.creator.abstractfactory.ProductA;

 

public class ProductA1 implements ProductA {

 

    public void doWorkA() {

       System.out.println("ProductA1.doWorkA()");

    }

 

}

package com.eekq.creator.abstractfactory.impl;

 

import com.eekq.creator.abstractfactory.ProductA;

 

public class ProductA2 implements ProductA {

 

    public void doWorkA() {

       System.out.println("ProductA2.doWorkA()");

    }

 

}

package com.eekq.creator.abstractfactory.impl;

 

import com.eekq.creator.abstractfactory.ProductB;

 

public class ProductB1 implements ProductB {

 

    public void doWorkB() {

       System.out.println("ProductB1.doWorkB()");

    }

 

}

package com.eekq.creator.abstractfactory.impl;

 

import com.eekq.creator.abstractfactory.ProductB;

 

public class ProductB2 implements ProductB {

 

    public void doWorkB() {

       System.out.println("ProductB2.doWorkB()");

    }

 

}

5.客户端的调用

package com.eekq.creator.abstractfactory;

 

import com.eekq.creator.abstractfactory.impl.ConcreteCreator1;

import com.eekq.creator.abstractfactory.impl.ConcreteCreator2;

 

public class Client {

 

    public static void main(String[] args) {

       Creator c1 = new ConcreteCreator1();

       Creator c2 = new ConcreteCreator2();

       ProductA a1 = c1.factoryA();

       ProductA a2 = c2.factoryA();

       ProductB b1 = c1.factoryB();

       ProductB b2 = c2.factoryB();

       a1.doWorkA();

       a2.doWorkA();

       b1.doWorkB();

       b2.doWorkB();

    }

 

}

6. 输出结果

ProductA1.doWorkA()

ProductA2.doWorkA()

ProductB1.doWorkB()

ProductB2.doWorkB()

7.总结

抽象工厂是工厂方法的一种扩充,同样有着工厂方法的优点.工厂方法每个具体工厂对应一个具体产品,而抽象工厂则对应同一产品等级中的多个具体产品.前提条件是预先设计好产品的等级结构.

作者:飞行鱼 QQ6868861 推荐J2EE群:7715552

你可能感兴趣的:(设计模式,产品,class,interface,import,string,设计模式)