• Forum
  • Lounge
  • Which design method should I use for thi

 
Which design method should I use for this particular case?

I am going to develop a new class. But I do not have the full list of functionalities to be included in this class right now. But I would like to add new features at different times in the future. i.e I want to add new features to an existing class from time to time in the future as the new requirement arise. How should I start and which design pattern fits my requirement.
I do not have prior experience in using or applying patterns. I am starting out. If this is not the right place please guide me which codeguru forum I should resort to, to have more knowledge on applying design patterns.

Thanks,
ManojE
Without more information on what your class does, your guess is as good as mine.
Google around your class and see how others have designed it.

The point of OOP is that you can derive future classes from a base class and modify its behavior.
If the changes you make only add functionality, you can modify the base class directly.

Hope this helps.
The question suggests you are doing something wrong. One class = one concept, one term, one something. You cannot add "functionalities" to the class. It is not proper OOP to have a class that "does" or "represents" more than a single thing. It should include just methods and fields required to do that single thing well and nothing more. However, you may have several classes that represent several similar, but somehow different concepts. Then what is similar can be grouped into a common abstract base class or common interface (= pure virtual class). Adding new things to the system is done by adding more concrete classes then.

@Duoas: I'm not sure if I understand correctly, but overriding methods to change behaviour is very bad OOP (= generally if someone overrides a non pure-virtual method, there is something smelly with the design there). It is much better to use composition instead of inheritance in such cases.
Last edited on
Your thinking is too restricted. In the real world, new capabilities occur, new hardware is added, new methodologies apply. As long as the API is not modified (and part of an API is the functional component -- what it does) then it is entirely appropriate to modify existing methods, and it is always appropriate to add new methods. In either case, you are modifying the behaviors of the class itself -- and this is the whole point of OO.
Adding new features has nothing to do with good or bad OOP in my opininon... in a real application, there is no limit as to how many behaviours may be required from your class.

For the post in question, I dont think adding new features can be eased by using a specific design pattern. However, if you do need to implement the new functionalities using new derived classes, I would suggest to put in a kind of factory to ease away the creation/deletion of your basically similar functionalities.
Hi All (Duoas, Rapidcoder and Mgupta),

Thank you so much for responding to my questions (and this is the only forum that responded to my question (whether my question is sensible or non-sensible). Big thanks. All your answers were informative, as far as I am concerned.

Let me describe with an example about my question:

Suppose we have a class to do simple arithmetic calculations - Add,Subtract,Multiply and Divide.

I write a class that include the Add and Subtract functionalities. No idea at present that Multiply and Divide is there to be implementd.

I write an applicaton using this class. Later the client says that they want to include Multiply and Divide functionalities. I can derive a new class from the existing class to incorporate these new functionalites. Right?


There may be cases like these where more functionalities that is currently not known , but belongs to the same group, need to be added in future. (Say the client wants to have Exponentiation, Differentiation). This may goes on. We cannot foresee all requirements that belong to the same group at the first time itself.

My question is, Is inheritance the right approach I should follow, or is there a better approach (like any design pattern) for cases like these?


If inheritance is used, the level of inheritance may become deeper ( I have heard that it is not a good practise to use inheritance at a level greater than two or three, as it may make maintenance difficult).

Thanks,
ManojE
@Duoas, I misunderstood you meant modifying methods by overriding them in subclasses.
Of course, changes to the code are unavoidable. But this is not limited to OOP. The same is true for structural, functional, etc. programming.


Suppose we have a class to do simple arithmetic calculations - Add,Subtract,Multiply and Divide.

I write a class that include the Add and Subtract functionalities. No idea at present that Multiply and Divide is there to be implementd.

I write an applicaton using this class. Later the client says that they want to include Multiply and Divide functionalities. I can derive a new class from the existing class to incorporate these new functionalites. Right?


No. Just add them to that class, not derive a new one.
Anyway, I think that Add, Subtract, Multiply and Divide should be just separate classes, probably deriving from a common base declaring some abstract method like "evaluate". When you want more operations - you just add more classes. No need to modify anything. But don't use inheritance for *modifying* the behaviour. It will bite you sooner than you think.

Last edited on
RapidCoder, thanks for your prompt response and sorry for the delay from my end as I was busy with another work. For me, your suggested idea seems great - ie. using an abstract class and concrete classes for different operations. It gives me a solution to the question I asked. Ducas, MGupta what do you say. Any objection? Shall I mark this as solved? I am asking the opinion of yours as well because, you all have more knowledge than I have and I wish any one else coming across this thread must have the right answer for the question I posed.

Thanks once again RapidCoder.

Thanks,
ManojE.
Like I said at the beginning, the proper response depends entirely upon your requirements (specifically, what kind of class you are implementing). You have been given good advice within the limits of our knowledge about your project.
The question seems to inquire about loose coupling. Maybe try that as a Google keyword.

Perhaps the Visitor pattern, Command pattern, and the Pimpl idiom are of interest.
I agree with rapidcoder to say that Add, Subtract, Multiply and Divide should be separate classes. Otherwise you will have to keep modifying the base class and inheriting it and overloading methods again and again.
But in case the behaviours are only going to be mathematical, you may be better off having classes such as ArithmeticEvaluation, Algebraic Evaluation, DifferentialEvaluation and add future modifications to them.

The client may not know all the requirements right now, but has to have a realistic idea about what the program is going to be used for. Dont let the uncertainity affect your program.
Topic archived. No new replies allowed.