Many times we need to perform certain tasks and also group them.

 

These tasks can be,

  • Executing commands on console.

  • Performing some SVN operation.

  • Performing any thing specific to our project.

  • Etc.

Common things in all these tasks can be,

  • Every task need to be executed.

  • Every task returns its log.

  • Every task need to record execution time.

Now what if we want to group this task i.e, combine some of the tasks to create a bigger Grouped Task.

 

Then it will be a perfect example of Composite Design Pattern,

 

Intent of Composite Design Pattern:

Compose objects into tree structures to represent whole-part hierarchies. Composite lets clients treat individual objects and compositions of objects uniformly.

 

For other modules all tasks has same kind of properties i.e it can execute and returns log, even if its a singletask or a group of tasks. External entities act on all tasks uniformly.

 

Task Group

It has the same common properties like,

 

  • It needs to be executed : It executes all the tasks it contains.

  • It returns its log : It returns the combined logs of all the tasks it contains.

  • It needs to record execution time : It returns the total time taken by all the tasks it contains.

 

A Group Task example can be,

Task Group 1:

              Display Task 1
              Task Group

                             Display Task 2

                              SVN Task

Task Hierarchy ,

 

RunnableTask

Task Grouping Class Diagram

 

A Task Group can contains other task groups too.

 

For external world its every task is a Runnable Task only. Other entities doesn’t know while they are executing a leaf kind of task or a composite kind of task. Thus this composition will be hidden from outside world.

 

Base Class : RunnableTask

TaskGroup class : Non-Leaf Node in Composite Design.

DiplayTask class : Leaf Node in Composite Design.

SVNTask class : Leaf Node in Composite Design.

Usage Details,