java常用设计模式:工厂模式、策略模式、状态模式

工厂模式、策略模式、动态模式,这三个模式很像又有一定的区别,用代码来看一看有什么区别。
写一个接口,输出水果价格

public interface Fruit {

    void price();
}

写三个不同水果的实现类

public class Apple implements Fruit {

    @Override
    public void price() {
        System.out.println("苹果20元一斤");
    }
}
public class Banana implements Fruit {

    @Override
    public void price() {
        System.out.println("香蕉15块钱一斤");
    }
}
public class Tangerine implements Fruit {

    @Override
    public void price() {
        System.out.println("橘子10块钱一斤");
    }
}

工厂模式


/**
 * 工厂类
 */
public class FruitFactory {
    public Fruit getFruit(String fruitType) {
        if (fruitType.equals("apple")) {
            return new Apple();
        } else if (fruitType.equals("tangerine")) {
            return new Tangerine();
        } else if (fruitType.equals("banana")) {
            return new Banana();
        }
        return null;
    }
}
/**
 * 测试类(工厂模式)
 */
public class Test {

    public static void main(String[] args) {
        FruitFactory fruitFactory = new FruitFactory();
        Fruit fruit = fruitFactory.getFruit("apple");
        fruit.price();
    }
}

策略模式

/**
 * 策略类
 */
public class FruitStrategy {

    private Fruit fruit;

    public FruitStrategy(String fruitType) {
        if (fruitType.equals("apple")) {
            this.fruit = new Apple();
        } else if (fruitType.equals("tangerine")) {
            this.fruit = new Tangerine();
        } else if (fruitType.equals("banana")) {
            this.fruit = new Banana();
        }
    }

    /**
     * 策略方法
     */
    public void price() {
        fruit.price();
        System.out.println("价格太贵,我要砍价");
    }
}
/**
 * 测试类(策略模式)
 */
public class Test {
    public static void main(String[] args) {
        FruitStrategy fruitFactory = new FruitStrategy("apple");
        fruitFactory.price();
    }
}

工厂模式和策略的区别在于工厂模式只管生产实例,具体怎么使用由调用方来决定。策略模式是将生成实例的使用策略在策略类里面配置之后才提供给调用方会用。工厂模式调用发可以直接调用工厂实例的方法属性等,策略模式不能直接调用实例的方法属性,只能在策略类中封装策略才能代用。

状态模式

这里以洗衣机工作为例子
抽象接口

public interface State {

    void doJob(Washing washing);
}

开始状态

public class Start implements State {

    @Override
    public void doJob(Washing washing) {
        System.out.println("洗衣机开始工作");
        washing.setState(new Work());
        washing.request();
    }
}

工作状态

public class Work implements State {

    @Override
    public void doJob(Washing washing) {
        System.out.println("洗衣机工作中");
        washing.setState(new End());
        washing.request();
    }
}

结束状态

public class End implements State {

    @Override
    public void doJob(Washing washing) {
        System.out.println("洗衣机工作完成");
        washing.setState(null);
    }
}

洗衣机类

public class Washing {
    private State state;

    public void setState(State state) {
        this.state = state;
        if (state == null) {
            System.out.println("当前状态:null");
        } else {
            System.out.println("当前状态:" + state.getClass().getName());
        }
    }

    public void request() {
        if (state != null) {
            state.doJob(this);
        }
    }
}

测试类

public class Test {
    public static void main(String[] args) {
        Washing washing = new Washing();
        washing.setState(new Start());
        washing.request();
    }
}

状态模式的核心是将对象每一个状态做的事情分别交给每一个单独的状态对象处理,并且由状态自己控制向其他状态的转移;行为类仅向外提供方便用户使用的接口;

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值