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

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

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



Pattern: Template Method
Definition
"Define the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure."


This pattern is essentially an extension of abstract methods to more complex algorithms.

Applications in Delphi
Abstraction is implemented in Delphi by abstract virtual methods. Abstract methods differ from virtual methods by the base class not providing any implementation. The descendant class is completely responsible for implementing an abstract method. Calling an abstract method that has not been overridden will result in a runtime error.


A typical example of abstraction is the TGraphic class.

TGraphic is an abstract class used to implement TBitmap, TIcon and TMetafile. Other developers have frequently used TGraphic as the basis for other graphics objects such as PCX, GIF, JPG representations. TGraphic defines abstract methods such as Draw, LoadFromFile and SaveToFile which are then overridden in the concrete classes. Other objects that use TGraphic, such as a TCanvas only know about the abstract Draw method, yet are used with the concrete class at runtime.


Many classes that use complex algorithms are likely to benefit from abstraction using the template method approach. Typical examples include data compression, encryption and advanced graphics processing.

Implementation Example
To implement template methods you need an abstract class and concrete classes for each alternate implementation. Define a public interface to an algorithm in an abstract base class. In that public method, implement the steps of the algorithm in calls to protected abstract methods of the class. In concrete classes derived from the base class, override each step of the algorithm with a concrete implementation specific to that class.


This example shows some very simple alogrithm steps, but illustrates the principle of deferring implementation to a subclass.





unit Tpl_meth;

interface

type
TAbstractTemplateClass = class(TObject)
protected
function Algorithm_StepA: Integer; virtual; abstract;
function Algorithm_StepB: Integer; virtual; abstract;
function Algorithm_StepC: Integer; virtual; abstract;
public
function Algorithm: Integer;
end;

TConcreteClassA = class(TAbstractTemplateClass)
protected
function Algorithm_StepA: Integer; override;
function Algorithm_StepB: Integer; override;
function Algorithm_StepC: Integer; override;
end;

TConcreteClassB = class(TAbstractTemplateClass)
protected
function Algorithm_StepA: Integer; override;
function Algorithm_StepB: Integer; override;
function Algorithm_StepC: Integer; override;
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 Всички права запазени.