哪种设计模式适合这种情况? [英] What design pattern is proper for this situation?

查看:77
本文介绍了哪种设计模式适合这种情况?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习设计模式,我很困惑,因为我不知道哪种模式最适合这种情况:

I'm learning design patterns and I am confused because I don't know what pattern is best for this situation:

我有String,并且此String可能有所不同.可能是"123abc","abc123","abcdfe"或"123456",并且仅类似于那些字符串.

I have String and this String may be various. It may be "123abc", "abc123", "abcdfe" or "123456" and only like those string's.

public void doSomething(String variousString) {
    StringType type = checkType(variousString);
    if(type==StringType.Mixed) doMixedAction();
    if(type==StringType.OnlyLetters) doOnlyLetterAction();
    if(type==StringType.OnlyDigits) doOnlyDigitsAction();
}

首先,我想到了策略模式,但我认为这里不匹配.现在,我认为责任链在这里非常匹配.

Firstly I thought about Strategy pattern but I think that It doesn't match here. Now I think that Chain of Responsibility is matching here very well.

在这种情况下删除if语句的最佳模式是什么?您对此有何看法?

What is the best pattern for remove if statements in this situation? What are your opinions?

推荐答案

使用StringType显示的内容很可能被视为类型系统.本质上,此处的相关模式很可能是简单的切换",策略模式"或访问者".

What you show with StringType can likely be considered a Type System. Essentially the relevant patterns here are likely to be a simple 'switch', Strategy Pattern, or Visitor.

应选择哪种模式/实施方式,在很大程度上取决于范围和内容.动作和类型的可变性.

What patterns/ implementation should be chosen depend largely on the scope & variability of Actions and Types.

要考虑的一些问题:

  • 动作"数量很少在整个应用程序中作为基础结构重用,还是该应用程序会创建许多任意动作?
  • 类型是否固定?
  • 对于Action实现来说,能够相互委托会很有用,例如.从简单的行为中构建出复杂的行为?

现在是图案.

  • 实施MyProcess.doAction(),并有一个开关以选择适当的实施.
  • 适用于类型系统中很多动作或未知的动作,但类型固定得很好的情况.
  • implement MyProcess.doAction() with a switch to choose the implementation appropriately.
  • suitable when the Actions are many/ or not known to the Type System, but the Types are fairly well fixed.
  • 实施使用一个开关来适当地选择实施.
  • 适合类型"的数量适中&预先知道类型系统.
  • StringType的子类型或参数化以适当地实现doAction()方法.
  • 适用于所需的操作"数量适中&预先知道类型系统.
  • 策略还可以允许责任链";当策略委派时就会出现这种情况.
  • subtype or parameterize instances of StringType to implement the doAction() method appropriately.
  • suitable when the number of "Actions" needed is modest & known in advance to the type system.
  • Strategy can also allow 'Chain of Responsibility'; this arises when Strategies delegate.
  • 定义一个StringVisitor(哦,我的名字,有意义的名称会使它变得如此简单).
  • 它实现了visitMixed(),visitOnlyLetters(),visitOnlyDigits()方法.
  • 然后,根据类型将StringValue或某些帮助程序发送到任意访问者上.

说实话,给出的问题和例子很差&缺乏意义,无法给出有意义的设计答案-但以下内容应为您提供一些思考的机会.

To be honest the question and example given are poor & lacking in meaning, for meaningful design answers to be given -- but the following should give you some food for thought.

我绝对建议您尝试使用这些内容(IBAN代码,如果是这样的话)来构建有意义的域模型.您将需要考虑您的具体类型和内容.动作,以及这些动作的相对动态/固定性,以进行适当的设计.

I'd definitely suggest you try and make a meaningful domain model out of these things (IBAN Codes, if that's what they are). You will need to consider what your concrete types & actions are, and the relative dynamicity/ fixedness of these, to get a proper design.

这篇关于哪种设计模式适合这种情况?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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