抽象工厂模式

Posted 七月蜀葵

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了抽象工厂模式相关的知识,希望对你有一定的参考价值。

思考:工厂方法模式:http://www.cnblogs.com/maggiejyt/p/7561253.html

工厂方法模式UML:

问题:如果这家工厂不止要生产Ball(球),还要还有Sneakers(球鞋)等

则UML图为

当Product有多种类时则是抽象工厂模式

代码(Ball的代码见简单工厂模式:http://www.cnblogs.com/maggiejyt/p/7561253.html

Sneakers(球鞋抽象类)

package com.maggie.FactoryMethod;

public  abstract  class Sneakers {
     private String name;

    public Sneakers(String name) {
         super ();
         this .name = name;
    }

    public Sneakers() {
         super ();
    }

    public String getName() {
         return name;
    }

    public  void setName(String name) {
         this .name = name;
    }
    
    public  abstract  void show();

}

 

FootSneakers(足球鞋)

package com.maggie.FactoryMethod;

public  class FootSneakers extends Sneakers {

    @Override
    public  void show() {
        String show = "the sneakers is " + this .getName();
        System.out.println(show);

    }

    public FootSneakers() {
         super ();
    }

    public FootSneakers(String name) {
         super (name);
    }

}

BasketSneakers(篮球鞋)

package com.maggie.FactoryMethod;

public  class BasketSneakers extends Sneakers {

    @Override
    public  void show() {
        String show = "the sneakers is "+ this .getName();
        System.out.println(show);
    }
    public BasketSneakers() {
        super();
    }

    public BasketSneakers(String name) {
        super(name);
    }
}

IFactory(工厂接口)

package com.maggie.FactoryMethod;

public interface IFactory {
public abstract Ball createBall();
public abstract Sneakers createSneakers();
}

FootSneakers(足球工厂)

package com.maggie.FactoryMethod;

public class FootSneakers extends Sneakers {

    @Override
    public void show() {
        String show = "the sneakers is " + this.getName();
        System.out.println(show);

    }

    public FootSneakers() {
        super();
    }

    public FootSneakers(String name) {
        super(name);
    }

}

 BasketFactory(篮球工厂)

package com.maggie.FactoryMethod;

public class BasketFactory implements IFactory {

    @Override
    public Ball createBall() {
        // TODO Auto-generated method stub
        return new BasketBall("oranger");
    }

    @Override
    public Sneakers createSneakers() {
        return new BasketSneakers("basketball sneakers");
    }

}

 

客户端调用

package com.maggie.FactoryMethod;

public class Client {

    public static void main(String[] args) {
        IFactory iFactory = null;
        
        iFactory = new FootFactory();
        Ball footBall = iFactory.createBall();
        Sneakers footSneakers = iFactory.createSneakers();
        footBall.play();
        footSneakers.show();
        
        iFactory = new BasketFactory();
        Ball basketBall = iFactory.createBall();
        Sneakers basketSneakers = iFactory.createSneakers();
        basketBall.play();
        basketSneakers.show();
        
        
    }

}

结果输出:

the ball\'s color is white+black,I am FootBall
the sneakers is football sneakers
the ball\'s color is oranger,I am BasketBall
the sneakers is basketball sneakers

 

思考:

每次增加一种产品类,就要改动各种Factory的代码。。。。。

 

采用的是用简单工厂来改进抽象工厂

代码实现

SportsEquipmentFactory(采用简单工厂模式来改进)

package com.maggie.FactoryMethod;

public class SportsEquipmentFactory {
    private static final String STYLE = "basket";
    // private static final String STYLE="foot";只需要更改这里的代码配置

    public static Ball createBall() {
        Ball ball = null;
        switch (STYLE) {
        case "basket":
            ball = new BasketBall("oranger");
            break;
        case "foot":
            ball = new FootBall("white+black");
            break;
        }
        return ball;
    }

    public static Sneakers createSneakers() {
        Sneakers sneakers = null;
        switch (STYLE) {
        case "basket":
            sneakers = new BasketSneakers("basketball sneakers");
            break;
        case "foot":
            sneakers = new FootSneakers("football sneakers");
            break;
        }
        return sneakers;
    }
}

客户端调用

package com.maggie.FactoryMethod;

public class Client {

    public static void main(String[] args) {
        Ball ball = SportsEquipmentFactory.createBall();
        ball.play();
        Sneakers sneakers = SportsEquipmentFactory.createSneakers();
        sneakers.show();

    }

}

输出

the ball\'s color is oranger,I am BasketBall
the sneakers is basketball sneakers

 

 

思考:如果业务又做大,工厂又需要生产网球类的产品,则需要在SportsEquipmentFactory的switch中增加case,这又违反了开放-封闭原则

这时就要考虑反射。

 

以上是关于抽象工厂模式的主要内容,如果未能解决你的问题,请参考以下文章

设计模式学习——简单工厂模式工厂模式抽象工厂模式

设计模式 创建者模式 工厂设计模式 -- 抽象工厂设计模式介绍和实现

设计模式:抽象工厂模式

设计模式11:抽象工厂模式

C++工厂模式(简单工厂工厂方法抽象工厂)

C++工厂模式(简单工厂工厂方法抽象工厂)