Java: using polymorphism to avoid if-statements?

前端 未结 9 744
执念已碎
执念已碎 2020-12-29 08:30

I\'m attempting to write a java program that initializes certain layouts based on what the user selects. What I want to do is try to avoid writing a bunch of if-statements s

9条回答
  •  Happy的楠姐
    2020-12-29 08:56

    Something, somewhere, needs to specify the implementation. That might be a chain of if statements in source code -- but it could also be something else; e.g., a classname specified in an external data source, instantiated via reflection.

    For some interfaces, there may be many more calls to the interface than instantiations. Polymorphism can reduce coupling to specific implementations.

    This reduced coupling can help make code more maintainable. You can add a new implementation without modifying potentially many callers.

提交回复
热议问题