Partial Class or Chain Inheritance

In my opinion, partial classes frowned a bit on professional developers, but I ran into a problem:

I made an implementation of the RichTextBox control, which uses user32.dll calls to edit large texts more quickly. This leads to quite a lot of code. Then I added the spelling checker controls, this was done in another class that inherits the RichTextBox control. It also makes up some code.

These two functions are quite different, but I would like them to be combined so that I can remove one control in my form that has both quick editing and spell checking capabilities. I feel that just adding the code form of one class to another will lead to a too large code file, especially since there are two very different areas of functionality, so I seem to need a different approach.

Now to my question; To combine these two classes, I have to make RichTextBox spell check inherit from quick editing, which in turn inherits RichTextBox? Or should I make two classes partial for one class and thereby make them more "equal", so to speak?

This is more about OO principles and an exercise on my part than about trying to reinvent the wheel; I know that there are many good text editing tools. But this is just a hobby for me, and I just want to know how this decision will be managed by a professional.

Thanks!

+4
source share
2 answers

I'm not quite sure if I understand what you are trying to do, but it seems to me that you are just looking for the Decorator design template . If this does not solve your problem enough, and you really think about creating classes from signs, look at the political design , although I'm not as much as possible in C #. There is also this book, The Modern C ++ Design , which advocates for policy-based development, but it discusses trade-offs for what you call partial classes against (multiple) inheritance. The problem with chain inheritance is to determine the order, because this order creates strong dependencies, and if you add SpellChecking to RichTextEdit, you will have a problem if you want to use SpellChecking for, for example, SearchBox, which could be SimpleEdit, but it would be nice provide spell checking for users ... I hope this helps at least a little.

+2
source

Here's an argument for multiple inheritance!

You can define an interface for functionality (IFastEditTextBox and ISpellCheckingTextBox) and implement methods in each of them. It will be more OO, but there is potential for copy and paste code.

There are no problems with using partial classes. The only fundamental problem with them is that they can encourage a developer who is already leaning towards a procedural approach to everything in the same class.

+1
source

Source: https://habr.com/ru/post/1308444/


All Articles