1. 程式人生 > 實用技巧 >設計模式(三)---- 抽象工廠模式

設計模式(三)---- 抽象工廠模式

  抽象工廠模式可以理解為根據需要建立相應的工廠,進而生產出對應的產品。

  接著上一篇文章,定義一個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();
            }
        }
    }
}