我刚刚从互联网上了解了战略模式的真正含义。但是我想知道它如何改善我的代码。例如,我在Internet上这样找到以下代码。这是名为Animal的超类:

abstract public class Animal {
    private String name;
    private int weight;
    private String sound;

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

    public String getName(){
        return name;
    }

    public void setWeight(int weight){
        if(weight > 0){
            this.weight = weight;
        }else {
            System.out.println("Weight must be bigger than 0");
        }
    }

    public int getWeight(){
        return weight;
    }

    public void setSound(String sound){
        this.sound = sound;
    }
    public String getSound(){
        return sound;
    }

    public void specialMethod(){
        System.out.println("Ok");
    }
}


这是名为Dog的子类:

public class Dog extends Animal {
    public void digHole(){
        System.out.println("Dig a hole");
    }

    public Dog(){
        super();
        setSound("bark");
    }

    public void testSuper(Animal obj){
        System.out.println(obj.getName());
    }
}


在教程中,它说是否要增加飞行能力,以便我可以检查狗是否可以飞行。如下面的代码所示,像这样直接添加代码是不好的。

具有超强飞行能力方法的超一流动物

abstract public class Animal {
    private String name;
    private int weight;
    private String sound;

    // Add fly method to the superclass which is a bad idea
    public String fly(){
        return " I am flying ";
    }

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

    public String getName(){
        return name;
    }

    public void setWeight(int weight){
        if(weight > 0){
            this.weight = weight;
        }else {
            System.out.println("Weight must be bigger than 0");
        }
    }

    public int getWeight(){
        return weight;
    }

    public void setSound(String sound){
        this.sound = sound;
    }

    public String getSound(){
        return sound;
    }

    public void specialMethod(){
        System.out.println("Ok");
    }
}


使用策略模式,我们可以使用fly方法创建名为Flys的接口,允许任何子类实现该方法,因此,如本教程所示,我创建了名为Flys的接口,其中有两个实现该接口的子类:

public interface Flys {
    String fly();
}

class ItFlys implements Flys{
    public String fly(){
        return "Flying high";
    }
}

class CantFly implements Flys{
    public String fly(){
        return "I can't fly";
    }
}


建立介面后,就可以重构Animal类,

abstract public class Animal {
    private String name;
    private int weight;
    private String sound;
    Flys flyingType; // Add an object of the interface to the superclass

    public String tryToFly(){ // add a new method tryToFly
        return flyingType.fly();
    }

    // Adding another method named setFlyingAbility
    public void setFlyingAbility(Flys newFlyType){
        flyingType = newFlyType;
    }

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

    public String getName(){
        return name;
    }

    public void setWeight(int weight){
        if(weight > 0){
            this.weight = weight;
        }else {
            System.out.println("Weight must be bigger than 0");
        }
    }

    public int getWeight(){
        return weight;
    }

    public void setSound(String sound){
        this.sound = sound;
    }

    public String getSound(){
        return sound;
    }

    public void specialMethod(){
        System.out.println("Ok");
    }
}


现在,在我的Dog子类中,我只需添加另一个代码

public class Dog extends Animal {
    public Dog(){
        super();
        setSound("bark");
        flyingType = new CantFly(); // I set flyingType object
    }

    public void digHole(){
        System.out.println("Dig a hole");
    }

    public void testSuper(Animal obj){
        System.out.println(obj.getName());
    }
}


在最后一堂课,我可以执行所有代码,并检查我的Dog课是否可以飞行。

public class AnimalPlay {
    public static void main(String args[]){
        Animal sparky = new Dog();
        Animal tweety = new Bird();
        System.out.println("Dog " + sparky.tryToFly()); // the result is I can't fly
        System.out.println("Bird " + tweety.tryToFly()); // the result is I am flying
        sparky.setFlyingAbility(new ItFlys());
        System.out.println("Dog " + sparky.tryToFly()); // the result is I am flying
    }
}


我的问题是,如果仍然以传统方式添加fly()方法,它会得到相同的结果吗?

在超类中添加fly()方法,以便可以在Dog类中覆盖fly()方法,但这不是一个好主意。

abstract public class Animal {
    private String name;
    private int weight;
    private String sound;

    // Add fly method to the superclass which is a bad idea
    public String fly(){
        return " I am flying ";
    }

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

    public String getName(){
        return name;
    }

    public void setWeight(int weight){
        if(weight > 0){
            this.weight = weight;
        }else {
            System.out.println("Weight must be bigger than 0");
        }
    }

    public int getWeight(){
        return weight;
    }

    public void setSound(String sound){
        this.sound = sound;
    }
    public String getSound(){
        return sound;
    }

    public void specialMethod(){
        System.out.println("Ok");
    }
}

最佳答案

我的问题是,如果仍然以传统方式添加fly()方法,它会得到相同的结果吗?


答案是不'。


策略模式允许您将行为转移到单独的类中,这对SOLID原则“ S”(单一职责)有利。您需要学习机器人或人类以“吠叫”的图像-无需使它们继承动物基类。而且您也不需要在每个类中实现吠叫。
在基类中具有所有属性也是不好的,因为它反对SOLID'L'-Liskou替代。如果猴子不需要吠叫该怎么办呢?
策略模式允许您根据SOLID'I'进行相应的代码设计-接口隔离-仅使IAnimalAction接口实现吠叫的许多实现,并将IAnimalAction属性分配给所需的动物类(作为属性或作为另一个要实现的接口)
策略也可以帮助解决SOLID'D'的问题-您可以注入所需的动物策略(吠叫,飞行),而无需让每个动物都知道


我们可以继续寻找其他奖金。但是你可以看到一张照片

祝好运!

关于java - 我们为什么要使用策略模式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34248705/

10-12 03:57