Reputation:
I have three classes: InputSocket
, InternalSocket
, OutputSocket
. They all inherit from BaseSocket
with implements ISocket
interface, where I have common code for all classes and this part works fine, I have problem with partially shared code.
InputSocket
and InternalSocket
shares Parsing part with OutputSocket
does not have.
InputSocket
and OutputSocket
shares GUI part with InternalSocket
does not have.
I'm looking for a way to share common parts of code between InputSocket
/ InternalSocket
and InputSocket
/ OutputSocket
. Code with consist of methods and parameters.
I already tried using multiple interfaces but it didn't solve the problem, well it made it a little more manageable. When I create IParsable
for parsing part and IExternal
for Input/Output common part, then I need to create IParsableSocket
for InternalSocket
IParsableExternalSocket
for InputSocket
and IExternalSocket
for OutputSocket
. It does not change the point that I still need to implement code for every case, with leads to code duplication.
Unfortunately there is no way for InputSocket
to inherit from OutputSocket
and InternalSocet
at the same time in c#.
Is there an alternative, to copy/paste of code or making an general Socket class with bunch of if logic and nulls?
Upvotes: 1
Views: 91
Reputation: 54628
(You should suffix with base, not prefix with base)
So you have:
public class InputSocket : BaseSocket { }
public class InternalSocket : BaseSocket {}
public class OutputSocket : BaseSocket {}
public abstract class BaseSocket : ISocket { }
public interface ISocket {}
InputSocket and InternalSocket shares Parsing part with OutputSocket does not have.
public interface IParsingSocket {}
public class InputSocket : BaseSocket, IParsingSocket { }
public class InternalSocket : BaseSocket, IParsingSocket {}
public class OutputSocket : BaseSocket {}
InputSocket and OutputSocket shares GUI part with InternalSocket does not have.
public interface IParsingSocket {}
public interface IGUISOcket {}
public class InputSocket : BaseSocket, IParsingSocket, IGUISOcket { }
public class InternalSocket : BaseSocket, IParsingSocket {}
public class OutputSocket : BaseSocket, IGUISOcket {}
Implementation using Extension Methods:
public static class IParsingSocketExtensions
{
public static void Parse(this IParsingSocket) {}
}
public static class IGUISOcketExtensions
{
public static void DoGUI(this IGUISOcket) {}
}
Now you can
var inps = new InputSocket();
var ints = new InternalSocket();
var outs = new OutputSocket();
inps.Parse() // valid
ints.Parse() // valid
outs.Parse() // invalid
inps.DoGUI() // valid
ints.DoGUI() // invalid
outs.DoGUI() // valid
Upvotes: 1
Reputation: 4078
I do not think inheritance
is the right solution for you here. You should not have methods in your interface which you do not intend to use/ implement in your inherited class. This is violation of Interface Segregation Principle
The interface-segregation principle (ISP) states that no client should be forced to depend on methods it does not use
In your case, it means BaseSocket
should NOT be a God class having all the "common" methods. Instead you would need multiple interfaces and implementation which does one specfic operation, for example, Parse
and Gui
in your case.
Your different Socket
classes then get Composed
of those behaviour/ operations instead of inhert
ing. This is known as Composition over Inheritance
Composition over inheritance in object-oriented programming is the principle that classes should achieve polymorphic behavior and code reuse by their composition rather than inheritance from a base or parent class. This is an often-stated principle of OOP, such as in the influential book Design Patterns.
Hope this helps.
Upvotes: 1