Define Method Implementation During Construction

3 次查看(过去 30 天)
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?

回答(1 个)

Abhijeet
Abhijeet 2022-6-19
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 :-

类别

Help CenterFile Exchange 中查找有关 Whos 的更多信息

产品


版本

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by