Click here to Skip to main content
15,888,590 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
Hi. I am developing graphical block programming tool[^] in WPF but it seems I've got myself into problem.
As I am basically using rectangles to draw blocks and lines to draw wires each, successive class that extends basic rectangle or line class adds more functionality to that particular object.

So for example let me take wire class hierarchy :
1. WireBaseClass - has line object, defines : WireName, WireOrientation, StartPosition, EndPosition and so on..
2. WireConnectableClass - extends WireBaseClass, adds functionality to connect two wires together as double linked list.
2. WireMoveClass - extends WireBaseClass, adds functionality to move wire

So now I have few questions/problems :
a) I've started using hierarchically highest wire (at that time - WireMoveClass) throughout project as I defined new wire objects. Now I am facing problem how to extend WireMoveClass (to add more functionality) without changing my whole project?

b) How to 'upgrade' already instantiated class to hierarchically higher (to give it more functionality) once it met few requirements. For example :
WireReferenceClass should add functionality to WireMoveClass. Do I have to copy each attribute form WireMoveClass to WireReferenceClass to achieve that?


I guess, in another words, I am struggling with inheritance.
Any help is much appreciated and welcome.

Kind regards
Posted
Comments
Sergey Alexandrovich Kryukov 9-Nov-15 9:17am    
What do you mean "how"? By doing appropriate software development work: refactorization, redesign, thinking, planning, and so on. You did not point out a single technical problem. If you need to extend a class, move it, just do it. Are you expecting a miraculous trick? There is no such thing.
—SA
hrvoje59 9-Nov-15 9:23am    
Thank you for response. Of course I am not expecting miraculous trick. But I would not complain about it.
Problem that I am facing does not appear to be technical nor simply solvable. I did not post question to get hostile question in return but rather guidance. So you say refactorization is necessary. How to achieve that in this stage?
Nathan Minier 9-Nov-15 9:40am    
Shim objects most likely.

It is a good example of why coding against interfaces is an important technique.
Sergey Alexandrovich Kryukov 9-Nov-15 13:03pm    
I would be careful with such expressions like "hostile question". It would be much more reasonable to assume that all comments are written in attempt to help you. After all, you write some unreasonable question, you are responsible for it, not those who criticize it. After all, such criticism is done in attempt to help you: if you take it into account, you can ask better questions and ultimately get more help. It would be strange to accuse in hostility people who help many members, spending a lot of their time for such help, without any payment or other reward.

But I don't say your question is completely unreasonable, I would just say that you did not provide enough material for thinking at best solutions. Therefore, you can only hope for some very general advice, and not very technical. I tried to explain certain things in Solution 2, please see it.

—SA
Philippe Mori 10-Nov-15 12:32pm    
The first question to ask yourself is weither a class hierarchy is the right solution and if all functionnality have to be handled though that hierarchy.

Depending on the problem, you might need another class hierarchy to handle some particular aspects...

Since we don't have enough informations on the actual issues, we cannot suggest how to refactor your code...

Searching on the web for SOLID principles and design patterns might give you many ideas on how to improve your design.

Well your best option is stick to principles like SOLID.
And also use design patterns:
http://www.dofactory.com/net/design-patterns[^]

In you case I would suggest patterns like Builder, Factory Method, Decorator, Template Method or Visitor.

Just try to make a quick look on them and think about how this solution could help you.

If you struggling with inheritance/polymorphism you can for start read this few articles:

Diving in OOP (Day 1) : Polymorphism and Inheritance (Early Binding/Compile Time Polymorphism) [^]

Also if you post some examples of you work we can help you with some concrete examples.
 
Share this answer
 
Sorry that I'm not giving you any definitive advice. Without knowing all essential detail of your project, it would be simply impossible. Only a very general advice could be given. I think you do not expect such advice yourself. If feels like you just need some reassurance.

I want to say "it's too late", but it would be misleading and could be taken wrong. Of course, it's never too late to fix your project. It looks like you wish to change your project development radically while not doing too much work and, apparently, not making it worse. And this is something which might be too late.

First of all, please see my post: http://www.codeproject.com/Members/SAKryukov?msg=5157460#xx5157460xx[^].

Can you see where I'm coming from? If your architecture was good in that sense, you probably would not ask this question at all, so probably it is not, and then it means that you have to go hard way. Don't get me wrong: also, there could be some change that the easy way exists, but then again, it would be impossible to see it without knowing a lot more detail of your project. The detail you provided are not definitive. Usually, extension of the class is not a problem and rarely requires any redesign, but moving a class along the hierarchy could be a problem, unless your architecture is extremely flexible. But the fact that you have some problems even with such a simple thing as extension is a bad sign: it tells us that most likely you have to do a major redesign. And this is the reassurance I mentioned above: this is what it is: if you feel you need major redesign, do major redesign, don't be afraid of it.

I would tell you more: in by observation, really good projects are developed at least twice, with second attempt performed nearly from scratch. This is pretty natural; first you create a really working prototype and don't like it much. But then you clearly see all the mistakes you have made, so you gladly do the same project from scratch. And only then you create a really good thing.

Look, I see many people who would protest this idea; they say this is wasteful. Too bad that there are too many of such people, who already caused enormous waste of time by denying the need for the time for such prototype. They pretend they saved a lot of time, but in fact they create projects which never reach maturity, and waste a lot more time on all those multiple version with endless "improvements", often never coming to an acceptably good product.

—SA
 
Share this answer
 
Comments
hrvoje59 10-Nov-15 4:11am    
Thank you for answering. I am aware that I did not ask technical questions so this turned out to be more philosophical conversation. I have read few articles regarding good practices in OOP. For sure I took the wrong turn few times and now I am fixing it. I'll try to ask more technical question once I find out more specifically where the problem is.
Sergey Alexandrovich Kryukov 10-Nov-15 7:49am    
You are very welcome.
I just think you need to understand that those "wrong turns" is a natural thing; and re-engineering needs to be the usual practice. It's better to foresee this need than pretend that everything will come right at once.
With you the best of luck.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900