Home  >  Q&A  >  body text

Java编程里一般怎么管理接口的实现类?

比如说Color接口有三个实现类分别是:Red、Blue和Green
现在我要在运行的时候才决定用某个实现类
怎么管理比较优雅?别if..else..

PHPzPHPz2743 days ago583

reply all(4)I'll reply

  • 阿神

    阿神2017-04-18 10:54:49

    工厂方法模式

    reply
    0
  • ringa_lee

    ringa_lee2017-04-18 10:54:49

    In programming, 顺序分支循环三种结构。if-else is the most commonly used branch structure and is indispensable.

    Regarding this issue, if-else is the simplest programming method for anyone who has learned programming. And the simplest is the most elegant.

    And what if-else有时候看起来比较糟糕的地方在于,if中嵌套很多层if-else sometimes looks bad is that there are many layers nested in if

    , resulting in excessive depth of logical judgment.

    if的的时候就提前返回结果,保证ifSo a good solution is to limit the maximum depth of early exit

    to two levels.


    Here is a similar question you can look at:

    There are many if-else, how to optimize it?

    简单的设计设计模式Design patterns can bring you convenience, but don’t force design patterns into your own solutions. You need to make compromises between

    for your own code. 🎜

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:54:49

    The title description feels more in line with the design pattern 策略模式.

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:54:49

    Why not use polymorphism and leave it to implicit type conversion? It is handled according to the implementation of the interface method.

    reply
    0
  • Cancelreply