apply template method pattern in report implementation

17
Apply Template Method Pattern in Report Implementation Albert Guo [email protected]

Upload: guo-albert

Post on 19-May-2015

1.194 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Apply Template Method Pattern in Report Implementation

Apply Template Method Pattern in Report ImplementationAlbert Guo

[email protected]

Page 2: Apply Template Method Pattern in Report Implementation

2

Agenda Introduction Template method: UML class diagram Usage Example Benefits

Page 3: Apply Template Method Pattern in Report Implementation

3

Introduction A template method defines the program skeleton of an

algorithm. One or more of the algorithm steps can be overridden by subclasses to allow differing behaviors while ensuring that the overarching algorithm is still followed.

In object-oriented programming, first a class is created that provides the basic steps of an algorithm design. These steps are implemented using abstract methods. Later on, subclasses change the abstract methods to implement real actions. Thus the general algorithm is saved in one place but the concrete steps may be changed by the subclasses.

Page 4: Apply Template Method Pattern in Report Implementation

4

Introduction – cont. Intent

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.

Motivation Sometimes you want to specify the order of

operations that a method uses, but allow subclasses to provide their own implementations of some of these operations

Page 5: Apply Template Method Pattern in Report Implementation

5

Participants

CollaborationsConcreteClass relies on AbstractClass to implement the invariant steps of the algorithm.

Page 6: Apply Template Method Pattern in Report Implementation

6

Participants – cont.

Abstract class Defines abstract primitive operations that concrete subclasses

define to implement steps of an algorithm. Implements a template method defining the skeleton of an

algorithm. The template method calls primitive operations as well as operations defined in AbstractClass or those of other objects.

Concrete class implements the primitive operations to carry out subclass-

specific steps of the algorithm.

Page 7: Apply Template Method Pattern in Report Implementation

7

Usage Operations which must be overridden by a subclass should

be made abstract If the template method itself should not be overridden by a

subclass, it should be made final In a template method, the parent class calls the operations of

a subclass and not the other way around. This is an inverted control structure that’s sometimes referred to as "the Hollywood principle," as in, "Don't call us, we'll call you".

Page 8: Apply Template Method Pattern in Report Implementation

8

Example

Page 9: Apply Template Method Pattern in Report Implementation

9

Use Case Diagram

Page 10: Apply Template Method Pattern in Report Implementation

10

Class Diagram

Abstract primitive operations

Template method

implement primitive operations

Page 11: Apply Template Method Pattern in Report Implementation

11

Template Method Content

Get data source

Assign Jasper

Template File

Generate JasperPrint

Setup Export Format

Export Report

Page 12: Apply Template Method Pattern in Report Implementation

12

Abstract primitive operations

Template method

Abstract Class

Page 13: Apply Template Method Pattern in Report Implementation

13

extends abstract class

Implement operation in each method

Concrete Class

Page 14: Apply Template Method Pattern in Report Implementation

14Implement operation in each methodConcrete Class

Page 15: Apply Template Method Pattern in Report Implementation

15

Sequence Diagram

Page 16: Apply Template Method Pattern in Report Implementation

16

NIG135Controller

it will be executed in sequence:1. prepareDataSource2. getReportTemplateFile3. generateJasperPrint4. setupExportFormat5. exportReport

Page 17: Apply Template Method Pattern in Report Implementation

17

Benefits To make many similar operations template. From many specialized operations to a generalized

operation. Refactor common behavior to simplify code. Algorithm related improvement.