Skip to content

Commit 995a745

Browse files
committed
add Template Pattern
1 parent 6c4b87b commit 995a745

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.template;
7+
8+
public class BasicDesktop extends DesktopTemplate
9+
{
10+
@Override
11+
public boolean addExtraRam()
12+
{
13+
return false;
14+
}
15+
16+
@Override
17+
public boolean addGraphicCard()
18+
{
19+
return false;
20+
}
21+
22+
@Override
23+
public boolean addSSD()
24+
{
25+
return false;
26+
}
27+
28+
@Override
29+
public boolean addExtraCooler()
30+
{
31+
return false;
32+
}
33+
}
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.template;
7+
8+
public abstract class DesktopTemplate
9+
{
10+
public void addCasing()
11+
{
12+
System.out.println("Added Basic Casing");
13+
}
14+
15+
public void addMother()
16+
{
17+
System.out.println("Added Motherboard");
18+
}
19+
20+
public void addCPU()
21+
{
22+
System.out.println("Added CPU");
23+
}
24+
25+
public void addHDD()
26+
{
27+
System.out.println("Added HDD");
28+
}
29+
30+
public void addCooler()
31+
{
32+
System.out.println("Added Cooler");
33+
}
34+
35+
public void addRam()
36+
{
37+
System.out.println("Added Ram");
38+
}
39+
40+
public abstract boolean addExtraRam();
41+
public abstract boolean addGraphicCard();
42+
public abstract boolean addSSD();
43+
public abstract boolean addExtraCooler();
44+
45+
public void build()
46+
{
47+
addCasing();
48+
addMother();
49+
addCPU();
50+
addHDD();
51+
addCooler();
52+
addRam();
53+
54+
if(addExtraRam())
55+
{
56+
System.out.println("Added Extra RAM");
57+
}
58+
if(addGraphicCard())
59+
{
60+
System.out.println("Added Graphic Card");
61+
}
62+
else
63+
{
64+
System.out.println("No Graphic Card");
65+
}
66+
if (addSSD())
67+
{
68+
System.out.println("Added SSD");
69+
}
70+
if (addExtraCooler())
71+
{
72+
System.out.println("Added extra cooler");
73+
}
74+
}
75+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.template;
7+
8+
public class HighEndDesktop extends DesktopTemplate
9+
{
10+
@Override
11+
public boolean addExtraRam()
12+
{
13+
return true;
14+
}
15+
16+
@Override
17+
public boolean addGraphicCard()
18+
{
19+
return true;
20+
}
21+
22+
@Override
23+
public boolean addSSD()
24+
{
25+
return true;
26+
}
27+
28+
@Override
29+
public boolean addExtraCooler()
30+
{
31+
return true;
32+
}
33+
34+
@Override
35+
public void addCasing()
36+
{
37+
System.out.println("Add Gaming Casing");
38+
}
39+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.template;
7+
8+
public class MediocreDesktop extends DesktopTemplate
9+
{
10+
@Override
11+
public boolean addExtraRam()
12+
{
13+
return true;
14+
}
15+
16+
@Override
17+
public boolean addGraphicCard()
18+
{
19+
return true;
20+
}
21+
22+
@Override
23+
public boolean addSSD()
24+
{
25+
return false;
26+
}
27+
28+
@Override
29+
public boolean addExtraCooler()
30+
{
31+
return false;
32+
}
33+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
* Author Steven Yeoh
3+
* Copyright (c) 2019. All rights reserved.
4+
*/
5+
6+
package com.dsl.design.pattern.template;
7+
8+
public class TemplatePatternDemo
9+
{
10+
public static void main(String[] args)
11+
{
12+
BasicDesktop basicDesktop = new BasicDesktop();
13+
System.out.println("============= BASIC DESKTOP =============");
14+
basicDesktop.build();
15+
System.out.println("============= BASIC DESKTOP =============");
16+
17+
System.out.println("\n============= MEDIOCRE DESKTOP =============");
18+
MediocreDesktop mediocreDesktop = new MediocreDesktop();
19+
mediocreDesktop.build();
20+
System.out.println("============= MEDIOCRE DESKTOP =============");
21+
22+
System.out.println("\n============= HIGH END DESKTOP =============");
23+
HighEndDesktop highEndDesktop = new HighEndDesktop();
24+
highEndDesktop.build();
25+
System.out.println("============= HIGH END DESKTOP =============");
26+
}
27+
}

0 commit comments

Comments
 (0)