M
M
mletov2018-03-14 16:04:40
OOP
mletov, 2018-03-14 16:04:40

Which pattern is better to use when you need to implement methods from different classes?

Please tell me:
There is an interface/abstract class A
It has 2 methods without implementation of M1 and M2
There is a class B inherited from A
There is a class C inherited from A
Both have their own implementation of methods M1 and M2
Suppose I need to create a class D, in where M1 contains an implementation of class B, and method M2 contains an implementation of class C.
There is no multiple inheritance in C#. Is there any generally accepted solution or ready-made pattern for such situations?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
#
#, 2018-03-14
@mletov

there is a great tool - extensions .. create any orgy ...
ps By the way, extensions wean you from the habit of producing extra classes due to 1-2-3 methods ..;))

M
Maxim Fedorov, 2018-03-14
@qonand

Suppose I need to create a class D, in which M1 contains the implementation of class A, and the method M2 contains the implementation of class B.

Problems like this usually arise from poorly designed classes, in particular when classes "take on too much". You should describe at least in general terms what kind of classes and methods you have

S
Sergey, 2018-03-14
@red-barbarian

not knowing how it happened)
Perhaps because of the overloaded interface. (it's very important to stick to thin interfaces. e.g. interface M1 interface M2)
But allow (maybe) so
D implements the interface, but delegates the execution of M1 to class B, M2 to class C
i.e. delegation pattern

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question