Skip to content

Commit cb73d25

Browse files
Chaklader Asfak ArefeChaklader Asfak Arefe
Chaklader Asfak Arefe
authored and
Chaklader Asfak Arefe
committed
updated
1 parent 35a1f08 commit cb73d25

36 files changed

+773
-0
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Design_Patterns_00/.DS_Store

0 Bytes
Binary file not shown.

Design_Patterns_00/Template/.DS_Store

10 KB
Binary file not shown.
Binary file not shown.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.javacodegeeks.patterns.templatepattern;
2+
3+
import java.util.Date;
4+
5+
public abstract class ConnectionTemplate {
6+
7+
private boolean isLoggingEnable = true;
8+
9+
public ConnectionTemplate(){
10+
isLoggingEnable = disableLogging();
11+
}
12+
13+
public final void run(){
14+
15+
setDBDriver();
16+
logging("Drivers set ["+new Date()+"]");
17+
18+
setCredentials();
19+
logging("Credentails set ["+new Date()+"]");
20+
21+
connect();
22+
logging("Conencted");
23+
24+
prepareStatement();
25+
logging("Statement prepared ["+new Date()+"]");
26+
27+
setData();
28+
logging("Data set ["+new Date()+"]");
29+
30+
insert();
31+
logging("Inserted ["+new Date()+"]");
32+
33+
close();
34+
logging("Conenctions closed ["+new Date()+"]");
35+
36+
destroy();
37+
logging("Object destoryed ["+new Date()+"]");
38+
}
39+
40+
public abstract void setDBDriver();
41+
42+
public abstract void setCredentials();
43+
44+
public void connect(){
45+
System.out.println("Setting connection...");
46+
}
47+
48+
public void prepareStatement(){
49+
System.out.println("Preparing insert statement...");
50+
}
51+
52+
public abstract void setData();
53+
54+
public void insert(){
55+
System.out.println("Inserting data...");
56+
}
57+
58+
public void close(){
59+
System.out.println("Closing connections...");
60+
}
61+
62+
public void destroy(){
63+
System.out.println("Destroying connection objects...");
64+
}
65+
66+
public boolean disableLogging(){
67+
return true;
68+
}
69+
70+
private void logging(String msg){
71+
if(isLoggingEnable){
72+
System.out.println("Logging....: "+msg);
73+
}
74+
}
75+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.javacodegeeks.patterns.templatepattern;
2+
3+
public class MySqLCSVCon extends ConnectionTemplate{
4+
5+
@Override
6+
public void setDBDriver() {
7+
System.out.println("Setting MySQL DB drivers...");
8+
}
9+
10+
@Override
11+
public void setCredentials() {
12+
System.out.println("Setting credentials for MySQL DB...");
13+
}
14+
15+
@Override
16+
public void setData() {
17+
System.out.println("Setting up data from csv file....");
18+
}
19+
20+
@Override
21+
public boolean disableLogging() {
22+
return false;
23+
}
24+
25+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.javacodegeeks.patterns.templatepattern;
2+
3+
public class OracleTxtCon extends ConnectionTemplate{
4+
5+
@Override
6+
public void setDBDriver() {
7+
System.out.println("Setting Oracle DB drivers...");
8+
}
9+
10+
@Override
11+
public void setCredentials() {
12+
System.out.println("Setting credentials for Oracle DB...");
13+
}
14+
15+
@Override
16+
public void setData() {
17+
System.out.println("Setting up data from txt file....");
18+
}
19+
20+
21+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.javacodegeeks.patterns.templatepattern;
2+
3+
4+
/*
5+
* The connection template pattern
6+
*/
7+
public class TestTemplatePattern {
8+
9+
public static void main(String[] args) {
10+
11+
System.out.println("For MYSQL....");
12+
ConnectionTemplate template = new MySqLCSVCon();
13+
template.run();
14+
15+
System.out.println("\nFor Oracle...");
16+
template = new OracleTxtCon();
17+
template.run();
18+
}
19+
}
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
2+
3+
INTENT
4+
——————
5+
Define the skeleton of an algorithm in an operation, deferring some steps to client subclasses. Template Method lets subclasses redefine certain steps of an algorithm without changing the algorithm's structure. Base class declares algorithm 'placeholders', and derived classes implement the placeholders.
6+
7+
8+
PROBLEM
9+
———————
10+
Two different components have significant similarities, but demonstrate no reuse of common interface or implementation. If a change common to both components becomes necessary, duplicate effort must be expended.
11+
12+
13+
DISCUSSION
14+
——————————
15+
The component designer decides which steps of an algorithm are invariant (or standard), and which are variant (or customizable). The invariant steps are implemented in an abstract base class, while the variant steps are either given a default implementation, or no implementation at all. The variant steps represent "hooks", or "placeholders", that can, or must, be supplied by the component's client in a concrete derived class.
16+
17+
The component designer mandates the required steps of an algorithm, and the ordering of the steps, but allows the component client to extend or replace some number of these steps.
18+
19+
Template Method is used prominently in frameworks. Each framework implements the invariant pieces of a domain's architecture, and defines "placeholders" for all necessary or interesting client customization options. In so doing, the framework becomes the "center of the universe", and the client customizations are simply "the third rock from the sun". This inverted control structure has been affectionately labelled "the Hollywood principle" - "don't call us, we'll call you".
20+
21+
22+
23+
CHECK LIST
24+
——————————
25+
26+
Examine the algorithm, and decide which steps are standard and which steps are peculiar to each of the current classes.
27+
28+
Define a new abstract base class to host the "don't call us, we'll call you" framework.
29+
30+
Move the shell of the algorithm (now called the "template method") and the definition of all standard steps to the new base class.
31+
32+
Define a placeholder or "hook" method in the base class for each step that requires many different implementations. This method can host a default implementation – or – it can be defined as abstract (Java) or pure virtual (C++).
33+
34+
Invoke the hook method(s) from the template method.
35+
36+
Each of the existing classes declares an "is-a" relationship to the new abstract base class.
37+
38+
Remove from the existing classes all the implementation details that have been moved to the base class.
39+
40+
The only details that will remain in the existing classes will be the implementation details peculiar to each derived class.
41+
42+
43+
RULES OF THUMB
44+
——————————————
45+
46+
Strategy is like Template Method except in its granularity.
47+
48+
Template Method uses inheritance to vary part of an algorithm. Strategy uses delegation to vary the entire algorithm.
49+
50+
Strategy modifies the logic of individual objects. Template Method modifies the logic of an entire class.
51+
52+
Factory Method is a specialization of Template Method.
53+
54+
55+
56+
57+
58+
59+
60+
61+
INTRODUCTION
62+
————————————
63+
64+
The Template Design Pattern is a behavior pattern and, as the name suggests, it provides a template or a structure of an algorithm which is used by users. A user provides its own implementation without changing the algorithm’s structure.
65+
66+
It is easier to understand this pattern with the help of a problem. We will understand the scenario in this section and will implement the solution using the Template pattern in a later section.
67+
68+
Have you ever connected to a relation database using your Java application? Let’s recall some important steps which are required to connect and insert data into the database. First, we need a driver according to the database we want to connect with. Then, we pass some credentials to the database, then, we prepare a statement, set data into the insert statement and insert it using the insert command. Later, we close all the connections, and optionally destroy all the connection objects.
69+
70+
You need to write all these steps regardless of any vendor’s relational database. Consider a problem where you need to insert some data into the different databases. You need to fetch some data from a CSV file and have to insert it into a MySQL database. Some data comes from a text file and which should be insert into an Oracle database. The only difference is the driver and the data, the rest of the steps should be the same, as JDBC provides a common set of interfaces to communicate to any vendor’s specific relation database.
71+
72+
We can create a template, which will perform some steps for the client, and we will leave some steps to let the client to implement them in its own specific way. Optionally, a client can override the default behavior of some already defined steps.
73+
74+
75+
76+
77+
78+
79+
80+
DESCRIPTION
81+
———————————
82+
83+
The Template Pattern defines the skeleton of an algorithm in an operation, deferring some steps to subclasses. Template Method lets subclasses to redefine certain steps of an algorithm without changing the algorithm’s structure.
84+
85+
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. 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.
86+
87+
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. 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.
88+
89+
90+
————————————————————————————————————————————————————————————————————————————————————
91+
STRUCTURE
92+
—————————
93+
94+
AbstractClass
95+
—————————————
96+
Defines abstract primitive operations that concrete subclasses define to implement steps of an algorithm.
97+
98+
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.
99+
100+
ConcreteClass
101+
—————————————
102+
Implements the primitive operations to carry.
103+
————————————————————————————————————————————————————————————————————————————————————
104+
105+
106+
107+
108+
109+
110+
USAGES
111+
——————
112+
To implement the invariant parts of an algorithm once and leave it up to subclasses to implement the behavior that can vary.
113+
114+
When common behavior among subclasses should be factored and localized in a common class to avoid code duplication. You first identify the differences in the existing code and then separate the differences into new operations. Finally, you replace the differing code with a template method that calls one of these new operations.
115+
116+
To control subclasses extensions. You can define a template method that calls “hook” operations (see Consequences) at specific points, thereby permitting extensions only at those points.
117+
118+
119+
TEMPLATE DESIGN PATTERN IN JDK
120+
——————————————————————————————
121+
i. java.util.Collections#sort()
122+
ii. java.io.InputStream#skip()
123+
iii. java.io.InputStream#read()
124+
iv. java.util.AbstractList#indexOf()
125+
126+
127+
65.4 KB
Loading
68.5 KB
Loading
93.9 KB
Loading

Design_Patterns_00/Template/barista/Coffee.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package design.design_patterns.templateMethod.barista;
22

3+
34
public class Coffee extends CaffeineBeverage {
5+
46
public void brew() {
57
System.out.println("Dripping Coffee through filter");
68
}
9+
710
public void addCondiments() {
811
System.out.println("Adding Sugar and Milk");
912
}

Design_Patterns_00/Template/barista/Tea.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package design.design_patterns.templateMethod.barista;
22

33
public class Tea extends CaffeineBeverage {
4+
45
public void brew() {
56
System.out.println("Steeping the tea");
67
}

Design_Patterns_00/Template/simplebarista/Barista.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
public class Barista {
44

55
public static void main(String[] args) {
6+
67
Tea tea = new Tea();
78
Coffee coffee = new Coffee();
89
System.out.println("Making tea...");
10+
911
tea.prepareRecipe();
12+
1013
System.out.println("Making coffee...");
1114
coffee.prepareRecipe();
1215
}

Design_Patterns_00/Template/sort/Duck.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package design.design_patterns.templateMethod.sort;
22

33
public class Duck implements Comparable<Duck> {
4+
45
String name;
56
int weight;
67

Design_Patterns_00/Visitor/.DS_Store

8 KB
Binary file not shown.
Binary file not shown.
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>VisitorPattern</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.javacodegeeks.patterns.visitorpattern;
2+
3+
public class CssClassVisitor implements Visitor{
4+
5+
@Override
6+
public void visit(HtmlElement element) {
7+
element.setStartTag(element.getStartTag().replace(">", " class='visitor'>"));
8+
9+
}
10+
11+
@Override
12+
public void visit(HtmlParentElement parentElement) {
13+
parentElement.setStartTag(parentElement.getStartTag().replace(">", " class='visitor'>"));
14+
}
15+
16+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.javacodegeeks.patterns.visitorpattern;
2+
3+
public interface Element {
4+
5+
public void accept(Visitor visitor);
6+
}

0 commit comments

Comments
 (0)