策略设计模式 Strategy Design Pattern

本文介绍策略设计模式的应用场景和实现方式,通过一个飞行能力的例子详细展示了如何利用接口和多态来减少代码冗余并提高代码的灵活性。文章还讨论了模式的优点与潜在缺点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. 策略设计模式初衷

减少代码冗余,降低代码之间的耦合度。同时保证代码的可维护性。
Positive:
- Often reduces long lists of conditions
- Avoid duplicate code
- Keep class changes from other class changes
- Can hide complicated/ secret code from the user
Negative:
Increased number of Objects/Classes

2. 策略实际模式应用场景

  1. 当我们想要定义一组算法,而且这些算法所要表现的的内容很相似时。如果在每个父类定义该算法的普适版本,然后在子类中覆盖override该方法这样一方面增加了代码的冗余度,另一方面违背了子类方法应该相互之间不同这一设计原则。
  2. 但我们想要动态改变对象的某种行为,且该行为有几种可供选择项时。

3. 具体UML图及解释

这里的Animal作为一个base class, derived class–包括Dog和Bird可以直接继承该父类,完成相应的功能,但这不是好的设计准则。因为这样引入了大量冗余代码,且违背了OOD的封装特性。
这里写图片描述

4. 一个具体的demo

package com.fqyuan.strategy;

public interface Flys {
    public String fly();
}

class FlyFast implements Flys {
    @Override
    public String fly() {
        return "Flying fast!";
    }
}

class FlySlow implements Flys {
    @Override
    public String fly() {
        return "Flying slowly!";
    }
}

class NotFly implements Flys {
    @Override
    public String fly() {
        return "Unable to fly!";
    }
}

package com.fqyuan.strategy;

public class Animal {
    private String name;
    public Flys flyType;

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

    public String getName() {
        return name;
    }

    // The super class pushes off the responsibility for flying to flyType, an
    // interface instance
    public String tryToFly() {
        return flyType.fly();
    }

    // We can add the method to change the Flys type dynamically
    public void setFlyType(Flys newFlyType) {
        flyType = newFlyType;
    }

    @Override
    public String toString() {
        return name;
    }

    /*
     * Bad, you don't want to add methods to the super class. You need to
     * separate what is different from subclass and the super class.
     */
    // public void fly(){
    // System.out.println("I'm flying!");
    // }
}

package com.fqyuan.strategy;

public class Dog extends Animal {
    public Dog(String name) {
        this.setName(name);
        /*
         * We set the interface polymorphically. Here we set the dog disability
         * to fly.
         */
        flyType = new NotFly();
    }

    /*
     * Bad! You can override the fly() method, but we're breaking the rule that
     * we need to abstract what is different to the subclasses.
     * 即:子类的方法尽量保持一般是和其他子类方法不同,这样可以减少代码的冗余度。
     */
    // public void fly() {
    // System.out.println("I can't fly!");
    // }
}

package com.fqyuan.strategy;

public class Cat extends Animal {
    public Cat(String name) {
        setName(name);
        this.flyType = new FlyFast();
    }
}

package com.fqyuan.strategy;

/*
 * 设计模式出现的缘由?
 * 代码的可读性,可维护性,减少代码的冗余度.
 * 2条重要原则:
 * 1). 能使用composition的尽量不要用inheritance.
 * 2). 能使用Interface的尽量不要用具体的class.
 */
public class StrategyDemo {

    public static void main(String[] args) {
        Animal dog = new Dog("Rick");
        Cat cat = new Cat("Kitty");

        System.out.println(dog + " " + dog.tryToFly());
        System.out.println(cat + " " + cat.tryToFly());
        dog.setFlyType(new FlySlow());
        System.out.println(dog + " " + dog.tryToFly());
    }

}

//Running result:
Rick Unable to fly!
Kitty Flying fast!
Rick Flying slowly!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值