java - How to make it easier for a class to add attributes in future expansion?
天蓬老师
天蓬老师 2017-05-27 17:41:12
0
3
605

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.

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all(3)
PHPzhong

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

迷茫

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

左手右手慢动作

Object composition takes precedence over inheritance

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