本文主要介紹裝飾者模式的缺點(diǎn)(裝飾者模式j(luò)s),下面一起看看裝飾者模式的缺點(diǎn)(裝飾者模式j(luò)s)相關(guān)資訊。
裝飾器模式檢查裝飾器循環(huán)過度設(shè)計(jì)更加復(fù)雜。
公共抽象類shape { public virtual string as string= string。空的;}類circ:形狀{ float riduspublic circel(float rid us){ this . rid us = rid us;}公共覆蓋字符串a(chǎn)s string{ return $ circle riduse cho 0-@ . com { rid us } } public void r:形狀{浮邊;公共廣場(浮動(dòng)邊){ sid: {邊} } }公共抽象類shape decorator : shape { protected internal readonly list type types = new;內(nèi)部形狀形狀;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(循環(huán)。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} );如果(循環(huán)。applicationallowed(類型[0],類型。跳過(1)。tolist)某人。追加($ 具有顏色{ color } );歸還某人。tostring;} }公共類透明:形狀{ shape shape浮動(dòng)百分比;公共透明(形狀形狀,浮動(dòng)百分比age){ this . shape = shape;this.percentage = _ percentage}公共覆蓋字符串a(chǎn)s 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($ 檢測到循環(huán)!類型為錯(cuò)誤,{type。全名} );返回true} public override bool application allowed(type type,ilist type allttype){ return handler(type,allttype);}公開結(jié)束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);控制臺(tái)。writeline(sq。asstring);colored shape color shape = new colored shape(sq, 黑色 );color shape = new colored shape(color shape, 紅色 );控制臺(tái)。writeline(colorshape。asstring);} }
標(biāo)簽:
不允許復(fù)雜性
了解更多裝飾者模式的缺點(diǎn)(裝飾者模式j(luò)s)相關(guān)內(nèi)容請關(guān)注本站點(diǎn)。