用多态性替换条件 - 理论上很好,但不实用 [英] Replace conditional with polymorphism - nice in theory but not practical

查看:20
本文介绍了用多态性替换条件 - 理论上很好,但不实用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

用多态性替换条件"只有在已经为您选择了您正在为其执行 switch/if 语句的对象类型时才是优雅的.例如,我有一个 Web 应用程序,它读取一个名为action"的查询字符串参数.Action 可以有view"、edit"、sort"等值.那么如何用多态性来实现呢?好吧,我可以创建一个名为 BaseAction 的抽象类,并从中派生 ViewAction、EditAction 和 SortAction.但是我不需要一个条件来决定要实例化哪种类型的 BaseAction 吗?我不明白如何用多态性完全替换条件.如果有的话,条件句只是被推到链的顶部.

"Replace conditional with polymorphism" is elegant only when type of object you're doing switch/if statement for is already selected for you. As an example, I have a web application which reads a query string parameter called "action". Action can have "view", "edit", "sort", and etc. values. So how do I implement this with polymorphism? Well, I can create an abstract class called BaseAction, and derive ViewAction, EditAction, and SortAction from it. But don't I need a conditional to decided which flavor of type BaseAction to instantiate? I don't see how you can entirely replace conditionals with polymorphism. If anything, the conditionals are just getting pushed up to the top of the chain.

public abstract class BaseAction
{
    public abstract void doSomething();
}

public class ViewAction : BaseAction
{
    public override void doSomething() { // perform a view action here... }
}

public class EditAction : BaseAction
{
    public override void doSomething() { // perform an edit action here... }
}

public class SortAction : BaseAction
{
    public override void doSomething() { // perform a sort action here... }
}


string action = "view";  // suppose user can pass either "view", "edit", or "sort" strings to you.
BaseAction theAction = null;

switch (action)
{
    case "view":
        theAction = new ViewAction();
        break;

    case "edit":
        theAction = new EditAction();
        break;

    case "sort":
        theAction = new SortAction();
        break;
}

theAction.doSomething();    // So I don't need conditionals here, but I still need it to decide which BaseAction type to instantiate first. There's no way to completely get rid of the conditionals.

推荐答案

你是对的——条件被推到链的顶端"——但没有公正"的.它非常强大.正如@thkala 所说,您只需选择一次;从那时起,对象就知道如何开展业务了.您描述的方法——BaseAction、ViewAction 和其他方法——是一种很好的方法.试试看,看看你的代码会变得多干净.

You're right - "the conditionals are getting pushed up to the top of the chain" - but there's no "just" about it. It's very powerful. As @thkala says, you just make the choice once; from there on out, the object knows how to go about its business. The approach you describe - BaseAction, ViewAction, and the rest - is a good way to go about it. Try it out and see how much cleaner your code becomes.

当你有一个工厂方法,它接受一个像View"这样的字符串并返回一个动作,你调用它,你已经隔离了你的条件.那太棒了.除非你尝试过,否则你无法正确地欣赏它的力量——所以试一试吧!

When you've got one factory method that takes a string like "View" and returns an Action, and you call that, you have isolated your conditionality. That's great. And you can't properly appreciate the power 'til you've tried it - so give it a shot!

这篇关于用多态性替换条件 - 理论上很好,但不实用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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