Patterns for the overlap of two objects

…衆ロ難τιáo~ 提交于 2019-12-05 21:32:29

Table driven designs can be effective. Steve Maguire had few nice examples in Writing Solid Code .

They are also a great way to capture tests, see fit .

In your case something like:

Field1ReadonlyRules = {
    'user class 1' : True,
    'user class 2' : False
}

field1.readOnly = Field1ReadonlyRules[ someUser.userClass ]

As an aside you probably want to model both users and user classes/roles/groups instead of combining them. A user typically captures who (authentication) while groups/roles capture what (permissions, capabilities)

At first blush it sounds more like you have two different types of users and they have different access levels. This could be solved by inheritance (PowerUser, User) or by containing a security object or token that sets the level for the user.

If you don't like inheritance as a rule, you could use a State pattern on the application, Decorate the user objects (Shudder) or possibly add strategy patterns for differing security levels. But I think it's a little early yet, I don't normally apply patterns until I have a firm idea of how the item will grown and be maintained.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!