Tải bản đầy đủ (.pdf) (25 trang)

Software design: Lecture 41 - Sheraz Pervaiz

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (344.83 KB, 25 trang )

1

Software Design
Lecture : 41


2

Template Design Pattern
OR
Pattern Design Pattern


3

Software Processes and Templates
i.

Requirement Specification Template

ii. Functional Specfication Template
iii. Design Document Template
iv. Audit Documents
v. Example : Volere Template  etc


4

Motivation for Template Design 
Pattern
 The  Template  Method  pattern  can  be  used  in  situations 


when  there  is  an    algorithm,  some  steps  of  which  could 
be implemented in multiple different ways.

 Some  portion  of  the  solution  is  fix  for  all  the  scenarios 
and  some  portion  of  the  solution  is  specific  to  any 
situation


5

Motivation for Template Pattern
 In such scenarios, the Template Method pattern suggests 
keeping  the  outline  of  the  algorithm  in  a  separate 
method referred to as a template method inside a class, 
which  may  be  referred  to  as  a  template  class,  leaving 
out the specific implementations of the variant portions 
(steps  that  can  be  implemented  in  multiple  different 
ways)  of  the  algorithm  to  different  subclasses  of  this 
class.


6

Motivation for Using Template 
Pattern
 The Template class does not necessarily have to leave the 
implementation  to  subclasses  in  its  entirety.  Instead,  as 
part  of  providing  the  outline  of  the  algorithm,  the 
Template  class  can  also  provide  some  amount  of 
implementation  that  can  be  considered  as  invariant 

across different implementations


7

Motivation for Template Pattern
 It  can  even  provide  default  implementation  for  the 
variant parts, if appropriate. Only specific details will be 
implemented  inside  different  subclasses.  This  type  of 
implementation  eliminates  the  need  for  duplicate  code, 
which means a minimum amount of code to be written.


8

Intent of Template Pattern
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  letting  them  to 
change the algorithm's structure.


9

A discussion on Final Method 
type
In  java  when  we  want  to  restrict  the  subclass 
from  overriding  the  super  class  method,  we  can 
declare  that  super  class  method  a  “Final” 

method


10

Framework Vs Library


11

Hollywood principle
 “Don't call us, we'll call you”
 This refers to the fact that instead of calling the methods 
from  base  class  in  the  subclasses,  the  methods  from 
subclass  are  called  in  the  template  method  from 
superclass.


12

Hooks or Hot Spots
 The hooks are generally empty methods that are called in 
superclass  (and  does  nothing  because  are  empty),  but 
can be implemented in subclasses.

 Customization Hooks can be considered a particular case 
of  the  template  method  as  well  as  a  totally  different 
mechanism.



13

Class Diagram


14


15

Components of Abstract Class
Abstract Class
 defines  abstract  primitive  operations  that 
concrete subclasses define to implement steps of 
an algorithm.
Concrete  Class  ­  implements  the  primitive 
operations  to  carry  out  subclass­specific  steps  of 
the algorithm.


16

Problem Statement


17

• Lets'  assume  we  have  to  develop  an  application  for  a 
travel  agency.  The  travel  agency  is  managing  each  trip. 
All  the  trips  contain  common  behavior  but  there  are 

several  packages.  For  example  each  trip  contains  the 
basic steps:

i. The tourists are transported to the holiday location by 
plane/train/ships,...
ii.  Each day they are visiting something
iii. They are returning back home.


18

Proposed Class Diagram


19


20

Java Code


21

public class Trip {
        public final void performTrip(){
                 doComingTransport();
                 doDayA();
                 doDayB();
                 doDayC();

                 doReturningTransport
        }
        public abstract void doComingTransport();
        public abstract void doDayA();
        public abstract void doDayB();
        public abstract void doDayC();
        public abstract void doReturningTransport();
}


22

public class PackageA extends Trip 
{
        public void doComingTransport() 
{
  System.out.println("The turists are comming by air ...");
        }
        public void doDayA() {
     System.out.println("The turists are visiting the aquarium ...");
        }
        public void doDayB() {
     System.out.println("The turists are going to the museum  ...");
        }
        public void doDayC() {
          System.out.println("The turists are going to mountains ...");
        }
        public void doReturningTransport() {
                 System.out.println("The turists are going home by air ...");
        }}



23

public class PackageB extends Trip {
        public void doComingTransport() {
                System.out.println("The turists are comming by train ...");
        }
        public void doDayA() {
      System.out.println("The turists are visiting the mountain ...");
        }
        public void doDayB() {
  System.out.println("The turists are going to the olympic  
stadium ...");
        }
        public void doDayC() {
                 System.out.println("The turists are going to zoo ...");
        }
        public void doReturningTransport() {
         System.out.println("The turists are going home by train ...");
        }
}


24

To Do Tasks


25


i.

Identify  a  scenario  where    there  should  be  2  level  of 
template  design  pattern  implementation­e  Subclasses 
should  behave  as  template  class  also  for  the  level  2 
subclasses.

ii. Write  a  scenario  where  template  design  pattern  is 
replaceable by Observer Design Pattern. 

iii. Write  a  scenario  where  template  pattern  can  be  used   
along with iterator pattern


×