用多态替换条件 - 当你的类型改变时如何处理?

Replace Conditional with Polymorphism - How to handle when your type changes?(用多态替换条件 - 当你的类型改变时如何处理?)
本文介绍了用多态替换条件 - 当你的类型改变时如何处理?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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...
      }
    }
}

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

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

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?

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

解决方案

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.

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.)

API

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);
    }
}

Statusimplementations

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");
    }
}

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

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

DispatcherQueue null when trying to update Ui property in ViewModel(尝试更新ViewModel中的Ui属性时DispatcherQueue为空)
Drawing over all windows on multiple monitors(在多个监视器上绘制所有窗口)
Programmatically show the desktop(以编程方式显示桌面)
c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
LINQ many-to-many relationship, how to write a correct WHERE clause?(LINQ多对多关系,如何写一个正确的WHERE子句?)