简体   繁体   English

哪种设计模式最适合每种客户方法的多种实现方式?

[英]What design pattern best fits this multiple implementations per customer method?

I have customers and each customer has a method to generate a reference number, but the reference number is different for each customer. 我有客户,每个客户都有一种生成参考号的方法,但是每个客户的参考号不同。 I have an implementation below, but I am curious if there are any scalability issues and what design pattern is this or what design pattern would best fit this scenario: 我在下面有一个实现,但是我很好奇是否存在任何可伸缩性问题,这是什么设计模式,或者哪种设计模式最适合这种情况:

//All customers will implement this.
public interface ICustomer
{

}

//All customers will implement this.
public interface IReferenceNumber
{
    string GenerateReferenceNumber();
}

   public class CustomerOne : ICustomer, IReferenceNumber
{
    public string GenerateReferenceNumber()
    {
        return "Reference Implemenation 1";
    }
}

public class CustomerTwo : ICustomer, IReferenceNumber
{
    public string GenerateReferenceNumber()
    {
        return "Reference Implemenation 2";
    }
} 

Here is how I might call it: 我可以这样称呼它:

Dictionary<string,ICustomer> customers = new Dictionary<string,ICustomer>();

customers.Add("CustomerOne",new CustomerOne());
customers.Add("CustomerTwo",new CustomerTwo());

CustomerOne customerOne = (CustomerOne)customers["CustomerOne"];
CustomerTwo customerTwo = (CustomerTwo)customers["CustomerTwo"];

Console.WriteLine(customerOne.GenerateReferenceNumber());
Console.WriteLine(customerTwo.GenerateReferenceNumber());

Here are some more specific questions: 以下是一些更具体的问题:

  1. The only reason why I had ICustomer was so I didn't have to do something like Dictionary<string,object> customers = new Dictionary<string,object>() , but now I am wondering if the IReferenceNumber is unnecessary and GenerateReferenceNumber should be moved into ICustomer 我拥有ICustomer的唯一原因是,我不必做像Dictionary<string,object> customers = new Dictionary<string,object>()之类的IReferenceNumber ,但是现在我想知道是否IReferenceNumber是不必要的, GenerateReferenceNumber应该是移至ICustomer

Also, I hate having to do CustomerOne customerOne = (CustomerOne)customers["CustomerOne"]; 另外,我讨厌不得不做CustomerOne customerOne = (CustomerOne)customers["CustomerOne"]; . Is there a way I can just get the Customer back and call the method on it without having to explicitly cast? 有没有一种方法可以让我返回Customer并在其上调用方法而无需显式强制转换?

It seems based on comments, I should just have something like: 根据评论看来,我应该像这样:

public class Customer
{
   public string GenerateReferenceNumber()
   {
       //Logic
       return "Default reference number";
   }
}

But if each customer requires unique logic to generate a reference number, how can I only keep one Customer object and override the behavior unless I have multiple GenerateReferenceNumber methods or one huge if else statement or different concrete implementations of customers that can use the default reference number or their own unique logic to generate one. 但是,如果每个客户都需要唯一的逻辑来生成参考号,那么除非我有多个GenerateReferenceNumber方法或one huge if else statement或可以使用默认参考号different concrete implementations of customers否则如何只保留一个Customer对象并覆盖行为?或自己产生的逻辑。

You can move the GenerateReferenceNumber() method to ICustomer interface and use it in dictionary much simpler: 您可以将GenerateReferenceNumber()方法移至ICustomer接口,并在字典中使用它更加简单:

//All customers will implement this.
public interface ICustomer
{
    string GenerateReferenceNumber();
}

public class CustomerOne : ICustomer
{
    public string GenerateReferenceNumber()
    {
        return "Reference Implemenation 1";
    }
}

public class CustomerTwo : ICustomer
{
    public string GenerateReferenceNumber()
    {
        return "Reference Implemenation 2";
    }
} 


Dictionary<string,ICustomer> customers = new Dictionary<string,ICustomer>();

customers.Add("CustomerOne",new CustomerOne());
customers.Add("CustomerTwo",new CustomerTwo());

var customerOne = customers["CustomerOne"];
var customerTwo = customers["CustomerTwo"];

Console.WriteLine(customerOne.GenerateReferenceNumber());
Console.WriteLine(customerTwo.GenerateReferenceNumber());

Or if you really want to keep both interfaces you can make the ICustomer derived from IReferenceNumber: 或者,如果您确实想保留两个接口,则可以使ICustomer继承自IReferenceNumber:

public interface ICustomer : IReferenceNUmber
{

}

and then the usage is the same - no cast required. 然后用法是一样的-不需要强制转换。

As for design pattern question, I don't see any specific design pattern that should be used here. 至于设计模式问题,我看不到这里应使用的任何特定设计模式。 It is basic case of inheritance, not the more complex behavior which has design pattern to implement it as a general approach. 它是继承的基本情况,而不是更复杂的行为,后者具有将其实现为一般方法的设计模式。 So no need for complicating this code with design patterns. 因此,无需将此代码与设计模式复杂化。

Using Composition over inheritance , I'd come up with something like this: 在继承上使用Composition ,我会想到这样的东西:

public class Customer
{
    private readonly IReferenceNumberGetter ReferenceNumberGetter;

    public Customer(IReferenceNumberGetter referenceNumberGetter)
    {
        ReferenceNumberGetter = referenceNumberGetter;
    }

    public string GenerateReferenceNumber()
    {
        return ReferenceNumberGetter.GenerateReferenceNumber();
    }

    // other Customer stuff
}

public interface IReferenceNumberGetter
{
    string GenerateReferenceNumber();
}

public class ReferenceNumberGetterOne : IReferenceNumberGetter
{
    public string GenerateReferenceNumber()
    {
        return "4";
    }
}

public class ReferenceNumberGetterTwo : IReferenceNumberGetter
{
    public string GenerateReferenceNumber()
    {
        return "42";
    }
}

And use it like this: 并像这样使用它:

var customerOne = new Customer(new ReferenceNumberGetterOne()); 
var customerTwo = new Customer(new ReferenceNumberGetterTwo()); 

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

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