"findobj" for user defined class?

2 ビュー (過去 30 日間)
Song
Song 2015 年 7 月 23 日
コメント済み: Song 2015 年 7 月 28 日
Does anyone has an idea whether is there a way to fetch all instances of certain class from a up-level instance of different classes?
For example, instance A1 of Class A and instance B1 of Class B both contain instances of Class C. Is there a way to call something like "findobj" to get all instances of Class C from A1 and B1?
Thanks.
  1 件のコメント
per isakson
per isakson 2015 年 7 月 25 日
AFAIK: No, there isn't!

サインインしてコメントする。

採用された回答

Muthu Annamalai
Muthu Annamalai 2015 年 7 月 27 日
While @Steve Lord has answered the question "why you shouldn't do this," I'll go ahead and try to answer how you can do this, and possibly shoot yourself in the foot - due to inconsistent state.
To do this, you want to use a factory design pattern and keep a log of all instantiated objects in a singleton.
  1 件のコメント
Song
Song 2015 年 7 月 28 日
actually, I came to this workaround by myself too :)

サインインしてコメントする。

その他の回答 (1 件)

Steven Lord
Steven Lord 2015 年 7 月 27 日
No, not unless the classes themselves define such a way (via a method or a property.)
Suppose instance A1 of class A has an instance of class C stored as a private property, intended to be accessed only by methods of class A and not by users. Granting users unrestricted access to that contained object of class C could render A1 no longer a valid instance of class A, if class A further restricts the values properties of class C can take.
For instance, consider class A representing an Outlook meeting invitation. It has a property representing whether a meeting requires videoconferencing (abbreviated from now on as VC.) It also has a property of class C representing the room in which the meeting is to be held, and the rooms have a property indicating whether the room is VC capable. Meetings that require VC can only be held in rooms that are VC capable. If you can change the VC property of the room stored in the meeting invitation class without going through or informing the invitation object, you could end up with a meeting that requires VC but is being held in a room that is not VC capable.

カテゴリ

Help Center および File ExchangeGraphics Object Programming についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by