Is there a name for this design pattern (dynamic packaging around another class)?

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?

+6
source share
2 answers

This Decorator Pattern is the creation of a tool that decorates new functions on existing objects (possibly at different times) without affecting other instances of the same type (or, if necessary, explicitly affect them).

This differs from the proxy template in the sense that you do not postpone the task of sending the correct proxy operation, you are actually planting new functions on the object.

+3
source

I suppose you are asking about Proxy here (providing an interface for other objects by creating a wrapper class as a proxy).

A couple of its advantages add security access and simplifies the API for the wrapped object (a proxy can provide a simple API so that client code does not deal with the complexity of the object of interest).

+1
source

Source: https://habr.com/ru/post/982892/


All Articles