Template Method Pattern implemented in PL/SQL


Design Pattern are great! Not just in Java or C#

The Template Method Pattern can also be implementet with Oracle’s PL/SQL language (using clean Object Types).

The Template Method Design Patterns is probalby one of the most widely used and useful design pattern. common example of this is when writing a program that will handle data using various detail algorithms. The abstract Class would have a method called the “Template” Method which contains the main program logic. There also exists “helper method”s in the class, which are specified by any class that inherits from it.
So the abstract class defines the main logic. The subclasses itself implement the details logic. So every developer can implement his specific details without changing the main logic.

English Deutsch/German

In PL/SQL this design pattern can be implementet the procedural way as been done by Lucas Jellema (Amis.nl)
Here I created a PL/SQL sample with Object Types:

drop type adress_o;
drop type adress_abstract_o;
--------------------------------------------------------------------------------
--
--  adress_abstract_o    [abstract]
--
--------------------------------------------------------------------------------
create or replace type adress_abstract_o as object (
PersId number
, member procedure setPersId(pPersId in number)
, member function getPersId return number
, member function getMainResult return number
, member function doSomethingA return number
, member function doSomethingB return number
) not final not instantiable
;

create or replace type body adress_abstract_o as
--
-- Setters / Getters
--
member procedure setPersId(pPersId in number) is
begin
self.PersId := pPersId;
end;

member function getPersId return number is
begin
return self.PersId;
end;

--
-- Main Template Method!
-- This is the main package operation! It contains the main program logic!
-- It has to be implemented HERE!
-- It must not be implementet in the subclasses!
--
member function getMainResult return number
is
begin
return doSomethingA + doSomethingB;
end;

--
-- private method supporting the template method
-- can be overridden by subclass methods
-- if someond likes a different kind of the doSomethingA calculation, he can
-- subclass and override this method (but not the important getMainResult method!)
--
member function doSomethingA return number
is
begin
return 0;
end;

--
-- private method supporting the template method
-- can be overridden by subclass methods
-- if someond likes a different kind of the doSomethingA calculation, he can
-- subclass and override this method (but not the important getMainResult method!)
--
member function doSomethingB return number
is
begin
return 0;
end;

end;

--------------------------------------------------------------------------------
--
--  adress_o    [instantiable]
--
-- extends adress_abstract_o and overrides some methods
--------------------------------------------------------------------------------
create or replace type adress_o under adress_abstract_o (
OVERRIDING member function doSomethingA return number
, OVERRIDING member function doSomethingB return number
) final instantiable
;

create or replace type body adress_o as
--
-- private methods (overriding the adress_abstract_o class!)
--
OVERRIDING member function doSomethingA return number
is
begin
return 1;
end;

--
-- private methods (overriding the adress_abstract_o class!)
--
OVERRIDING member function doSomethingB return number
is
begin
return 2;
end;

end;

--------------------------------------------------------------------------------
--
--  Testcase on adress_o
--
--------------------------------------------------------------------------------
declare
a adress_o;
p number(12);
begin
a := adress_o(41848);
dbms_output.put_line('PersId= '    ||a.getPersId);
dbms_output.put_line('adresse= '   ||a.getMainResult);
end;

1 thought on “Template Method Pattern implemented in PL/SQL”

  1. Pingback: Decorator Design Pattern in Oracle PL/SQL: Using Object Type with Constructor » SERPland

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top