如何构造许多类似事件的方法?

发布于 2024-11-15 17:06:15 字数 339 浏览 2 评论 0原文

我正在开发的系统的中心对象变得有点太笨重了。问题在于它可以通过多种不同的方式采取行动。您可以:

  • 取消它
  • 拒绝它
  • 丢弃它
  • 出售它
  • 拆分它
  • 使其过期
  • ......以及更多。

我正在考虑将那些类似事件的方法从类中分解出来,并使其更加通用,就像命令模式一样。但大多数方法都有不同的参数,因此使用像 Run() 或 Execute() 这样的解决方案可能会出现问题?

有什么想法如何构建它,以使其灵活且简单地添加新命令、操作或您想称呼它们的内容吗? (我不会称它们为真实事件,因为没有听众。)

The central object of a system I'm developing is getting a bit too unwieldy. The problem is that it can be acted upon in so many different ways. You can:

  • Cancel it
  • Deny it
  • Discard it
  • Sell it
  • Split it
  • Expire it
  • ... and plenty more.

I'm considering factorizing those event-like methods away from the class and make it a bit more generic, like a Command pattern. But most of the methods have different parameters, so using a solution like Run() or Execute() could be a problem perhaps?

Any ideas how to structure this, to make it flexible and simple to add new commands, or actions or what you would like to call them? (I wouldn't call them real events since there are no listeners.)

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

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

发布评论

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

评论(2

总攻大人 2024-11-22 17:06:15

您可以在其构造函数中传递命令所需的参数吗?这样,您就可以使用无参数命令。

class SellItCommand : ICommand {

  private decimal price;

  SellItCommand(decimal price) {
    this.price = price;
  }

  void Execute() {
    // Do whatever needs to be done with in the sell command using this.price
  }
}

Can you pass the required parameters for your commands in their constructor ? That way, you can have a parameter-less command.

class SellItCommand : ICommand {

  private decimal price;

  SellItCommand(decimal price) {
    this.price = price;
  }

  void Execute() {
    // Do whatever needs to be done with in the sell command using this.price
  }
}
蝶…霜飞 2024-11-22 17:06:15

将参数封装为单个参数:

public class SellCommand: Command {
   Product OwnerProduct;

   ProductCommand (AOwnerProduct)
   {
     this.OwnerProduct = AOwnerProduct;
   }

   public override void Execute(KeyValueArray Parameters)
   {
      double Price = (double)Parameters.ValueofKey("price");
      // do something else
   }
}

public class BuyCommand: Command {
   Product OwnerProduct;

   ProductCommand (AOwnerProduct)
   {
     this.OwnerProduct = AOwnerProduct;
   }

   public override void Execute(KeyValueArray Parameters)
   {
      double Cost = (double)Parameters.ValueofKey("cost");
      // do something else
   }
}


public class Product {
   public void AnyMethod()
   {
     KeyValueArray Parameters =  new KeyValueArray Parameters();
     KeyValueArray.Add("price", "12.5");
   } 
}

它是一种伪代码,您可能喜欢使用与您的编程框架相匹配的集合库。

Encapsulate the parameters, as a single parameter:

public class SellCommand: Command {
   Product OwnerProduct;

   ProductCommand (AOwnerProduct)
   {
     this.OwnerProduct = AOwnerProduct;
   }

   public override void Execute(KeyValueArray Parameters)
   {
      double Price = (double)Parameters.ValueofKey("price");
      // do something else
   }
}

public class BuyCommand: Command {
   Product OwnerProduct;

   ProductCommand (AOwnerProduct)
   {
     this.OwnerProduct = AOwnerProduct;
   }

   public override void Execute(KeyValueArray Parameters)
   {
      double Cost = (double)Parameters.ValueofKey("cost");
      // do something else
   }
}


public class Product {
   public void AnyMethod()
   {
     KeyValueArray Parameters =  new KeyValueArray Parameters();
     KeyValueArray.Add("price", "12.5");
   } 
}

Its sort of pseudocode, you may like to use the collection libraries that match your programming framework.

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