您当前的位置: 首页 > 
  • 0浏览

    0关注

    674博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

简单工厂模式和策略模式的区别

沙漠一只雕得儿得儿 发布时间:2020-08-08 12:11:07 ,浏览量:0

策略模式传入不同的实现对象,然后执行相应的不同实现方式。而工厂模式则仅仅是返回了不同的实例对象。

首先看下简单工厂模式的例子:

先写一个人的接口类,有price draw 2个方法

/**
 * 水果接口
 */
public interface Fruit {

    int price();

    void draw();

}

分别写两个实现类,一个是苹果的实现类,一个是香蕉的实现类

/**
 * 苹果
 */
public class Apple implements Fruit {

    private int price = 100;

    public Apple() {

    }

    public Apple(int price) {
        this.price = price;
    }

    @Override
    public int price() {
        return price;
    }

    @Override
    public void draw() {
        System.out.print("苹果红富士");
    }

    public void setPrice(int price) {
        this.price = price;
    }
}


/**
 * 香蕉
 */
public class Banana implements Fruit {

    private int price = 60;

    @Override
    public int price() {
        return price;
    }


    @Override
    public void draw() {
        System.out.print("仙人蕉");
    }


    public void setPrice(int price) {
        this.price = price;
    }
}

简单工厂类代码:

public class StaticFactory {

    public static final int TYPE_APPLE = 1;//苹果
    public static final int TYPE_ORANGE = 2;//桔子
    public static final int TYPE_BANANA = 3;//香蕉

    public static Fruit getFruit(int type) {
        if (TYPE_APPLE == type) {
            return new Apple();
        } else if (TYPE_ORANGE == type) {
            return new Orange("Peter", 80);
        } else if (TYPE_BANANA == type) {
            return new Banana();
        }
        return null;
    }
}

策略类代码:

public class FruitStrategy {

    private Fruit fruit;

    private static final int TYPE_APPLE = 1;//苹果
    private static final int TYPE_ORANGE = 2;//桔子
    private static final int TYPE_BANANA = 3;//香蕉

    //调用方式一:直接传入具体水果实例类
    public FruitStrategy(Fruit fruit) {
        this.fruit = fruit;
    }

    //调用方式二:传入数字在策略类里new出具体水果
    public FruitStrategy(int type) {
        if (TYPE_APPLE == type) {
            this.fruit =  new Apple();
        } else if (TYPE_ORANGE == type) {
            this.fruit = new Orange("Peter", 80);
        } else if (TYPE_BANANA == type) {
            this.fruit = new Banana();
        }
    }

    public void draw() {
        //添加其他方法或者流程调用
        this.fruit.draw();
    }

    public int price() {
        //添加其他方法或者流程调用
        return this.fruit.price();
    }

}

两种模式的调用方式:

public class FruitStrategyClient {
    public static void main(String[] args) {
        //调用方式:策略模式
        FruitStrategy fruit = new FruitStrategy(new Apple());
        fruit.draw();

        //调用方式:工厂模式:
        Fruit fruit = StaticFactory.getFruit(StaticFactory.TYPE_BANANA);//调用方式一
        fruit.draw();
    }
}

这么看来这两种模式好像是一模一样的,那么区别到底在哪呢。

   从工厂模式的代码中可以看到 工厂模式主要是返回的接口实现类的实例化对象,最后返回的结果是接口实现类中的方法;而策略模式是在实例化策略模式的时候已经创建好了,我们可以再策略模式中随意的拼接重写方法,而工厂模式是不管方法的拼接这些的,他只关注最后的结果,不注重过程,而策略模式注重的是过程。

可以明显看出,工厂模式中只有具体不同实例的返回,工厂类StaticFactory中只是返回了不同对象,调用方只能使用返回对象中的方法;

策略模式类FruitStrategy中除了返回不同对象,并且可以添加其他方法或者流程的调用,调用方可以处理不同对象的不同流程。

简单来说就是,工厂模式是根据不同条件返回不同对象的,为了方便统一管理;策略模式,顾名思义,策略是由一系列方法流程结合而来的,选择策略就是为了根据不同对象,去实现相同方法的不同策略流程。

具体例子见:https://github.com/buder-cp/base_component_learn/blob/master/patternsDesign/patterns/src/main/java/com/test/patterns/vs_factory_strategy/FruitStrategy.java

关注
打赏
1657159701
查看更多评论
立即登录/注册

微信扫码登录

0.0361s