How can I design this better? (Avoiding a switch statement with Object Oriented design)

前端 未结 6 799
不思量自难忘°
不思量自难忘° 2020-12-30 11:23

I know a little bit about Object Oriented design, but I\'m not sure how to go about using those principles in my code. Here\'s what I\'m working on:

    pub         


        
6条回答
  •  伪装坚强ぢ
    2020-12-30 12:02

    You will likely want to implement the strategy pattern here. Basically, each of your possible "types" in your switch statement would become a class of its own that implements the same interface.

    Applying the Strategy Pattern

    You can then use a factory method that takes a "type" value as its parameter. That method would return the correct class (its return type is the interface mentioned above).

提交回复
热议问题