哪种设计模式适合——策略有意义?

发布于 2024-10-09 14:39:56 字数 772 浏览 0 评论 0原文

我有一个简单的数据库表,用于存储通过电子邮件订阅文件夹或显示在网站上(仅在 Web UI 上)的用户列表。在存储表中,这是由数字控制的(1 - 在站点上显示 2 - 通过电子邮件)。当我在用户界面中显示时,我需要在用户订阅的每个文件夹旁边显示一个复选框(电子邮件和现场)。

有一个单独的表存储一组默认订阅,如果用户未表达其订阅,则这些默认订阅将适用于每个用户。这基本上是一个文件夹 ID 和一个虚拟组名称。

但是,电子邮件订阅不会计入应用这些默认组。因此,如果没有“现场”订阅,则应用默认组。这就是规则。

这里的策略模式怎么样(伪代码)

Interface ISubscription
  public ArrayList GetSubscriptionData(Pass query object)

Public class SubscriptionWithDefaultGroup
  Implement ArrayList GetSubscriptionData(Pass query object)

Public class SubscriptionWithoutDefaultGroup
  Implement ArrayList GetSubscriptionData(Pass query object)

Public class SubscriptionOnlyDefaultGroup
  Implement ArrayList GetSubscriptionData(Pass query object)

这是否有意义?我非常高兴收到任何批评/帮助/注释。我在学习。

干杯

I have a simple database table that stores list of users who have subscribed to folders either by email OR to show up on the site (only on the web UI). In the storage table this is controlled by a number(1 - show on site 2- by email). When I am showing in UI I need to show a checkbox next to each of folders for which the user has subscribed (both email & on site).

There is a separate table which stores a set of default subscriptions which would apply to each user if user has not expressed his subscription. This is basically a folder ID and a virtual group name.

But, Email subscriptions do not count for applying these default groups. So if no "on site" subscription apply default group. Thats the rule.

How about a strategy pattern here (Pseudo code)

Interface ISubscription
  public ArrayList GetSubscriptionData(Pass query object)

Public class SubscriptionWithDefaultGroup
  Implement ArrayList GetSubscriptionData(Pass query object)

Public class SubscriptionWithoutDefaultGroup
  Implement ArrayList GetSubscriptionData(Pass query object)

Public class SubscriptionOnlyDefaultGroup
  Implement ArrayList GetSubscriptionData(Pass query object)

does this even make sense? I would be more than glad for receive any criticism / help / notes. I am learning.

Cheers

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

浸婚纱 2024-10-16 14:39:56

下面怎么样呢。我的选择是使用装饰器模式。顺便说一句,我理解你的问题,总是有默认订阅,用户可以额外订阅更多内容。 装饰器用于这种增强,而策略用于替代实现

Interface ISubscription
{
  public List<ISubscription> GetSubscriptions(Pass query object);
}

Public class DefaultSubscritions implements ISubscription {...}

Public class SubscribedSubscritions implements ISubscription 
{
   private ISubscription sub;

   public class SubscribedSubscritions(ISubscription in)
   {
     sub=in;
   }

   public List<ISubscription> GetSubscriptions(Pass query object) 
   {
     List<ISubscription> defaultSubs=sub.GetSubscriptions(...);
     List<ISubscription> additionalSubs=//retrieve the subscribed ones


     List<ISubscription> allSubs=//defaultSubs + additionalSubs

     return allSubs;
   }
}

策略允许您用替代实现来替换某些内容。装饰器透明地增强对象。

How about below. My choice is to use Decorator pattern. By the way I understood your problem, there are always default subscriptions and users can ADDITIONALLY subscribe for more. And Decorator is meant for this kind of ENHANCEMENTS while Strategy is for ALTERNATE IMPLEMENTATIONS.

Interface ISubscription
{
  public List<ISubscription> GetSubscriptions(Pass query object);
}

Public class DefaultSubscritions implements ISubscription {...}

Public class SubscribedSubscritions implements ISubscription 
{
   private ISubscription sub;

   public class SubscribedSubscritions(ISubscription in)
   {
     sub=in;
   }

   public List<ISubscription> GetSubscriptions(Pass query object) 
   {
     List<ISubscription> defaultSubs=sub.GetSubscriptions(...);
     List<ISubscription> additionalSubs=//retrieve the subscribed ones


     List<ISubscription> allSubs=//defaultSubs + additionalSubs

     return allSubs;
   }
}

Strategy allows you to replace something with an alternative implementation. Decorator transparently enhances objects.

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