Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Apply Template Method Pattern in Report Implementation


Published on

  • Be the first to comment

  • Be the first to like this

Apply Template Method Pattern in Report Implementation

  1. 1. Apply Template Method Pattern in Report Implementation<br />Albert Guo<br /><br />
  2. 2. Agenda<br />Introduction<br />Template method: UML class diagram<br />Usage<br />Example<br />Benefits<br />2<br />
  3. 3. Introduction<br />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.<br />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.<br />3<br />
  4. 4. Introduction – cont.<br />Intent<br />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.<br />Motivation<br />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<br />4<br />
  5. 5. Participants<br />5<br />Collaborations<br />ConcreteClass relies on AbstractClass to <br />implement the invariant steps of the algorithm.<br />
  6. 6. Participants – cont.<br />Abstract class<br />Defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.<br />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.<br />Concrete class<br />implements the primitive operations to carry out subclass-specific steps of the algorithm.<br />6<br />
  7. 7. Usage<br />Operations which must be overridden by a subclass should be made abstract<br />If the template method itself should not be overridden by a subclass, it should be made final<br />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".<br />7<br />
  8. 8. Example<br />8<br />
  9. 9. Use Case Diagram<br />9<br />
  10. 10. Class Diagram<br />10<br />Abstract primitive operations<br />Template method<br />implement primitive operations<br />
  11. 11. Template Method Content<br />11<br />
  12. 12. 12<br />Abstract Class<br />Abstractprimitiveoperations<br />Template method<br />
  13. 13. 13<br />Concrete Class<br />extends abstract class<br />Implement operation in each method<br />
  14. 14. 14<br />Concrete Class<br />Implement operation in each method<br />
  15. 15. Sequence Diagram<br />15<br />
  16. 16. NIG135Controller<br />16<br />it will be executed in sequence:<br />1. prepareDataSource<br />2. getReportTemplateFile<br />3. generateJasperPrint<br />4. setupExportFormat<br />5. exportReport<br />
  17. 17. Benefits<br />To make many similar operations template.<br />From many specialized operations to a generalized operation.<br />Refactor common behavior to simplify code.<br />Algorithm related improvement.<br />17<br />