使用哪种 oo 设计模式
我需要建立一个具有不同会员级别的付费会员资格的网站。根据会员级别,他们将有权使用某些功能。有没有我可以使用的设计模式?
I need to make a website that will have paid memberships with differing membersip levels. Based on a members level, they will have access to certain features. Is there a design pattern I could use for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
我建议您阅读更多有关设计模式和 OOAD 的内容,并了解设计模式的真正目的。您的应用程序是一个非常模糊的情况,无法直接在其上应用模式。模式的使用取决于应用程序内部的各个方面,而不是整个应用程序。
I recommend you to read more about design-patterns and OOAD and understand the real purpose of design-patterns. Your application is a very vague case for applying patterns directly onto it. use of patterns depend on various aspects inside the application and not on the application as whole.
看起来您刚刚开始学习设计模式。 :) 常见的事情是,当您开始学习设计模式时,您尝试将其放在任何地方,尝试用模式解决每个问题,并且通常模式的使用成为您的目标,而不是您要解决的实际问题。
您应该暂时忘记设计模式,专注于您的目标,专注于您要解决的问题。然后尝试解决它,然后也许就不需要使用任何模式了。也许你会意识到架构出了问题,然后你才应该考虑一些重构和模式。
Looks like you've just started learning Design patterns. :) The common thing is that when you start learning design patterns, you try to put it everywhere, you try to solve every problem with patterns, and commonly the use of patterns become your goal instead of real problem you're going to solve.
You should forget for a while about design patterns and concentrate on your goal, on what problem you're trying to solve. Then try to solve it, and then perhaps there won't be a need to use any pattern. And perhaps you will realise that something is wrong with the architecture, only then you should think about some refactoring and patterns.
您所描述的内容与面向对象的设计模式无关,但与访问控制有关。如果您有大量的分层访问,我建议使用基于角色的访问控制,否则坚持使用 ACL。
What you're describing has nothing to do with a Object Oriented design pattern, but has to do with access control. If you have tons of hierarchical accesses, I suggest using Role Based Access Control, otherwise stick to ACLs.
看看 MVC、.net 提供的一些示例以及过去对我们有用的东西
Have a look at MVC, .net offer a few examples and something that has worked for us in the past
我建议避免用户继承并选择在对象中带有表示访问权限的标志的用户。面向对象很好,但在简单对象中使用标志要容易得多,尤其是在处理网站时。
I suggest avoiding user inheritance and opting for users with flags in the object denoting access rights. OO is good, but it's far easier to work with flags in a simple object, especially when dealing with websites.
在最简单的层面上,您可以有一个名为“Feature”的枚举,并让“Member”类包含他/她有权使用的“Features”列表。
您可以决定按照某人的建议将它们作为标志存储在数据库中。
如果复杂性增加,您可以使“Feature”成为一个完整的类层次结构,例如有一个名为 IFeature 的接口。您可以为 IFeatures 实现具体的类。然后,您的“Member”对象将包含 IFeature 列表
In its simplest level, you could have an enumeration called e.g 'Feature' and have the 'Member' class contain a list of 'Features' that he/she is entitled to.
You may decide to store them as flags in the database as someone suggested.
If complexity increases, you can make 'Feature' a full blown class hierarchy e.g. have an interface called IFeature. You can them implement concrete classes for IFeatures. Your 'Member' object will then contain a list of IFeature(s)
您想要的是责任链设计模式。
这是一个 C# 示例:
希望有帮助。
What you want is the Chain of Responsibility design pattern.
Here is an example in C#:
Hope that helps.