Segíthet referenciák hozzáadásával vagy a közzé nem tett tartalom eltávolításával. További részletekért lásd a beszélgetés oldalt .
A szoftverfejlesztésben a dekoratőr az egyik tervezési mintaszerkezet neve .
A dekoratőr lehetővé teszi az objektumok új felelősségének dinamikus hozzárendelését . A dekorátorok elég rugalmas alternatívát kínálnak az örökléshez , hogy új funkciókat alkossanak.
A Decorator minta a huszonhárom GOF minta egyike . Megoldja a következő problémákat:
Az öröklés használatakor a különböző alosztályok különböző módon bővítik a szülői osztályt. De a fordítás idején egy kiterjesztés van az osztályhoz csatolva, és futás közben nem változhat.
Itt az öröklést használják.
//______________________________________________________________________ // Déclarations abstract class Voiture { public abstract double Prix { get; } } class AstonMartin : Voiture { public override double Prix { get { return 999.99; } } } //______________________________________________________________________ // Décorateurs class Option : Voiture { protected Voiture _originale; protected double _tarifOption; public Option(Voiture originale, double tarif) { _originale = originale; _tarifOption = tarif; } public override double Prix { get { return _originale.Prix + _tarifOption; } } } class VoitureAvecClimatisation : Option { public VoitureAvecClimatisation (Voiture originale) : base(originale, 1.0) { } } class VoitureAvecParachute : Option { public VoitureAvecParachute (Voiture originale) : base(originale, 10.0) { } } class VoitureAmphibie : Option { public VoitureAmphibie (Voiture originale) : base(originale, 100.0) { } } //______________________________________________________________________ // Implémentation class Program { static void Main() { Voiture astonMartin= new AstonMartin(); astonMartin = new VoitureAvecClimatisation(astonMartin); astonMartin = new VoitureAvecParachute(astonMartin); astonMartin = new VoitureAmphibie(astonMartin); Console.WriteLine(astonMartin.Prix); // affiche 1110.99 } }Valóban, a Delphi és a Free Pascal nyelvek támogatják az osztálysegítőket, amelyek szükségtelenné teszik a dekoratőr mintázatát .
program NoMoreDecorators; type TMyObject = class procedure WriteHello; end; TMyObjectHelper = class helper for TMyObject procedure WriteHello(const Name: string); overload; end; procedure TMyObject.WriteHello; begin writeln('Hello'); end; procedure TMyObjectHelper.WriteHello(const Name: string); begin writeln('Hello, ', Name, '!'); end; var o: TMyObject; begin o := TMyObject.Create; o.WriteHello; o.WriteHello('Jean'); o.Free; end.forrás: Delphi GOF DesignPatterns (CodePlex)
Példa a Delphiben unit decorator; interface type IComponent = interface ['{8021ECE2-0D60-4C96-99AA-C5A6C515DF52}'] function Operation(): String; End; TComponent = class (TInterfacedObject, IComponent) public function Operation(): String; end; TDecoratorA = class (TInterfacedObject, IComponent) private FComponent: IComponent; public function Operation(): String; constructor Create(c: IComponent); end; TDecoratorB = class (TInterfacedObject, IComponent) private FComponent: IComponent; public addedState: String; function Operation(): String; function AddedBehaviour(): String; constructor Create(c: IComponent); end; TClient = class class procedure Display(s: String; c: IComponent); end; implementation { TComponent } function TComponent.Operation: String; begin Result := 'I am walking '; end; { TDecoratorA } constructor TDecoratorA.Create(c: IComponent); begin inherited Create; Self.FComponent := c; end; function TDecoratorA.Operation: String; var s: String; begin s := Self.FComponent.Operation; s := s + 'and listening to Classic FM '; Result := s; end; { TDecoratorB } function TDecoratorB.AddedBehaviour: String; begin Result := 'and I bouth a capuccino '; end; constructor TDecoratorB.Create(c: IComponent); begin inherited Create; Self.FComponent := c; Self.addedState := 'past the coffe shop '; end; function TDecoratorB.Operation: String; var s: String; begin s := Self.FComponent.Operation; s := s + 'to school '; Result := s; end; { TClient } class procedure TClient.Display(s: String; c: IComponent); begin WriteLn(s + c.Operation); end; end. { projet } program Structural.Decorator.Pattern; {$APPTYPE CONSOLE} uses SysUtils, decorator in 'decorator.pas'; var component, decoratorA, decoratorB: IComponent; decB: TDecoratorB; begin try WriteLn('Decorator Pattern' + #10); component := TComponent.Create; try decoratorA := TDecoratorA.Create(component); decoratorB := TDecoratorB.Create(component); TClient.Display('1. Basic component : ', component); TClient.Display('2. A-Decorated : ', decoratorA); TClient.Display('3. B-Decorated : ', decoratorB); decoratorB := TDecoratorB.Create(decoratorA); TClient.Display('4. B-A-Decorated : ', decoratorB); decB := TDecoratorB.Create(component); decoratorA := TDecoratorA.Create(decB); TClient.Display('5. A-B-Decorated : ', decoratorA); WriteLn(decB.addedState + decB.AddedBehaviour); finally component.Free; end; ReadLn; except on E:Exception do Writeln(E.Classname, ': ', E.Message); end; end.