简体   繁体   English

试图避免类结构中的多个子类型

[英]Trying to avoid multiple sub-types in a class structure

I have two abstract classes Business & Person. 我有两个抽象类Business&Person。 The problem is that I have a customer type that can be either a Business or a Person. 问题是我有一个可以是商业或个人的客户类型。 Is there a way to model this so that I don't have both CustomerBusiness and CustomerPerson classes? 有没有办法对此进行建模,以便我没有CustomerBusiness和CustomerPerson类? Multiple inheritance isn't an option as this is C#. 多重继承不是一种选择,因为这是C#。 I've been looking at this so long I can't see the forest for the trees. 我一直在看这个东西,以至于看不到树木的森林。

public abstract class Business {
  public string Name { get; set; }
}

public abstract class Person {
  public string FirstName { get; set; }
  public string LastName { get; set; }
  public string MiddleName { get; set; }
  public DateTime? BirthDate { get; set; }
  public string Comments { get; set; }
}

public class CustomerBusiness : Business, CustomerRoles {
  public bool BillTo { get; set; }
  public bool ShipTo { get; set; }
  public bool DeliverTo { get; set; }
  public EntityType Type { get; set; }
}

public class CustomerPerson : Person, CustomerRoles {
  public bool BillTo { get; set; }
  public bool ShipTo { get; set; }
  public bool DeliverTo { get; set; }
  public EntityType Type { get; set; }
} 

public interface CustomerRoles {
  bool BillTo { get; set; }
  bool ShipTo { get; set; }
  bool DeliverTo { get; set; }
}

You could prefer composition over inheritance having the Business and Person classes "have a" ICustomerRoles instead of "is a" ICustomerRoles . 您可能更喜欢使用BusinessPerson类“拥有” ICustomerRoles而不是“是” ICustomerRoles

public class Business
{
    public string Name { get; set; }

    public ICustomerRoles CustomerRoles { get; set; }
}

public class Person
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string MiddleName { get; set; }
    public DateTime? BirthDate { get; set; }
    public string Comments { get; set; }

    public ICustomerRoles CustomerRoles { get; set; }
}

I would say that you have your heirarchy in the wrong order. 我要说的是,您的继承制度顺序错误。 Customer should be a base class and then Business and Person should be implementations of the Customer class. 客户应该是基类,然后业务和人员应该是客户类的实现。 I'm sure there are more attributes of a Customer that are common to all types of Customer other than those in the CustomerRoles interface. 我确信除了CustomerRoles界面中的客户之外,所有类型的客户都有更多的客户属性。

I would have thought there would be things like CustomerCode etc. Also you could state that all customers have a Name property, and it's up to the getter in each sub class to return the appropriate value. 我原以为会有类似CustomerCode等的东西。另外你可以说所有客户都有一个Name属性,并且由每个子类中的getter决定是否返回适当的值。 As in a Business would have a single property called Name, whereas a person would have each of FirstName, MiddleName, Surname and also a Name property that would concatenate these in some way. 因为在Business中有一个名为Name的属性,而一个人将拥有FirstName,MiddleName,Surname和Name属性,它们将以某种方式连接这些属性。

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

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