Java编程里一般怎么管理接口的实现类?
PHPz
PHPz 2017-04-18 10:52:49
0
4
629

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

PHPz
PHPz

学习是最好的投资!

reply all(4)
阿神

工厂方法模式

左手右手慢动作

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. 🎜
Ty80

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

刘奇

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

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template