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
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();
}
}
Source code of Editor.java
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!");
}
}
Source code of Browser.java
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.");
}
}
Source code of TestMain.java
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();
}
}
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.