Commits

Nick  committed e6ac69f

Adding better cleanup

  • Participants
  • Parent commits 917b8f3

Comments (0)

Files changed (2)

File Patterns/Decorator/DecoratorDesignPattern.dpr

   Beverage4: TBeverage;
 begin
   Beverage := TEspresso.Create;
-  Beverage := TMocha.Create(Beverage);
-  Beverage := TMocha.Create(Beverage);
-  Beverage := TWhip.Create(Beverage);
-  OutputBeverage(Beverage);
+  try
+    Beverage := TMocha.Create(Beverage);
+    Beverage := TMocha.Create(Beverage);
+    Beverage := TWhip.Create(Beverage);
+    OutputBeverage(Beverage);
+  finally
+    Beverage.Free;
+  end;
 
   Beverage2 := TDarkRoast.Create;
-  Beverage2 := TMocha.Create(Beverage2);
-  Beverage2 := TMocha.Create(Beverage2);
-  Beverage2 := TWhip.Create(Beverage2);
-  OutputBeverage(Beverage2);
+  try
+    Beverage2 := TMocha.Create(Beverage2);
+    Beverage2 := TMocha.Create(Beverage2);
+    Beverage2 := TWhip.Create(Beverage2);
+    OutputBeverage(Beverage2);
+  finally
+    Beverage2.Free;
+  end;
 
   // Alternate way to call the same thing as above....
   Beverage3 := TWhip.Create(TMocha.Create(TMocha.Create(TDarkRoast.Create)));
-  OutputBeverage(Beverage3);
+  try
+    OutputBeverage(Beverage3);
+  finally
+    Beverage3.Free;
+  end;
 
   Beverage4 := THouseBlend.Create;
-  Beverage4 := TSoy.Create(Beverage4);
-  Beverage4 := TMocha.Create(Beverage4);
-  Beverage4 := TWhip.Create(Beverage4);
-  OutputBeverage(Beverage4);
+  try
+    Beverage4 := TSoy.Create(Beverage4);
+    Beverage4 := TMocha.Create(Beverage4);
+    Beverage4 := TWhip.Create(Beverage4);
+    OutputBeverage(Beverage4);
+  finally
+    Beverage4.Free;
+  end;
 end;
 
 begin

File Patterns/Decorator/uStarBuzzCoffee.pas

     FBeverage: TBeverage;
   public
     constructor Create(aBeverage: TBeverage);
+    destructor Destroy; override;
   end;
 
   TEspresso = class(TBeverage)
   FBeverage := aBeverage;
 end;
 
+destructor TBeverageDecorator.Destroy;
+begin
+  FBeverage.Free;
+  inherited;
+end;
+
 { TWhip }
 
 function TWhip.Cost: Double;
   Result := FBeverage.GetDescription + ', Whip';
 end;
 
+
+
 end.