search

Home  >  Q&A  >  body text

java - How to make it easier for a class to add attributes in future expansion?

Suppose there are two signs on the map, a hospital and a road, and these two signs have their own attribute data:
For example, a hospital has attributes such as grade, number of buildings, number of patients, floor area, name, etc. ;Roads have attributes such as name, grade, length, construction time, etc. That is, different types of map identifiers have different attribute data, and it is also necessary to consider that new attributes will be added in the future.
I would like to ask everyone, can you give me a rough design idea (just the design idea of ​​static class diagram)?
PS: My own idea is to implement it through the decorator pattern, but in that case, every time an attribute is added in future extensions, the original class will have to be decorated again, or the originally designed class will be directly invaded. , add attributes.

天蓬老师天蓬老师2768 days ago613

reply all(3)I'll reply

  • PHPz

    PHPz2017-05-27 17:43:12

    haha, why not use Dictionary type, for example, use map to store key-value pairs?

    reply
    0
  • 迷茫

    迷茫2017-05-27 17:43:12

    It should comply with the opening and closing principle. It is best not to modify an entity class after it is created. Just use inheritance to add new attributes to the original attributes without destroying the original structure

    reply
    0
  • ringa_lee

    ringa_lee2017-05-27 17:43:12

    Object composition takes precedence over inheritance

    reply
    0
  • Cancelreply