产生直通代码时"宁愿在继承"组成; [英] Generating pass-through code when "preferring composition over inheritance"
问题描述
问题
比方说,我想一个手机的型号作为普通电话和PDA的结合。这有点多继承场景(手机的是的手机,它的是的PDA)。由于C#不支持多重继承,这几乎呼吁某种成分。 。另外,让我们说,我有别的原因青睐组成反正
Let's say I'm trying to model a cell phone as a combination of a regular phone and a PDA. It's sort of a multiple inheritance scenario (a cell phone is a phone, and it is a PDA). Since C# doesn't support multiple inheritance, this pretty much calls for some kind composition. Plus, let's say that I have other reasons to favor composition anyway.
我一直怀疑的东西:有没有将生成所有不可避免的密码任何工具通过代码自动
让我充实我的例子一些实际的代码:
Let me flesh out my example with some actual code:
< STRONG>接口:
public interface IPhone
{
public void MakeCall(int phoneNumber);
public void AnswerCall();
public void HangUp();
}
public interface IPda
{
public void SendEmail(string[] recipientList, string subject, string message);
public int LookUpContactPhoneNumber(string contactName);
public void SyncWithComputer();
}
实现:
public class Phone : IPhone
{
public void MakeCall(int phoneNumber) { // implementation }
public void AnswerCall() { // implementation }
public void HangUp() { // implementation }
}
public class Pda : IPda
{
public void SendEmail(string[] recipientList, string subject, string message) { // implementation }
public int LookUpContactPhoneNumber(string contactName) { // implementation }
public void SyncWithComputer() { // implementation }
}
的手机类
public class CellPhone : IPhone, IPda
{
private IPhone _phone;
private IPda _pda;
public CellPhone(IPhone phone, IPda pda)
{
_phone = phone;
_pda = pda;
}
public void MakeCall(int phoneNumber)
{
_phone.MakeCall(phoneNumber);
}
public void AnswerCall()
{
_phone.AnswerCall();
}
public void HangUp()
{
_phone.HangUp();
}
public void SendEmail(string[] recipientList, string subject, string message)
{
_pda.SendEmail(recipientList, subject, message);
}
public int LookUpContactPhoneNumber(string contactName)
{
return _pda.LookUpContactPhoneNumber(contactName);
}
public void SyncWithComputer()
{
_pda.SyncWithComputer();
}
}
编写手机类是乏味和错误易发:
所有这些类确实是充当了电话管道
和掌上电脑
类。实在是应该不需要理由的人的努力,键入所有这些直通语句(如 _phone.MakeCall(phoneNumber的);
)。这只是暴露了几个成员字段的公共接口。
All this class really does is act as a conduit for the Phone
and Pda
classes. There is really no reason human effort should be required to type out all these pass-through statements (like _phone.MakeCall(phoneNumber);
). It's just exposing the public interface of a couple member fields.
问题
-
是否有一个工具(最好是免费:)),将节省我从编写直通方法容易出错的乏味?我知道,我可以自动生成使用VS存根,但这只是让我有一半的路。
Is there a tool (preferably free :)) that will save me from the error-prone tedium of writing pass-through methods? I'm aware that I can automatically generate the stubs using VS, but this only gets me half the way there.
您可以率的可行性和可取性这样的功能?难道是值得投入的建议,微软增加这种类型的代码生成的?你会给予好评,如果我这样做?如果没有,你有什么异议?
Can you rate the feasibility and desirability of such a feature? Would it be worth putting in a suggestion to Microsoft to add this type of code generation? Would you upvote it if I did? If not, what objections do you have?
修改
每个人似乎都在说同样的事情:我为什么不只是让 _phone
和 _pda
进入公共属性?我的这种反对意见是,它违反了最少知识原则。我的手机
类的客户端应该只是做一个手机做的事情,它不应该有应对搞清楚哪些功能是电话
的功能,哪些是掌上电脑
功能。这将创建额外的依赖,使手机界面不太明显的特点。
Everyone seems to be saying the same thing: Why don't I just make _phone
and _pda
into public properties? My objection to this is that it violates the "principle of least knowledge". The client of my CellPhone
class should just do the things a cell phone does, it shouldn't have to deal with figuring out which features are Phone
features and which are Pda
features. This creates additional dependencies and makes the features of the CellPhone interface less obvious.
另外,不要只是专注于这个例子。如果我正在写一个适配器?它可能包含几个接口成员,只是直通,但它可能还含有具有独特实施一些独特的成员。我相信有很多情况下,直通代码是好事的,我只是不喜欢写它。
Also, don't focus just on this example. What if I were writing an adapter? It might contain several interface members that are just pass-throughs, but it might also contain some unique members with unique implementation. I believe there are many cases where pass-through code is a good thing, I just don't like writing it.
推荐答案
是的,你可以用辉煌VS插件,生成方法 ReSharper的
Yes, you can generate the methods using the brilliant VS add-in, ReSharper
选择生成委托方法 选项从代码生成菜单(Alt-Insert组合与默认的快捷方式)。
Choose the "generate delegating methods" option from the code-generation menu (Alt-Insert with the default shortcut scheme).
如果你有一个实现接口的类,包含实现现场同样的接口,R#会给你一个选项生成直通代码。它可与任何数量的接口工作,也
Where you have a class that implements an interface, containing a field that implements the same interface, R# will give you an option to generate the pass-through code. It can work with any number of interfaces, too.
这篇关于产生直通代码时"宁愿在继承"组成;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!