フィルターのクリア

Define Method Implementation During Construction

2 ビュー (過去 30 日間)
Rodrigo Fischer
Rodrigo Fischer 2022 年 6 月 3 日
回答済み: Abhijeet 2022 年 6 月 19 日
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 Center および File ExchangeWhos についてさらに検索

製品


リリース

R2021b

Community Treasure Hunt

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

Start Hunting!

Translated by