How do I initialize classes with lots of fields in an elegant way?

后端 未结 6 1193
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 11:40

In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an

6条回答
  •  臣服心动
    2020-12-23 12:20

    I just provide this answer because it was mentioned in a comment and I think it should also be a part of this enumeration of Design Patterns.


    Null Object Design Pattern

    Intent

    The intent of a Null Object is to encapsulate the absence of an object by providing a substitutable alternative that offers suitable default do nothing behavior. In short, a design where "nothing will come of nothing"

    Use the Null Object pattern when

    • an object requires a collaborator. The Null Object pattern does not introduce this collaboration--it makes use of a collaboration that already exists
    • some collaborator instances should do nothing
    • you want to abstract the handling of null away from the client

    Here you find the full part of "Null Object" Design Pattern

提交回复
热议问题