- Jon Skeet - < sk *** @ pobox.com> http://www.pobox.com/~skeet 如果回复该群组,请不要给我发邮件。 /> A couple of possibilities: 1) Don''t have a single base class in the first place - split it intoseveral. This sounds like it would be a good idea anyway. 2) Change DalBase to several interfaces, and have a proxy class whichimplements all the interfaces. Split DalMain into several smallerclasses, each implementing a single interface. The proxy class wouldcreate an instance of each of the smaller classes, and forward themethod calls for each interface to the appropriate implementation. --Jon Skeet - <sk***@pobox.com> http://www.pobox.com/~skeetIf replying to the group, please do not mail me too " Jon Skeet [C#MVP]" < SK *** @ pobox.com>写道: "Jon Skeet [C# MVP]" <sk***@pobox.com> wrote: 几种可能性: 1)首先没有一个基类 - 将其拆分为一些。这听起来好像无论如何都是个好主意。 2)将DalBase更改为多个接口,并拥有一个代理类,实现所有接口。将DalMain拆分为几个较小的类,每个类实现一个接口。代理类将创建每个较小类的实例,并将每个接口的方法调用转发到适当的实现。 A couple of possibilities: 1) Don''t have a single base class in the first place - split it into several. This sounds like it would be a good idea anyway. 2) Change DalBase to several interfaces, and have a proxy class which implements all the interfaces. Split DalMain into several smaller classes, each implementing a single interface. The proxy class would create an instance of each of the smaller classes, and forward the method calls for each interface to the appropriate implementation. Jon 谢谢!我真的很喜欢你所解释的第二个想法的声音。 从我的解决方案和代理设计模式的阅读(我看起来是 up),这是我对你的建议的理解: 1)DalBase类改为更小的接口: IDalBalloon和IDalLiquid 2)将DalMain分成较小的类,每个类实现一个较小的接口: //气球 - 现在包含Float()的具体实现 公共类气球:IDalBalloon { public void Float() {。} } //液体 - 现在包含Flow的具体实现() 公共类液体:IDalLiquid { public void Flow() {。} } 3)H ave一个代理,实现所有接口并创建一个实例 的各个类: 公共类DalProxy:IDalBalloon,IDalLiquid { //会员 //某种形式的实例化 气球气球=新气球(); //施放到IDalBalloon以检查它是否是IDalBalloon IDalBalloon isBalloon = balloon作为IDalBalloon; 液体液体=新液体(); //施放到IDalLiquid以检查它是否是IDalLiquid IDalLiquid isLiquid =液体作为IDalLiquid; Public void Float() { if(isBalloon!= null) isBalloon.Float(); else 抛出新的异常(气球必须 实施IDalBalloon); } Public void Flow() { if(i液体!= null) isLiquid.Flow(); 其他 扔新品例外(Liquid必须 实施IDalLiquid); } } 4)然后在客户端。 DalProxy p =新的DalProxy; //调用Flow() p.Flow(); //调用Float() p。 Float(); 这解决了我的两个问题中的第一个问题:具有 类的逻辑层,它们执行所有具体实现并且必须是 可互换。换句话说,我现在可以拥有一个SQL类库: (实现IDalBalloon的SqlBalloon)和Oracle 类的替代库(实现IDalBalloon的OracleBalloon)。 但是,使用一个代理类(DalProxy)来调用方法 会出现同样的问题,即我在我的中遇到一个DalMain 解决方案。这意味着让编辑DalProxy的团队排在队伍中的开发瓶颈仍然存在! 但好的是,我可以拥有专业代理类的数量, DalBalloonProxy和DalLiquidProxy分别实现了IDalBalloon和 IDalLiquid接口,每个接口都对他们的方法负责 具体来说。 再次感谢。 Jon Thanks! I really like the sound of the second idea you''ve explained. From my reading of your solution and the Proxy design pattern (which Ilooked up), this is my understanding of your suggestion: 1) The DalBase class is changed to smaller interfaces: IDalBalloon and IDalLiquid They contain definitions for Float() and Flow() respectively. 2) Split DalMain into smaller classes each implementing a smaller interface: // Balloon - now contains a concrete implementation of Float() public class Balloon : IDalBalloon { public void Float() {.} } //Liquid - now contains a concrete implementation of Flow() public class Liquid : IDalLiquid { public void Flow() {.} } 3) Have one proxy which implements all the interface and creates an instanceof the individual classes: public class DalProxy : IDalBalloon, IDalLiquid { //Members //some form of instantiation Balloon balloon = new Balloon(); //cast to IDalBalloon to check if it is an IDalBalloon IDalBalloon isBalloon = balloon as IDalBalloon; Liquid liquid = new Liquid(); //cast to IDalLiquid to check if it is an IDalLiquid IDalLiquid isLiquid = liquid as IDalLiquid; Public void Float() { if (isBalloon != null) isBalloon.Float(); else throw new Exception("Balloon mustimplement IDalBalloon"); } Public void Flow() { if (iLiquid != null) isLiquid.Flow(); else throw new Exception("Liquid mustimplement IDalLiquid"); } } 4) Then in a client. DalProxy p = new DalProxy; //invoke Flow() p.Flow(); //invoke Float() p.Float(); This solves the first of my two problems: that of having a logical layer ofclasses which do all the concrete implementation and have to beinterchangeable. In other words I can now have a library of SQL classes:(SqlBalloon implementing IDalBalloon) and an alternative library of Oracleclasses (OracleBalloon implementing IDalBalloon). However, using one single Proxy class (DalProxy) for invoking the methodswould present the same problem of having one DalMain, that I faced in mysolution. It means that the development bottleneck of having the team liningup to edit DalProxy would still be there! But the good thing is, I can have a number of specialist Proxy classes,DalBalloonProxy and DalLiquidProxy which each implements the IDalBalloon andIDalLiquid interfaces respectively, and each is responsible to their methodsspecifically. Thanks again. 这篇关于拆分继承的类以跨团队共享的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-24 18:54