📜  策略模式 |设置 2(实施)

📅  最后修改于: 2021-09-10 02:39:22             🧑  作者: Mango

我们已经讨论了一个战斗机示例并在第 1 组中介绍了策略模式。

策略模式 |第一套(介绍)

在这篇文章中,我们将策略模式应用于战斗机问题并讨论实现。

第一步是识别未来可能因不同类别而异的行为,并将它们与其他类别分开。对于我们的示例,让它们成为踢腿和跳跃行为。为了分离这些行为,我们将从Fighter类中提取这两种方法,并创建一组新的类来表示每个行为。

战斗机3

Fighter 类现在将委托其踢腿和跳跃行为,而不是使用在 Fighter 类或其子类中定义的踢腿和跳跃方法。

战斗机4

返工后,最终的类图将是(单击图像以获得更好的视图):
f1

将我们的设计与封装踢腿和跳跃行为的策略模式定义进行比较,是两个算法系列。这些算法在实现中是可以互换的。

下面是相同的Java实现。

// Java program to demonstrate implementation of
// Strategy Pattern
  
// Abstract as you must have a specific fighter
abstract class Fighter
{
    KickBehavior kickBehavior;
    JumpBehavior jumpBehavior;
  
    public Fighter(KickBehavior kickBehavior,
                   JumpBehavior jumpBehavior)
    {
        this.jumpBehavior = jumpBehavior;
        this.kickBehavior = kickBehavior;
    }
    public void punch()
    {
        System.out.println("Default Punch");
    }
    public void kick()
    {
        // delegate to kick behavior
        kickBehavior.kick();
    }
    public void jump()
    {
  
        // delegate to jump behavior
        jumpBehavior.jump();
    }
    public void roll()
    {
        System.out.println("Default Roll");
    }
    public void setKickBehavior(KickBehavior kickBehavior)
    {
        this.kickBehavior = kickBehavior;
    }
    public void setJumpBehavior(JumpBehavior jumpBehavior)
    {
        this.jumpBehavior = jumpBehavior;
    }
    public abstract void display();
}
  
// Encapsulated kick behaviors
interface KickBehavior
{
    public void kick();
}
class LightningKick implements KickBehavior
{
    public void kick()
    {
        System.out.println("Lightning Kick");
    }
}
class TornadoKick implements KickBehavior
{
    public void kick()
    {
        System.out.println("Tornado Kick");
    }
}
  
// Encapsulated jump behaviors
interface JumpBehavior
{
    public void jump();
}
class ShortJump implements JumpBehavior
{
    public void jump()
    {
        System.out.println("Short Jump");
    }
}
class LongJump implements JumpBehavior
{
    public void jump()
    {
        System.out.println("Long Jump");
    }
}
  
// Characters
class Ryu extends Fighter
{
    public Ryu(KickBehavior kickBehavior,
               JumpBehavior jumpBehavior)
    {
        super(kickBehavior,jumpBehavior);
    }
    public void display()
    {
        System.out.println("Ryu");
    }
}
class Ken extends Fighter
{
    public Ken(KickBehavior kickBehavior,
               JumpBehavior jumpBehavior)
    {
        super(kickBehavior,jumpBehavior);
    }
    public void display()
    {
        System.out.println("Ken");
    }
}
class ChunLi extends Fighter
{
    public ChunLi(KickBehavior kickBehavior,
                  JumpBehavior jumpBehavior)
    {
        super(kickBehavior,jumpBehavior);
    }
    public void display()
    {
        System.out.println("ChunLi");
    }
}
  
// Driver class
class StreetFighter
{
    public static void main(String args[])
    {
        // let us make some behaviors first
        JumpBehavior shortJump = new ShortJump();
        JumpBehavior LongJump = new LongJump();
        KickBehavior tornadoKick = new TornadoKick();
  
        // Make a fighter with desired behaviors
        Fighter ken = new Ken(tornadoKick,shortJump);
        ken.display();
  
        // Test behaviors
        ken.punch();
        ken.kick();
        ken.jump();
  
        // Change behavior dynamically (algorithms are
        // interchangeable)
        ken.setJumpBehavior(LongJump);
        ken.jump();
    }
}

输出 :

Ken
Default Punch
Tornado Kick
Short Jump
Long Jump

参考:
Head First 设计模式