Behavioural Design Pattern: Template Pattern - BunksAllowed

BunksAllowed is an effort to facilitate Self Learning process through the provision of quality tutorials.

Community

demo-image

Behavioural Design Pattern: Template Pattern

Share This



Template Pattern defines the skeleton of a function in an operation, delegating some steps to its subclasses. It is an extremely prevalent method for retaining code. This is merely its primary advantage.

When common behavior among subclasses should be consolidated into a single common class to prevent duplication, this method is implemented.

Source code of Software.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
package com.t4b.test.java.dp.bp.tp;
abstract class Software {
abstract void initialize();
abstract void start();
abstract void end();
public final void play() {
initialize();
start();
end();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of Editor.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
package com.t4b.test.java.dp.bp.tp;
class Editor extends Software {
@Override
void end() {
System.out.println("Editor Finished!");
}
@Override
void initialize() {
System.out.println("Editor Initialized!");
}
@Override
void start() {
System.out.println("Editor Started!");
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of Browser.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
package com.t4b.test.java.dp.bp.tp;
class Browser extends Software {
@Override
void end() {
System.out.println("Browser Finished!");
}
@Override
void initialize() {
System.out.println("Browser Initialized!.");
}
@Override
void start() {
System.out.println("Browser Started.");
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
Source code of TestMain.java
1
2
3
4
5
6
7
8
9
10
11
12
13
package com.t4b.test.java.dp.bp.tp;
public class TestMain {
public static void main(String[] args) {
Software software = new Browser();
software.play();
software = new Editor();
software.play();
}
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

Happy Exploring!

Comment Using!!

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.