使用哪种 oo 设计模式

发布于 2024-10-21 19:07:37 字数 66 浏览 1 评论 0原文

我需要建立一个具有不同会员级别的付费会员资格的网站。根据会员级别,他们将有权使用某些功能。有没有我可以使用的设计模式?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

疏忽 2024-10-28 19:07:37

我建议您阅读更多有关设计模式和 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.

且行且努力 2024-10-28 19:07:37

看起来您刚刚开始学习设计模式。 :) 常见的事情是,当您开始学习设计模式时,您尝试将其放在任何地方,尝试用模式解决每个问题,并且通常模式的使用成为您的目标,而不是您要解决的实际问题。

您应该暂时忘记设计模式,专注于您的目标,专注于您要解决的问题。然后尝试解决它,然后也许就不需要使用任何模式了。也许你会意识到架构出了问题,然后你才应该考虑一些重构和模式。

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.

时间海 2024-10-28 19:07:37

您所描述的内容与面向对象的设计模式无关,但与访问控制有关。如果您有大量的分层访问,我建议使用基于角色的访问控制,否则坚持使用 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.

欢烬 2024-10-28 19:07:37

看看 MVC、.net 提供的一些示例以及过去对我们有用的东西

Have a look at MVC, .net offer a few examples and something that has worked for us in the past

狠疯拽 2024-10-28 19:07:37

我建议避免用户继承并选择在对象中带有表示访问权限的标志的用户。面向对象很好,但在简单对象中使用标志要容易得多,尤其是在处理网站时。

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.

A君 2024-10-28 19:07:37

在最简单的层面上,您可以有一个名为“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)

无人接听 2024-10-28 19:07:37

您想要的是责任链设计模式。

这是一个 C# 示例:

using System;

internal sealed class Program
{
    private static void Main()
    {
        // Setup Chain of Responsibility.
        Approver larry = new Director();
        Approver sam   = new VicePresident();
        Approver tammy = new President();

        larry.Successor = sam;
        sam.Successor   = tammy;

        Purchase purchase;

        // Generate and process purchase requests.
        purchase = new Purchase { 
          Number = 2034, Amount = 350.00, Purpose = "Supplies" };

        larry.ProcessRequest(purchase);

        purchase = new Purchase { 
          Number = 2035, Amount = 32590.10, Purpose = "Project X" };

        larry.ProcessRequest(purchase);

        purchase = new Purchase { 
          Number = 2036, Amount = 122100.00, Purpose = "Project Y" };

        larry.ProcessRequest(purchase);

        // Wait for user.
        Console.ReadKey();
    }
}

// Purchase event argument holds purchase info.
internal sealed class PurchaseEventArgs : EventArgs
{
    public Purchase Purchase { get; set; }
}

/// <summary>
/// The 'Handler' abstract class.
/// </summary>
internal abstract class Approver
{
    // Purchase event .
    public event EventHandler<PurchaseEventArgs> Purchase;

    // Purchase event handler.
    public abstract void PurchaseHandler(Object sender, PurchaseEventArgs e);

    // Constructor.
    public Approver()
    {
        Purchase += PurchaseHandler;
    }

    public void ProcessRequest(Purchase purchase)
    {
        OnPurchase(new PurchaseEventArgs { Purchase = purchase });
    }

    // Invoke the Purchase event.
    public virtual void OnPurchase(PurchaseEventArgs e)
    {
        if (Purchase != null)
        {
            Purchase(this, e);
        }
    }

    // Sets or gets the next approver.
    public Approver Successor { get; set; }
}

/// <summary>
/// The 'ConcreteHandler' class.
/// </summary>
internal sealed class Director : Approver
{
    public override void PurchaseHandler(Object sender, PurchaseEventArgs e)
    {
        if (e.Purchase.Amount < 10000.0)
        {
            Console.WriteLine("{0} approved request# {1}",
                this.GetType().Name, e.Purchase.Number);
        }
        else if (Successor != null)
        {
            Successor.PurchaseHandler(this, e);
        }
    }
}

/// <summary>
/// The 'ConcreteHandler' class.
/// </summary>
internal sealed class VicePresident : Approver
{
    public override void PurchaseHandler(Object sender, PurchaseEventArgs e)
    {
        if (e.Purchase.Amount < 25000.0)
        {
            Console.WriteLine("{0} approved request# {1}",
                this.GetType().Name, e.Purchase.Number);
        }
        else if (Successor != null)
        {
            Successor.PurchaseHandler(this, e);
        }
    }
}

/// <summary>
/// The 'ConcreteHandler' class.
/// </summary>
internal sealed class President : Approver
{
    public override void PurchaseHandler(Object sender, PurchaseEventArgs e)
    {
        if (e.Purchase.Amount < 100000.0)
        {
            Console.WriteLine("{0} approved request# {1}", 
                sender.GetType().Name, e.Purchase.Number);
        }
        else if (Successor != null)
        {
            Successor.PurchaseHandler(this, e);
        }
        else
        {
            Console.WriteLine("Request# {0} requires an executive meeting!", 
                e.Purchase.Number);
        }
    }
}

/// <summary>
/// Class that holds request details.
/// </summary>
internal sealed class Purchase
{
    public Double Amount   { get; set; }
    public String Purpose  { get; set; }
    public Int32  Number   { get; set; }
}

希望有帮助。

What you want is the Chain of Responsibility design pattern.

Here is an example in C#:

using System;

internal sealed class Program
{
    private static void Main()
    {
        // Setup Chain of Responsibility.
        Approver larry = new Director();
        Approver sam   = new VicePresident();
        Approver tammy = new President();

        larry.Successor = sam;
        sam.Successor   = tammy;

        Purchase purchase;

        // Generate and process purchase requests.
        purchase = new Purchase { 
          Number = 2034, Amount = 350.00, Purpose = "Supplies" };

        larry.ProcessRequest(purchase);

        purchase = new Purchase { 
          Number = 2035, Amount = 32590.10, Purpose = "Project X" };

        larry.ProcessRequest(purchase);

        purchase = new Purchase { 
          Number = 2036, Amount = 122100.00, Purpose = "Project Y" };

        larry.ProcessRequest(purchase);

        // Wait for user.
        Console.ReadKey();
    }
}

// Purchase event argument holds purchase info.
internal sealed class PurchaseEventArgs : EventArgs
{
    public Purchase Purchase { get; set; }
}

/// <summary>
/// The 'Handler' abstract class.
/// </summary>
internal abstract class Approver
{
    // Purchase event .
    public event EventHandler<PurchaseEventArgs> Purchase;

    // Purchase event handler.
    public abstract void PurchaseHandler(Object sender, PurchaseEventArgs e);

    // Constructor.
    public Approver()
    {
        Purchase += PurchaseHandler;
    }

    public void ProcessRequest(Purchase purchase)
    {
        OnPurchase(new PurchaseEventArgs { Purchase = purchase });
    }

    // Invoke the Purchase event.
    public virtual void OnPurchase(PurchaseEventArgs e)
    {
        if (Purchase != null)
        {
            Purchase(this, e);
        }
    }

    // Sets or gets the next approver.
    public Approver Successor { get; set; }
}

/// <summary>
/// The 'ConcreteHandler' class.
/// </summary>
internal sealed class Director : Approver
{
    public override void PurchaseHandler(Object sender, PurchaseEventArgs e)
    {
        if (e.Purchase.Amount < 10000.0)
        {
            Console.WriteLine("{0} approved request# {1}",
                this.GetType().Name, e.Purchase.Number);
        }
        else if (Successor != null)
        {
            Successor.PurchaseHandler(this, e);
        }
    }
}

/// <summary>
/// The 'ConcreteHandler' class.
/// </summary>
internal sealed class VicePresident : Approver
{
    public override void PurchaseHandler(Object sender, PurchaseEventArgs e)
    {
        if (e.Purchase.Amount < 25000.0)
        {
            Console.WriteLine("{0} approved request# {1}",
                this.GetType().Name, e.Purchase.Number);
        }
        else if (Successor != null)
        {
            Successor.PurchaseHandler(this, e);
        }
    }
}

/// <summary>
/// The 'ConcreteHandler' class.
/// </summary>
internal sealed class President : Approver
{
    public override void PurchaseHandler(Object sender, PurchaseEventArgs e)
    {
        if (e.Purchase.Amount < 100000.0)
        {
            Console.WriteLine("{0} approved request# {1}", 
                sender.GetType().Name, e.Purchase.Number);
        }
        else if (Successor != null)
        {
            Successor.PurchaseHandler(this, e);
        }
        else
        {
            Console.WriteLine("Request# {0} requires an executive meeting!", 
                e.Purchase.Number);
        }
    }
}

/// <summary>
/// Class that holds request details.
/// </summary>
internal sealed class Purchase
{
    public Double Amount   { get; set; }
    public String Purpose  { get; set; }
    public Int32  Number   { get; set; }
}

Hope that helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文