设计模式学习心得——one

背景

最近在工作中,要处理分布式多组件间的通信反馈问题,但是因为没有学习过设计模式,导致了编码实现的功能健壮性很差,并且难以维护,自己都理不清整个流程的逻辑细节。

面对这种情况,开始学习《Head First 设计模式》这本书。并将学到的模型记录下来。接下来学第一个设计模式:


策略模式


设计原则汇总

1. 找出应用中可能需要变化之处,把他们独立出来,不要和那些不需要变化的代码混在一起。
2. 针对接口编程,而不是针对实现编程。
3. 多用组合,少用继承。

代码实现

1.Duck.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public abstract class Duck {

    FlyBehavior flyBehavior;
    QuackBehavior quackBehavior;

    public Duck() {

    }

    public abstract void display();

    public void performFly() {
        flyBehavior.fly();
    }

    public void performQuack() {
        quackBehavior.quack();
    }

    public void swim() {
        System.out.println("All ducks float, even decoys!");
    }

    public void setFlyBehavior(FlyBehavior flyBehavior) {
        this.flyBehavior = flyBehavior;
    }

    public void setQuackBehavior(QuackBehavior quackBehavior) {
        this.quackBehavior = quackBehavior;
    }
}
2.FlyBehavior.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public interface FlyBehavior {
    public void fly();
}
3.FlyNoWings.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public class FlyNoWings implements FlyBehavior {
    @Override
    public void fly() {
        System.out.println("I can't fly.");
    }
}
4.FlyRocketPowered.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public class FlyRocketPowered implements FlyBehavior {
    @Override
    public void fly() {
        System.out.println("I'm flying with a rocket!");
    }
}
5.FlyWithWings.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public class FlyWithWings implements FlyBehavior {
    @Override
    public void fly() {
        System.out.println("I'm flying!!");
    }
}
6.MallardDuck.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public class MallardDuck extends Duck {

    public MallardDuck() {
        quackBehavior = new Quack();
        flyBehavior = new FlyWithWings();
    }

    @Override
    public void display() {
        System.out.println("I'm a real Mallard duck");
    }


}
7.MiniDuckSimulator.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public class MiniDuckSimulator {
    public static void main(String[] args) {
        Duck mallard = new MallardDuck();
        mallard.performQuack();
        mallard.performFly();

        Duck model = new ModelDuck();
        model.performFly();
        model.setFlyBehavior(new FlyRocketPowered());
        model.performFly();
    }
}
8.ModelDuck.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public class ModelDuck extends Duck {
    public ModelDuck() {
        flyBehavior = new FlyNoWings();
        quackBehavior = new Quack();
    }

    @Override
    public void display() {
        System.out.println("I'm a model duck");
    }
}
9.MuteQuack.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public class MuteQuack implements QuackBehavior {
    @Override
    public void quack() {
        System.out.println("<< Silence >>");
    }
}
10.Quack.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public class Quack implements QuackBehavior {
    @Override
    public void quack() {
        System.out.println("Quack");
    }
}
11.QuackBehavior.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public interface QuackBehavior {
    public void quack();
}
12.Squeak.java
package lesson1;

/**
 * Created by willard on 17/4/4.
 */
public class Squeak implements QuackBehavior {
    @Override
    public void quack() {
        System.out.println("Squea");
    }
}

结构关系图

待补充......





你可能感兴趣的:(设计模式)