„Díszítő programtervezési minta” változatai közötti eltérés

[ellenőrzött változat][ellenőrzött változat]
Tartalom törölve Tartalom hozzáadva
→‎Motiváció: első fele
27. sor:
==Motiváció==
[[Image:UML2 Decorator Pattern.png|thumb|400px|Windows példa UML Diagramja]]
Példaként vegyünk egy ablakot egy [[ablakozó rendszerben]]. Ahhoz, hogy egy ablak tartalmát [[görgetés|görgetni]] tudjuk, szükség lehet arra, hogy megfelelő vízszintes ill. függőleges [[görgetősáv]]ot adjunk hozzá. Tegyük fel, hogy az ablakokat az ''Window'' osztály példányai reprezentálnak, és tegyük fel, hogy ennek az osztálynak nem létezik görgetősáv hozzáadása funkcionalitása. Készíthetünk alosztályt a ''ScrollingWindow'' néven, amely ellátja ezt a feladatot, vagy készíthetünk egy ''ScrollingWindowDecorator'', amely hozzáadja ezt a funkcionalitást létező ''Window'' objektumokhoz. Ezen a ponton, bármelyik megoldás megfelelő lenne.
As an example, consider a window in a [[windowing system]]. To allow [[scrolling]] of the window's contents, we may wish to add horizontal or vertical [[scrollbar]]s to it, as appropriate. Assume windows are represented by instances of the ''Window'' class, and assume this class has no functionality for adding scrollbars. We could create a subclass ''ScrollingWindow'' that provides them, or we could create a ''ScrollingWindowDecorator'' that adds this functionality to existing ''Window'' objects. At this point, either solution would be fine.
 
NowMost let'stegyük assumefel, wehogy alsoszeretnénk desireolyan theképességet abilityis, toamely addkeretet bordersad tohozzá ouraz windowsablakunkhoz. Again,Megint ouraz originaleredeti ''Window'' classosztálynak hasnincs notámogatása supportehhez. TheA ''ScrollingWindow'' subclassalosztály nowproblémával poseskerül a problemszembe, becausemert itvalójában hasegy effectively created aúj newtípusú kindablakot ofhoz windowlétre. IfHa weszeretnénk wishkeretet tohozzáadni add border support toaz ''allösszes'' windowsablakoz, weúj mustosztályokat createkell subclasseslétrehoznunk ''WindowWithBorder'' and(keretes ablak) és ''ScrollingWindowWithBorder''. Obviously,(görgetősávos thiskeretes problemablak) getsnéven. worseNyilvánvalóan withez everya newprobléma featurerosszabbra tofordul, beminden addedúj tulajdonság hozzáadásakor. ForA thedíszítő decoratormegoldás solution,esetén wecsak simplyegyszerűen createkészítünk aegy newúj ''BorderedWindowDecorator'' (keretes ablak díszítőt) —at runtimefutási időben, wedíszíthetjük can decorate existing windowsa withlétező theablakot ''ScrollingWindowDecorator''-ral or(görgethető theablak díszítővel) vagy ''BorderedWindowDecorator''-ral or(keretes both,ablak asdíszítővel) wevagy mindkettővel, seeigény fitszerint.
 
NoteMegjegyzendő, inhogy theaz previouselőző example,példában that thea "SimpleWindow" &és "Window" classesosztályok implementsmegvalósítják thea "Window" interfaceinterfészt, whoamely definesdefiniálja thea "draw();" methodmetódust and theés "getDescription();" method,metódust. thatEzek areszükségesek requiredebben in thisa scenariofelállásban, inhogy orderdíszíteni totudjuk decorateaz aablak window controlvezérlőt.
 
Another good example of where a decorator can be desired is when there is a need to restrict access to an object's properties or methods according to some set of rules or perhaps several parallel sets of rules (different user credentials, etc.) In this case instead of implementing the access control in the original object it is left unchanged and unaware of any restrictions on its use, and it is wrapped in an access control decorator object, which can then serve only the permitted subset of the original object's interface.