Клубове Дир.бг
powered by diri.bg
търси в Клубове diri.bg Разширено търсене

Вход
Име
Парола

Клубове
Dir.bg
Взаимопомощ
Горещи теми
Компютри и Интернет
Контакти
Култура и изкуство
Мнения
Наука
Политика, Свят
Спорт
Техника
Градове
Религия и мистика
Фен клубове
Хоби, Развлечения
Общества
Я, архивите са живи
Клубове Дирене Регистрация Кой е тук Въпроси Списък Купувам / Продавам 15:07 01.06.24 
Клубове/ Компютри и Интернет / Delphi Всички теми Следваща тема Пълен преглед*
Информация за клуба
Тема Memento [re: PhantomAS]
Автор PhantomASМодератор (стар ерген®)
Публикувано22.01.03 23:24  



A Memento is an object that stores a snapshot of the
internal state of another object - the memento's originator,
according to Gamma, Helm, Johnson and Vlissides
("Design PATTERNS", Addision-Wesley, 1995)

In the following example the originator class is a class that
does some calculations named "tCalculator". The recall-class
must be a friend class of the originator class in order to
access the private variables that characterize the current state.
In delphi you can realize this if the two classes are defined
in the same unit.

A concrete implementation of the Memento design pattern looks
like this :

tCalculator = class
private
ValueA,ValueB,Interval : extended;
Strings : TStringList;
public
...
end;

tCalculatorRecall = class
private
RefObject : tCalculator;
ValueA,ValueB,Interval : extended;
Strings : TStringList;
public
constructor Create(Calculator : tCalculator);
destructor Destroy; override;
end;

constructor tCalculatorRecall.Create(Calculator: tCalculator);
begin
inherited Create;
RefObject := Calculator;
ValueA := RefObject.ValueA;
ValueB := RefObject.ValueB;
Interval := RefObject.Interval;

Strings := TStringList.Create;
Strings.Assign(RefObject.Strings);
end;

destructor tCalculatorRecall.Destroy;
begin
RefObject.ValueA := ValueA;
RefObject.ValueB := ValueB;
RefObject.Interval := Interval;

RefObject.Strings.Assign(Strings);
Strings.Free;
inherited Destroy;
end;


The following lines demonstrate how to use this class :

// Store state of object
CalculatorRecall:=tCalculatorRecall.Create(Calculator);

// Change the state of object to do some calculations
Calculator.ValueA := ...
Calculator.DoSomething;

// Restore the original state
CalculatorRecall.Destroy;



Examples from the VCL for the Memento Design Pattern
are tFontRecall, tPenRecall and tBrushRecall, three
new available classes in Delphi 6 that are derived from TRecall.
Of course you can also define your own classes in this way.
If you do this, consider two important points :

* Derive the originator class from TPersistent
* Implement the Assign procedure

Then our example looks like this :

TCalculator = class(TPersistent)
private
ValueA,ValueB,Interval : extended;
Strings : TStringList;
...
public
...
procedure Assign(Source: TPersistent); override;
...
end;

TCalculatorRecall = class(TRecall)
public
constructor Create(ACalculator : TCalculator);
end;

procedure tCalculator.Assign(Source: TPersistent);
var RefObject : tCalculator;
begin
if Source is tCalculator then
begin
RefObject := Source as tCalculator;
ValueA := RefObject.ValueA;
ValueB := RefObject.ValueB;
Interval := RefObject.Interval;
...
Strings.Assign(RefObject.Strings);
...
end else
inherited Assign(Source);
end;

constructor TCalculatorRecall.Create(ACalculator: TCalculator);
begin
inherited Create(TCalculator.Create, ACalculator);
end;

---
Е т'ва е живот!


Цялата тема
ТемаАвторПубликувано
* Design Patterns - продължение... PhantomASМодератор   22.01.03 21:53
. * Singleton PhantomAS   22.01.03 21:57
. * Adapter PhantomAS   22.01.03 23:01
. * Template Method PhantomAS   22.01.03 23:02
. * Builder PhantomAS   22.01.03 23:04
. * Abstract Factory PhantomAS   22.01.03 23:05
. * Factory Method PhantomAS   22.01.03 23:07
. * Iterators PhantomAS   22.01.03 23:13
. * Singleton - пример 2 PhantomAS   22.01.03 23:16
. * Mediator PhantomAS   22.01.03 23:20
. * Observer - Simple Observer PhantomAS   22.01.03 23:21
. * Observer PhantomAS   22.01.03 23:22
. * Memento PhantomAS   22.01.03 23:24
. * State PhantomAS   22.01.03 23:26
. * Choice PhantomAS   22.01.03 23:27
. * Delegate actions and operations PhantomAS   22.01.03 23:29
Клуб :  


Clubs.dir.bg е форум за дискусии. Dir.bg не носи отговорност за съдържанието и достоверността на публикуваните в дискусиите материали.

Никаква част от съдържанието на тази страница не може да бъде репродуцирана, записвана или предавана под каквато и да е форма или по какъвто и да е повод без писменото съгласие на Dir.bg
За Забележки, коментари и предложения ползвайте формата за Обратна връзка | Мобилна версия | Потребителско споразумение
© 2006-2024 Dir.bg Всички права запазени.