Can I "cast" a value class object to a handle class object on the fly

24 次查看(过去 30 天)
Hi
I'm relatively new to OOP. I currently have a value class object. Specifically, it's a custom date class that I'm working on. So, most of the time it makes sense to have the object as a value class.
However, there are occasions where I would want a particular instance of an object to be viewed as a handle class. Specifically in my case this could happen where I have an object that is let's say the current date. Hence if current date changes, all objects linked to that object should update. This obviously sounds like a handle class object.
So the question is, is there a way that I can use my date value class and cast a specific instance of that object as a handle.
Hope I stated that clearly.
Regards, Phillip

采纳的回答

Matt J
Matt J 2014-11-5
编辑:Matt J 2014-11-5
I found a way to do it (see the attached classdef file myclass.m), but it's fancy. Basically, it uses a Dependent property called "theDate" as an interface to let you get and set data. However, the data will be sent to or drawn from a value variable or a handle variable, depending on whether the object's "type" property is set to 'handle' or 'value'.
The usage works like this,
obj1=myclass('May 1');
obj2=cast(obj1,'handle');
obj3=obj2;
This creates a value object obj1. The cast() method then creates an unshared copy obj2 of obj1. However, obj2 will now have handle semantics. So, obj2 and obj3 will be linked with each other, but neither will be linked to obj1.
>> obj3.theDate='Jan 9'; obj1, obj2,obj3
May 1
Jan 9
Jan 9
The accompanying test.m runs a more extensive script of tests.
  1 个评论
Phillip
Phillip 2014-11-5
Now that's the type of trickery I wanted to see.
Thanks for that Matt, great suggestion. In going through all of this I came across this in the docs. Not sure how I missed it.
I'm still going through it and will provide feedback

请先登录,再进行评论。

更多回答(2 个)

Phillip
Phillip 2014-11-5
I'm going to give an alternative answer (I hope) to Matt's as stated in my comment:
Write your value class as normal except add the HandleCompatible property:
classdef (HandleCompatible) myvalueclass
% Code as usual
end
Now you subclass the myvalueclass as follows:
classdef myhandleclass < myvalueclass & handle
% No code
end
This way you inherit everything from your value class but you do not need to maintain two classes.
I guess if you only want to have one class, you can create a superclass that at construction gets defined as value or handle and then just subclass it in the superclass constructor.
I'm new at this so please correct if I'm wrong or someone has deeper insight into this.
Also please accept this answer if it is correct.
  2 个评论
Adam
Adam 2014-11-5
This does look useful and not something I have used before. I tend to default to using a handle class most of the time because I prefer them, but certainly they are not always the appropriate solution to a problem and I have had situations before where I wanted deep copies of handle class objects which can get very messy.

请先登录,再进行评论。


Matt J
Matt J 2014-11-5
编辑:Matt J 2014-11-5
You would use a converter method, I think
I don't think you can avoid writing a second class that is a handle version of the original class, if that's what you're asking.
Are you sure it makes sense to have a value class version in the first place? I didn't follow your explanation of that. What if you made it a handle class to begin with and then had a class method unshare() to make unlinked copies when you need them?
  1 个评论
Phillip
Phillip 2014-11-5
编辑:Phillip 2014-11-5
Hi Matt
Thanks for the response.
Regarding the question about why it's a value class in the first place. Most of the date objects will just be dates specific to objects. Ie the way I will be using them, it would be dangerous to have a handle class since i could inadvertently change a date (or assign a duplicate variable name) without intending to.
Another issue is that you can't see the "proper" size of a handle object. Some of my objects will have a large set of dates.
Final reason: Matlab's new datetime object is a value class. And those okes know what they are doing unlike me :)
As for the converter, on face va;ie, it doesn't seem to support the change of value to handle, leaving me to think that you are right in saying I would need to write a "duplicate" object. But I will look into it further
I was hoping there is some neat trickery to change an object from value to handle.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Class File Organization 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by