I have a main class which takes 2 arguments, Two arguments(Same) are coming from 2 another classes which are doing differnet computations . Now I need to define a 3rd argument which allows user to specify which class he wish to chooses .
3 views (last 30 days)
Show older comments
Lest say : Main Class is : obj = mainClass (a,b) other 2 classes which uses a,b are :
1) obj = Sum(a,b) 2) obj = Diff(a,b)
I need to provide a 3rd input argument in the main class where user can decide which one out of the 2 class he wants to use, wether sum class or diff class.
How do I define 3rd argument linking the 2 classes to choose.
0 Comments
Accepted Answer
Jeff Miller
on 11 Jul 2018
I'm guessing main class is a parent class and sum and diff are two child classes descended from it.
If that is right, the best approach is to write a separate function that returns whichever class the user wants, e.g.,
function myClass = ClassSelector(a,b,type)
switch type
case 'sum'
myClass = Sum(a,b);
case 'diff'
myClass = Diff(a,b);
end
end
In good OO design, the parent class should not know anything about the different child classes. For example, you don't want to have to change the parent class if you add a new type of child (e.g, Product(a,b)).
0 Comments
More Answers (0)
See Also
Categories
Find more on Signal Generation and Preprocessing in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!