c# - Implement common behaviour in alternative to abstract base classes? -


In C #, I have a class hierarchy with some basic classes located at the top and a proper number of derivative classes I

Some of these solid sections have some common properties and methods that have been implemented equally. It kills me as useless and such a solution may be to apply this general behavior to another abstract base class.

  Abstract class control; Abstract class square control: control {public Int SquarishProperty; Public Zero SquarishMethod (); }; Class window: Square control; Square buttons: Square control;  

However, if many other classes in the hierarchy share any other behavior but share it with one of the other base classes in control? Maybe there are so many areas of equality that it will not be impractical to make models with the implementation of basic basic classes?

  Abstract class flashable control: control {public int flash property; Public ZeroFlashMethod (); }; Class status: Flash controllable; / / But this is also a little square, hmm ...  

How do you share classes without using classes based on this kind of implementation?

I imagine I want to hand over the implementation of an interface to another class and implement those qualities and methods on behalf of the desired sections in that category, so that the user gets the status and window one Appears to support the standard interface, but under the cover it is something else that implements it.

I can imagine the consolidated sections implementing this behavior, but is it fair and is there any harm? what are the options?

Thanks

You can use a pattern like this:

Public Interface ICommonServices {string SomeProperty {get; Set; } Some method (string is absolute) zero; } Public static class CommonServiceMethods {Public Static Zero DoSomething (this is ICommonServices services, String param) {services.SomeMethod (services.SomeProperty + ":" + Ultimate + "Extra Something!);}} < P> All classes that implement ICommonServices now also get some free behavior through the extension method, which all depend on those features that are encountered by ICommonServices implementers. If you need access to the base class functionality You can put it in your own interface and apply it to ICommonServices. Now you can create 'default' extension functionality for the interface without using multiple base classes.


Edit

If you can make some of these methods internal, then modify the pattern in this way:

  public Class MyObject: IServices {public string public key Prti {get; private set;} string IServices.SomeProperty {get; set;} void Iervices.SomeMethod (string param) {// do something ...}} public interface IPublicServices {string PublicProperty {get; }} Internal Interface ISVARIS: IPublicServices {string SomeProperty {get; Set; } Some method (string absolute) zero; } Internal Static Class Service Methods {Public Static Zero DoSomething (This isiservice services, string absolute) {services.SomeMethod (services.SomeProperty + ":" + Ultimate + "Extra Something!);}}  

In fact, we are exposing both the public and internal interface. Note that we clearly apply the methods of internal interface so that the methods for public use can not be available (because the public customer interface is not available. Can not use Raksha.) In this case, the supporting extension methods are internal, internal interface, however you can also create public accessory methods that rely on the public interface.


Comments