Suppose I have a class uicontrolWrapper , which is a wrapper for uicontrol (but does not subclass it). uicontrol material is privately owned by uicontrolWrapper . Basically, I want to be able to do set/get against the wrapper, and calls will go into uicontrol .
I could do this:
classdef uicontrolWrapper < handle properties (Access = private) uic end properties (Dependent) Style String Value ... end methods function set.Style(obj, val) obj.uic.Style = val; end function val = get.Style(obj) val = obj.uic.Style; end ... end
but hardcoding like this is obviously pretty ugly.
Or I could dynamically generate properties depending on what I'm trying to wrap :
classdef uicontrolWrapper < dynamicprops properties (Access = private) uic end methods function obj = uicontrolWrapper(hObj) obj.uic = hObj; cellfun(@(prop) obj.createProperty(prop, fields(get(hObj)); end function createProperty(obj, prop) p = addprop(obj, prop); p.Dependent = true; p.SetMethod = @setUicontrolProp; p.GetMethod = @getUicontrolProp; function setUicontrolProp(obj, val) obj.uic.(prop) = value; end function val = getUicontrolProp(obj) val = obj.uic.(prop); end end end end
The thing is not to violate the Law of Demeter , without "going into" the property that we are trying to configure.
I don't know if this is a design pattern, but I used this type of thing to wrap objects of different types when subclasses are for some reason or not acceptable. (For example, the class is matlab.ui.control.UIControl Sealed and cannot be subclassed.) Does this have an actual name and an assumed typical use?
source share