本文主要介绍装饰者模式的缺点(装饰者模式js),下面一起看看装饰者模式的缺点(装饰者模式js)相关资讯。
装饰器模式检查装饰器循环过度设计更加复杂。
公共抽象类shape { public virtual string as string= string。空的;}类circ:形状{ float riduspublic circel(float rid us){ this . rid us = rid us;}公共覆盖字符串as string{ return $ circle riduse cho 0-@ . com { rid us } } public void r:形状{浮边;公共广场(浮动边){ sid: {边} } }公共抽象类shape decorator : shape { protected internal readonly list type types = new;内部形状形状;public shape decorator(shape shape){ shape = shape;if(shape is shape decorator sd){ types。add range(sd . types);} } }公共抽象类shapedecoratortself,tcycl: shapecodor其中tcycles :shaprdecoratorcyclepolicy,new{ protected readonly tcycles cycles = new tcycl: base(shape){ if(循环。typeadditionallowed(type of(tself),types)) { types。add(type of(tself));} } } public class shapedecotatorwithpolicy t :shapecodiortort,throwoncypolicy { public shapedecotatorwithpolicy(shap: base(shape){ } } public class colored shape//: shaped economic colored shape,throwoncypolicy//不允许相同的: shaped economic colored shape,absorbcyclicity//允许sam: shaped totor带有警用颜色shape { private readonly string color;public coloredshape(shape shape,string color): base(shape){ this。形状=形状;this.color = color} public override string as string{ var sb = new stringbuilder($ {形状。asstring} );如果(循环。applicationallowed(类型[0],类型。跳过(1)。tolist)某人。追加($ 具有颜色{ color } );归还某人。tostring;} }公共类透明:形状{ shape shape浮动百分比;公共透明(形状形状,浮动百分比age){ this . shape = shape;this.percentage = _ percentage}公共覆盖字符串as string{ return $ {形状。asstring} transparent : { percentage * 100.0 } % } }公共抽象类shaprdecoratorcyclepolicy {公共抽象bool type additionallowed(type type,ili sttype allttype);public abstract bool application allowed(type type,ilist type allttype);}公共类cycesallowedpolicy : shaprdecoratorcyclepolicy { public override bool application allowed(type type,ilist type allttype){ return true;} public override bool typeadditionallowed(type type,ilist type allttype){ return true;} } public class throwoncycpolicy : shaprdecoratorcyclepolicy { public bool handler(type type,ilisttype allttype) { if (allttype。contains(type))抛出新的invalidoperationexception($ 检测到循环!类型为错误,{type。全名} );返回true} public override bool application allowed(type type,ilist type allttype){ return handler(type,allttype);}公开结束ride bool typeadditionallowed(type type,ilist type allttype){ return handler(type,allttype);} } public class absorbcyclepolicy : shaprdecoratorcyclepolicy { public override bool application allowed(type type,ilisttype allttype) { return!allttype。包含(类型);} public override bool typeadditionallowed(type type,ilist type allttype){ return true;} }类程序{ static void main(string[]args){ square sq = new square(1.23 f);控制台。writeline(sq。asstring);colored shape color shape = new colored shape(sq, 黑色 );color shape = new colored shape(color shape, 红色 );控制台。writeline(colorshape。asstring);} }
标签:
不允许复杂性
了解更多装饰者模式的缺点(装饰者模式js)相关内容请关注本站点。