我们如何避免冗余接口实现? [英] How can we avoid redundant interface implementation?

查看:134
本文介绍了我们如何避免冗余接口实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我有一个极客的设计问题。我将提出一个假设的情况。

I have a design question for the geeks. I shall present a hypothetical situation.

我的应用程序需要几个接口来制作对象可用。让我们说它们是;

1。       
IAdult.cs,有两个属性

1.       IAdult.cs, with two properties

a。       
年龄

a.       Age

b。      
LicenceNo。

b.      LicenceNo.

2.       
IEducated.cs,有两种方法

2.       IEducated.cs, with two methods

a。       
读取()

a.       Read()

b。 写()

b.      Write()

< span style ="font-family:Calibri"> 如果我们必须从这些接口中实现一个抽象类,我们可以简单地创建一个类,比如抽象类EducatedAdult {} 。让我们假设这些接口的
实现在大多数时候都保持不变。

< span style ="font-family:Calibri; font-size:small"> 1.       ;&NBSP;
Carpenter.cs(源自A类)

1.       Carpenter.cs (derives from class A)

2.       
Plumber.cs(源自B类)

2.       Plumber.cs (derives from class B)

3。       
Programmer.cs(源自C类)

3.       Programmer.cs (derives from class C)

(此处A,B,C类属于外部库)现在要使这些对象可用于在程序中,他们需要实现上面给出的接口。因此问题
是对于这些类中的每一个,我必须明确地实现上述接口。是否有更好的设计,我不必在所有类中冗余地实现接口。这可能是一个解决方案,如果可能是
;

class EducatedAdult< t> :t,IAdult,IEducated {<接口的实现> }

稍后,也许,我可以初始化我的派生类,如;

Carpenter carpenter = new EducatedAdult< Carpenter>()。

这不是一个理想的解决方案,因为木工对象的公共接口仍然不包括我的接口成员(不需要
铸造) )。但那么,什么是最合适的设计呢?

推荐答案

接口可以通过合同访问对象。如果你作为木匠访问它为什么需要实现IAdult或IEducated?。如果你需要访问IAdult adult = new Carpenter(); IAdult adult2 = new Driver();或IEducated educated = new Carpenter();
那么你可以有一个超类说成人实现那些接口并将这些方法定义为虚拟然后那些派生类可以根据需要覆盖那些方法。合同仍然可行。



类EducatedAdult< t> ;:t,IAdult,IEducated {<接口的实现>这是可能的,但是
Carpenter carpenter = new EducatedAdult< Carpenter> ()。这不会起作用,因为木匠不同意这些合同和受过教育的成年人没有办法与木匠有关。

Interfaces are there to access the objects through a contract. If you access it as carpenter why need to implement IAdult or IEducated?. If you need to access IAdult adult= new Carpenter(); IAdult adult2=new Driver(); or IEducated educated=new Carpenter(); then you can have a super class say Adult which implement those interfaces and define those methods as virtual then those derived classes can override on not those methods upon requirement. The contract is still viable.

class EducatedAdult<t>: t, IAdult, IEducated { <implementation of interfaces> } this is possible, but Carpenter carpenter = new EducatedAdult<Carpenter>(). this will not work as carpenter does not agreed with those contracts and Educated adult no way related to carpenter.

如果对象需要使用两种行为/功能...例如,IAdult由Adult类实现,IEducated由以下实现:受过教育的班级,然后另一个班级想要使用这两个类功能然后 不建议
尝试遵循继承模式,而是遵循依赖注入路由,战略模式。

If two kind of behaviors/ functionality an object need to use.. for example IAdult is implemented by Adult class and IEducated is implemented by Educated class, then an another class want to use these two class functionality then  it is not advisable try to follow an inheritance pattern, instead follow dependency injection route, the strategic pattern.

你可以使用这样的东西..

You can use something like this..

 

 


 public interface IAdult
 {
  bool isAdult();
 }
 public interface IEducated
 {
  bool isEducated();
 }

 public class Educated:IEducated
 {

  public virtual bool isEducated()
  {
 	  return true;
  }
 }

 public class Adult : IAdult
 {

  public virtual bool isAdult()
  {
   return true;
  }
 }

 public class SeniorEmployee : Adult, IAdult
 {

 }
 public class Student :Adult, IAdult
 {
  public override bool isAdult()
  {
   return false;
  }
 }

 public class Child : Educated
 {
  public override bool isEducated()
  {
   return false;
  }
 }
 public class Manager : Educated
 {
 
 }

 public class Person
 {
  public string Name { get; set; }
  public Adult adult { get; set; }
  public Educated educated { get; set; }
  public static implicit operator Adult(Person obj)
  {
   return obj.adult;
  }

  public static implicit operator Educated(Person obj)
  {
   return obj.educated;
  }
 }



 public class Test
 {
  public bool test()
  {
   Person person = new Person();
   person.Name = "Person1";
   person.adult = new Student();
   person.educated = new Child();

   IEducated educated = (Educated)person;
   IAdult adult = (Adult)person;
   return adult.isAdult();
  }
 }


这篇关于我们如何避免冗余接口实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆