用多态替换条件-类型更改时如何处理? [英] Replace Conditional with Polymorphism - How to handle when your type changes?

查看:76
本文介绍了用多态替换条件-类型更改时如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于个人项目,我正在开发一款基于Web的小型游戏.

For a personal project, I'm working on a small web-based game.

我有一个具有Status属性的Card类,并且到处都有case语句.我想,嘿,这是用多态替换条件的绝佳机会!

I have a Card class that has a Status property, and there are case statements all over the place. I thought, hey, this is a great oppurtunity for Replace Conditional with Polymorphism!

问题是,我有几种方法可以执行以下操作:

The problem is, I have a couple methods that do stuff like this:

public class Card
{
    public void ChangeStatus()
    {
      switch (Status)
      {
        case MyStatusEnum.Normal:
          Status = MyStatusEnum.Underwater;
          break;
        case MyStatusEnum.Underwater:
          Status = MyStatusEnum.Dead;
          break;
        // etc...
      }
    }
}

在新的NormalCard类中重构它时,我像这样重写ChangeStatus方法:

When refactoring it in the new NormalCard class, I'm overriding the ChangeStatus method like this:

public override void ChangeStatus()
{
    base.Status = MyStatusEnum.Underwater;
}

问题是NormalCard的此对象的状态为Underwater.我无法重新分配this的类型,并且我真的不想将方法的返回值从void更改为CardBase.我有什么选择?有标准的方法吗?

The problem is this object of NormalCard has a status of Underwater. I can't reassign the type of this, and I don't really want to change the return of the methods from void to CardBase. What options do I have? Is there a standard way of doing this?

编辑 Tormod让我很直率.我想要状态模式.谢谢大家!

Edit Tormod set me straight. I want the State Pattern. Thanks all!

推荐答案

在您的情况下,我将有一个包含CardStatus属性的Card对象. CardStatus的子类型对应于先前的枚举值.将依赖于当前状态(状态转换)的行为重构为CardStatus子类型内的行为.

In your case, I'd have a Card object that contains a CardStatus property. The subtypes of CardStatus correspond to the previous enum values. Refactor behaviour that depends on the current status except the state transition to be inside the CardStatus subtypes.

第一个示例中的状态转换IMO应该保留在Card对象中.状态更改感觉更像是包含卡的行为而不是状态对象的行为.您可以做的是让CardStatus对象告诉您事件后要转换为什么状态.

The state transition that's in your first example should, IMO, remain inside the Card object. The state changing feels more like a behaviour of the containing card than of the state object. What you can do is have the CardStatus objects tell you what state to transition to after an event.

一个粗略的例子:(显然,可以使用更多的变体.)

A rough example: (obviously there's many more variations on this that could be used.)

interface ICardStatus {
    ICardStatus NextStatus(Card card);

    void DoStuff(Card card);
}

class Card {
    ICardStatus Status = new NormalCardStatus();

    void DoStuff() {
        Status.DoStuff(this);
    }

    void ChangeStatus() {
        Status = Status.NextStatus(this);
    }
}

状态实现

class NormalCardStatus : ICardStatus {
    ICardStatus NextStatus(Card card) {
        return new UnderwaterCardStatus();
    }

    void DoStuff(Card card) {
        // ...
    }
}

class UnderwaterCardStatus : ICardStatus {
    ICardStatus NextStatus(Card card) {
        return new DeathStatus();
    }

    void DoStuff(Card card) {
        // ...
    }
}

class DeathCardStatus : ICardStatus {
    ICardStatus NextStatus(Card card) {
        // ...
    }

    void DoStuff(Card card) {
        throw new Exception("Cannot do anything while dead");
    }
}

这篇关于用多态替换条件-类型更改时如何处理?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆