2011-07-28 11 views
1

私は、コンストラクタに提供される可能性のある、または他の方法で生成される可能性のあるプロパティを持つクラスを実装しようとしています。私は、データをディスクに保存したり、ロード時に生成したりしたくありません。私はこれまで持っていることは次のとおりです。一時的なプロパティのためにsaveメソッドを呼び出すMatlab OOPを呼び出します。

classdef MyClass 
     properties(GetAccess = public, SetAccess = private) 
      Property1 
      Property2 
      Property3 
     end 
     properties(Access = private) 
      Property4 
     end 
     properties(Transient = true) 
      ProblemProperty 
     end 
     properties(Dependent = true, Transient = true) 
      Property5 
     end 

     methods 
      function MyClass 
       % Constructor. 
      end 

      function val = get.Property5(B) 
       val = SomeFunction(Property1); 
      end 

      function val = get.ProblemProperty(B) 
       if isempty(B.ProblemProperty) 
        B = GenerateProblemProperty(B); 
       end 
       val = B.ProblemProperty; 
      end 

      function B = GenerateProblemProperty(B) 
       B.ProblemProperty = AnotherFunction(B.Property2); 
      end 
     end 
    end 

問題は、私はディスクにオブジェクトを保存しようとすると、MATLABは(ちょうど声明を救う上でプロファイラを実行することによって確認した)get.ProblemPropertyメソッドを呼び出すことです。 ProblemPropertyフィールドは空で、そのままにしておきます。 get.Property5メソッドは呼び出されません。

get.ProblemPropertyの呼び出しを避けるにはどうすればよいですか?

答えて

1

プロパティを設定することができます(コンストラクタ内で)ため、このプロパティは厳密には依存しません。 1つの解決策は、コンストラクタ内のプライベートプロパティ(下の例ではCustomProblemProperty)に設定可能な値を格納することです。 getメソッドがProblemPropertyの場合、このプライベートプロパティ値が空でない場合はそれを返し、それ以外の場合は生成された値を返します。

classdef MyClass 
    properties(GetAccess = public, SetAccess = private) 
     Property1 
     Property2 
     Property3 
    end 
    properties(Access = private) 
     Property4 
     CustomProblemProperty 
    end 
    properties(Dependent = true, Transient = true) 
     ProblemProperty 
     Property5 
    end 

    methods 
     function B = MyClass(varargin) 
      if nargin == 1 
       B.CustomProblemProperty = varargin{1}; 
      end 
     end 

     function val = get.Property5(B) 
      val = SomeFunction(Property1); 
     end 

     function val = get.ProblemProperty(B) 
      if isempty(B.CustomProblemProperty) 
       val = AnotherFunction(B.Property2); 
      else 
       val = B.CustomProblemProperty; 
      end 
     end 

    end 
end 
+0

私はこれについて考えましたが、値が実際にはコンストラクタで直接提供されることがあります。コンストラクタ内の依存プロパティの値を設定できますか? – MatlabSorter

+1

@MatlabSorter - コンストラクタに渡されるオプションのカスタム値を許可するように私の答えを更新しました。要点は、このオプションの値を保存するためにプライベートプロパティを使用し、このプライベートプロパティを取得するか新しい値を生成するかを決定するget.ProblemPropertyです。 –

+0

@ b3ありがとう、私はそれがすべきだと思います。私はMatlabが私の実装でなぜ呼び出すのか理解したいので、もう少し長く質問を残しておきます。 – MatlabSorter

1

あなたのソリューションは機能していますが、OOPの精神ではなく、オブジェクトの外側の形であるアクセサーを内側に混ぜています。

は、私は次の

classdef simpleClass 
    properties(Access = private) 
     % The concrete container for implementation 
     myNiceProperty % m_NiceProperty is another standard name for this. 
    end 
    properties(Dependent) 
     % The accessor which is part of the object "interface" 
     NiceProperty 
    end 

    method 
     % The usual accessors (you can do whatever you wish there) 
     function value = get.NiceProperty(this) 
      value = this.myNiceProperty; 
     end 
     function set.NiceProperty(this, value) 
      this.myNiceProperty = value; 
     end 
    end 
end 

NicePropertyが、その後どこにも保存されることはありません、あなたは標準のコードを書くの利点を持って助言します。

関連する問題