简体   繁体   English

强制执行方法

[英]Forcing the execution of a method

I'm writing a grocery cart simulation app 我正在写一个购物车模拟应用程序

Here is my situation, I want to force a type to execute a method it has defined for itself... 这是我的情况,我想强制一个类型来执行它为自己定义的方法......

Basically I have an interface 基本上我有一个界面

interface IGroceryCart
{
   // Other methods snipped for clarity
   void Checkout(IEnumerable<IGroceryItem> itemsToBuy);
}

Now the IGroceryItem interface looks like this 现在IGroceryItem接口看起来像这样

interface IGroceryItem
{
   // Other methods snipped for clarity
   CheckoutIssues EnsureFreshness();
}    

IGroceryCart and IGroceryItem have concrete implementations obviously, but what I am trying to do is this: IGroceryCart和IGroceryItem显然有具体的实现,但我想要做的是:

When I call Checkout on a grocery cart, I want all the items that are being checked out to internally call their EnsureFreshness() method and then react accordingly if one or more items had CheckoutIssues 当我在购物车上调用Checkout时,我希望所有正在检出的项目在内部调用他们的EnsureFreshness()方法,然后相应地做出反应,如果一个或多个项目具有CheckoutIssues

CheckoutIssues is just something like CheckoutIssues就是这样的

class CheckoutIssues
{
   string Description {get;set;}
   //etc
}

What would be the best way to implement a way that EnsureFreshness has been called on each grocery item? 实现在每个杂货项目上调用EnsureFreshness的方式的最佳方法是什么? Or an alternate approach that is better to get these kind of errors? 或者更好地获得这些错误的替代方法? Should I go the custom validator route? 我应该去自定义验证器路线吗? Each grocery item may have its own way of checking freshness, milk would be different from Eggs for example, so I have to rely on each individual implementation, but I'd like to force that call on checkout, hope that makes sense. 每个杂货项目可能有自己检查新鲜度的方式,例如牛奶与鸡蛋不同,所以我必须依赖每个单独的实施,但我想强制结账,希望这是有道理的。

You could just put it in your Checkout method, and add the returned object to some collection to deal with: 您可以将它放在Checkout方法中,并将返回的对象添加到某个集合中以处理:

List<CheckoutIssues> issues = new List<CheckoutIssues>();
foreach (IGroceryItem item in itemsToBuy) {
    issues.Add(item.EnsureFreshness());
}

Well lets say you go to Walmart and add items to the Cart , is it the duty of the Cart to checkout or is it the duty of the employer at counter to checkout? 好吧,让我们说你去沃尔玛并向Cart添加物品,是否有Cart的结账义务,或者是结账时雇主的义务?

I feel it is wired to have method Checkout on Shopping Cart . 我觉得在购物Cart有方法Checkout是有线的。 If I were you I would probably take this route. 如果我是你,我可能会走这条路。

 interface IGroceryCart
{

   /**void Checkout(IEnumerable<IGroceryItem> itemsToBuy);**/
   void AddItem(IEnumerable<IGroceryItem> itemsToBuy)//I like to add items to Grocery Cart.
}

I should be able to add Item to the GroceryCart. 我应该能够将物品添加到GroceryCart。

The second issue is when you add Apples to the cart in Walmart, the apple does not know if it fresh or expired or rotten. 第二个问题是当你将Apples添加到沃尔玛的购物车时,苹果不知道它是新鲜还是过期或腐烂。

    interface IGroceryItem
    { 
       CheckoutIssues EnsureFreshness(); //I am skeptic about having this method on GroceryItem
    }   

    class GroceryCart:IGroceryCart
    {
       public void AddItem(IEnumerable<IGroceryItem> itemsToBuy)
       {

       }
    }

    class Billing
   {


    public decimal BillItems(GroceryCart cart)
    {
         foreach item in cart
           if(itemIsfresh)
             Bill it.
    } 



     private bool IsItemFresh(GroceryItem item)
     {

     }

  }

What is wrong with what you have? 你有什么问题? Why not just iterate through your itemsToBuy and call EnsureFreshness on each? 为什么不迭代你的itemsToBuy并在每个上调用EnsureFreshness? If any errors come back, Checkout could throw an Exception, or it could be changed to return the list of errors, or just the first error. 如果有任何错误返回,Checkout可能会抛出异常,或者可能会更改它以返回错误列表,或者只返回第一个错误。

Does that help? 这有帮助吗?

What not having something like that: 什么没有这样的东西:

public abstract GroceryCart : IGroceryCart
{
    public void Checkout(IEnumerable<IGroceryItem> itemsToBuy)
    {
        foreach (var item in itemsToBuy)
        {
            item.EnsureFreshness();
        }

        this.InternalCheckout(itemsToBuy);
    }

    protected abstract void InternalCheckout(IEnumerable<IGroceryItem> itemsToBuy);
}

Now just inherit from your abstract class rather than implementing the interface. 现在只是从您的抽象类继承而不是实现接口。 And in your third party classes have a reference to GroceryCart rather than IGroceryCart when you need to enfore that freshness is checked. 并且在您需要检查新鲜度时,在您的第三方类中引用GroceryCart而不是IGroceryCart Where it's not mandatory, continue to use IGroceryCart . 如果不是强制性的,继续使用IGroceryCart

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM