Define Method Implementation During Construction

3 views (last 30 days)
Hi,
I'm looking for a way of constructing an object depending on configuration. Basically, I have something like this
classdef MyClass < handle
...
methods
...
function foo = foo()
do something
end
function boo = boo()
do something
end
end
end
The properties of the class are the same, and I expect every instance of this class to have the foo and boo methods. However, I have multiple possibilities for these methods. Say for each I have 3 different implementations, which I can combine, giving a total of 9 behaviours for class MyClass. One example is: foo can either do some computations and output a value, or it can read this value from a table, or it can generate a random number. I also can at any time create a new implementation, like generating a random number but this time with a different distribution, so it would be nice to have each implementation in a different file.
What I want to do is to construct an instance of this class by saying: implement foo like this and boo like this.
Are there any suggestions?

Answers (1)

Abhijeet
Abhijeet on 19 Jun 2022
Hi Rodrigo Fischer ,
I can understand that you want to construct an object with different functionalites based upon diffrent use cases.
In design pattern there is Strategy Pattern which can be used for such scenarios.
Sharing some resources :-

Products


Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!