Back Home Up Next

TEMPLATE METHOD

1 Purpose

2 Problem Description

3 Domain Definitions

4 Elisa Components

1  Purpose

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.

2  Problem Description

Consider an application framework that provides Application and Document classes. The Application class is responsible for opening existing documents stored in an external format, such as a file. A Document object represents the information in a document once it's read from the file.

Applications built with the framework can subclass Application and Document to suit specific needs. For example, a drawing application defines DrawApplication and DrawDocument subclasses; a spreadsheet application defines SpreadsheetApplication and SpreadsheetDocument subclasses. 

The abstract Application class defines the algorithm for opening and reading a document in its OpenDocument operation:

    void Application::OpenDocument (const char* name) {
        if (!CanOpenDocument(name)) {
            // cannot handle this document
            return;
        }
    
        Document* doc = DoCreateDocument();
    
        if (doc) {
            _docs->AddDocument(doc);
            AboutToOpenDocument(doc);
            doc->Open();
            doc->DoRead();
        }
    }

OpenDocument defines each step for opening a document. It checks if the document can be opened, creates the application-specific Document object, adds it to its set of documents, and reads the Document from a file.

We call OpenDocument a template method. A template method defines an algorithm in terms of abstract operations that subclasses override to provide concrete behavior. Application subclasses define the steps of the algorithm that check if the document can be opened (CanOpenDocument) and that create the Document (DoCreateDocument). Document classes define the step that reads the document (DoRead). The template method also defines an operation that lets Application subclasses know when the document is about to be opened (AboutToOpenDocument), in case they care.

By defining some of the steps of an algorithm using abstract operations, the template method fixes their ordering, but it lets Application and Document subclasses vary those steps to suit their needs.

3  Domain Definitions

We will transform the top-level relations into a set of corresponding domain definitions:

MyDocument  =  Document +  . . .

MyApplication  =  Application +  . . .

Document  =  . . . +  . . .

Application  =  . . . +  . . .

The next step is to translate the domain definitions into a number of Elisa components. In addition, we will use skeletons of definitions as shown in the preceding domain relationships

4  Elisa components 

Based on the domain definitions a set of related components are derived. The first component implements  a skeleton of the  Document domain.

component Documents;
type Document;

     CreateDocument() -> Document;

     Save(Document)   -> result;

     Open(Document)   -> result;

     Close(Document)  -> result;

     DoRead(Document) -> result;

      . . .
begin

            <<implementations of >>

     CreateDocument( ) = Document:[ ... ]; 

     Save(document) = ...;

     Open(document) = ...;

     Close(document)= ...;

     DoRead(document)= ...;

          . . .

end component Documents;

 

 

The following component is a skeleton example of a MyDocument:

 

component MyDocuments;
type MyDocument;

     CreateMyDocument(Document)-> MyDocument;

     Document(MyDocument)      -> Document;

     DoRead(MyDocument)        -> result;

      . . .
begin

            <<implementations of >>

     CreateMyDocument(document) = MyDocument:[ ..document... ];

     Document(myDocument) = myDocument.document;

     DoRead(myDocument) = ...;

          . . .

end component MyDocuments;

 

 

The following component is a skeleton example of an Application:

 

component Applications;
type Application;

     CreateApplication(Document)  -> Application;

     AddDocument(Application)     -> result;

     OpenDocument(Application)    -> result;

     DoCreateDocument(Application)-> result;

     CanOpenDocument(Application) -> result;

     AboutToOpenDocument(Application) -> result;

                  . . .
begin

            <<implementations of >>

     CreateApplication(document) = Application:[ ..document... ];

     AddDocument(application) = ...;

     OpenDocument(application)= ...;

     DoCreateDocument(application)= ...;

     CanOpenDocument(application) = ...;

     AboutToOpenDocument(application)= ...;

          . . .

end component Applications;

 

 

The following component is a skeleton example of a MyApplication:

 

component MyApplications;
type MyApplication;

     CreateMyApplication(Application)  -> MyApplication;

     Application(MyApplication)        -> Application;

     DoCreateDocument(MyApplication)   -> result;

     CanOpenDocument(MyApplication)    -> result;

     AboutToOpenDocument(MyApplication)-> result;

      . . .
begin

            <<implementations of >>

     CreateMyApplication(application) = MyApplication:[ ..application... ];

     Application(myApplication) = myApplication.application;

     DoCreateDocument(myApplication) = ...;

     CanOpenDocument(myApplication) = ...;

     AboutToOpenDocument(myApplication)= ...;

          . . .

end component MyApplications;

 

 

Back Home Up Next

  Part 5: Design Patterns   Template Method

            
Introduction

Home | Highlights of Elisa | Integrating Different Paradigms | Getting Started with Elisa | Demo's  | What is Domain Orientation | Bibliography | Copyright | News | Contact | Contents

Language Description:

Lexical Elements | Basic Data Types and Expressions | Definitions | Streams | Backtracking | Statements and Special Expressions | Arrays | Lists | Descriptors | Components | Collections | Generic Components | Terms | Categories | Types | Built-in Definitions | Higher-order Definitions | External Interfaces | Index 

Data Structures: Sequences | Examples involving Lists | Trees | Graphs | Searching State Spaces | Language Processing | Knowledge Representations
Domain Modeling:

Domain Modeling | Concepts | Domain Definitions | Domain Operations | Domain Implementations | Systems | Case study: an Order processing system | Case study: an Airport Support system | Domain Orientation versus Object Orientation

Design Patterns:

Introduction | Abstract Factory | Builder | Factory Method | Prototype | Singleton | Adapter | Bridge | Composite | Decorator | Facade | Flyweight | Proxy | Chain of Responsibility | Command | Interpreter | Iterator | Mediator | Memento | Observer | State | Strategy | Template Method | Visitor 

 

click tracking

 

Send me your comments

 

This page was last modified on 04-12-2012 15:01:16   

free hit counter