根据所给的例子,本人用matlab语言写了装饰器模式
shape.m
classdef shape < handle
methods(abstract)
draw(obj);
end
end
circle.m
classdef circle < shape
methods
function draw(~)
disp('shape: circle');
end
end
end
rectangle.m
classdef rectangle < shape
methods
function draw(~)
disp('shape: rectangle')
end
end
end
shapedecorator.m
classdef shapedecorator < shape
properties
shape
end
methods
function obj = shapedecorator(shape)
obj.shape=shape;
end
function draw(obj)
obj.shape.draw();
end
end
end
redshapedecorator.m
classdef redshapedecorator < shapedecorator
methods
function obj = redshapedecorator(shape)
obj = obj@shapedecorator(shape);
end
function draw(obj)
draw@shapedecorator(obj);
disp('border color:red');
end
end
end
测试代码:
circle = circle();
redcircle =redshapedecorator(circle());
redrectangle = redshapedecorator(rectangle());
disp('circle with normal border');
circle.draw();
disp('circle of red border');
redcircle.draw();
disp('rectangle of red border');
redrectangle.draw();
如对本文有疑问, 点击进行留言回复!!
【面试题】研究过tomcat的NioEndpoint源码吗?请阐述下Reactor多线程模型在tomcat中的实现。
荐 厉害了!阿里P8架构师用4大技术文档带你深入解读爆火的中台战略
网友评论