其他分享
首页 > 其他分享> > 设计模式(三)---- 抽象工厂模式

设计模式(三)---- 抽象工厂模式

作者:互联网

  抽象工厂模式可以理解为根据需要创建相应的工厂,进而生产出对应的产品。

  接着上一篇文章,定义一个AbstractFactory 提供 BallFactory 和 StickFactory 中的生产方法。

 1、定义抽象工厂类

/**
 * @Author: guaniu
 * @Description: 抽象工厂类:生成Ball工厂和Stick工厂
 * @Date: Create in 9:51 2020/12/6
 * @Modified by
 */
public abstract class AbstractFactory {
    public abstract Ball getBall(String color);
    public abstract Stick getStick(String color);
}

2、改造BallFactory

/**
 * @Author: guaniu
 * @Description: BallFactory 继承自 AbstractFactory,同时有getBall() 和 getStick() 方法
 * @Date: Create in 9:44 2020/12/6
 * @Modified by
 */
public class BallFactory extends AbstractFactory {
    @Override
    public Ball getBall(String color) {
        if ("red".equalsIgnoreCase(color)){
            return new RedBall();
        }else if ("green".equalsIgnoreCase(color)){
            return new GreenBall();
        }else if ("black".equalsIgnoreCase(color)){
            return new BlackBall();
        }else {
            try {
                throw new Exception("No Such Type of Ball!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    @Override
    public Stick getStick(String color) {
        return null;
    }
}

3、StickFactory

/**
 * @Author: guaniu
 * @Description: StickFactory 同样继承自抽象工厂类AbstractFactory
 * @Date: Create in 9:44 2020/12/6
 * @Modified by
 */
public class StickFactory extends AbstractFactory {

    @Override
    public Ball getBall(String color) {
        return null;
    }

    @Override
    public Stick getStick(String color) {
        if ("red".equalsIgnoreCase(color)){
            return new RedStick();
        }else if ("green".equalsIgnoreCase(color)){
            return new GreedStick();
        }else if ("black".equalsIgnoreCase(color)){
            return new BlackStick();
        }else {
            try {
                throw new Exception("No Such Type of Stick!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

4、Stick产品

/**
 * @Author: guaniu
 * @Description: 定义的Stick类产品
 * @Date: Create in 9:47 2020/12/6
 * @Modified by
 */
public interface Stick {
    void playStick();
}

5、RedStick产品

/**
 * @Author: guaniu
 * @Description:
 * @Date: Create in 9:49 2020/12/6
 * @Modified by
 */
public class RedStick implements Stick{

    @Override
    public void playStick() {
        System.out.println("Playing RedStick!");
    }
}

6、GreenStick产品

/**
 * @Author: guaniu
 * @Description:
 * @Date: Create in 9:50 2020/12/6
 * @Modified by
 */
public class GreedStick implements Stick{

    @Override
    public void playStick() {
        System.out.println("Playing GreenStick!");
    }
}

7、BlackStick产品

/**
 * @Author: guaniu
 * @Description:
 * @Date: Create in 9:48 2020/12/6
 * @Modified by
 */
public class BlackStick implements Stick{

    @Override
    public void playStick() {
        System.out.println("Playing BlackStick!");
    }
}

8、生产Factory的工具类

/**
 * @Author: guaniu
 * @Description:
 * @Date: Create in 9:55 2020/12/6
 * @Modified by
 */
public class FactoryProducer {
    public static AbstractFactory getFactory(String type){
        if ("ball".equalsIgnoreCase(type)){
            return new BallFactory();
        }else if ("stick".equalsIgnoreCase(type)){
            return new StickFactory();
        }else {
            try {
                throw new Exception("No Such Type of Factory!");
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}

9、测试类

public class Main {
    public static void main(String[] args){
        String[] types = {"ball", "stick"};
        String[] colors = {"red", "black", "green"};
        Random random = new Random();
        for (int i = 0; i < 10; i++){
            String type = types[random.nextInt(2)];
            String color = colors[random.nextInt(3)];
            AbstractFactory factory = FactoryProducer.getFactory(type); // 抽象工厂
            Ball ball = factory.getBall(color);
            Stick stick = factory.getStick(color);
            if (ball != null){
                ball.playBall();
            }
            if (stick != null){
                stick.playStick();
            }
        }
    }
}

 

标签:return,String,color,----,抽象,Stick,new,设计模式,public
来源: https://www.cnblogs.com/guaniu2750/p/14056486.html