Skip to content

Commit 51072dc

Browse files
committed
Added adapter example, factory example and chain of responsibility example
1 parent 955d3fd commit 51072dc

File tree

20 files changed

+407
-3
lines changed

20 files changed

+407
-3
lines changed

pom.xml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,15 @@
7171
</plugin>
7272
</plugins>
7373
</pluginManagement>
74+
<plugins>
75+
<plugin>
76+
<groupId>org.apache.maven.plugins</groupId>
77+
<artifactId>maven-compiler-plugin</artifactId>
78+
<configuration>
79+
<source>9</source>
80+
<target>9</target>
81+
</configuration>
82+
</plugin>
83+
</plugins>
7484
</build>
7585
</project>

src/main/java/com/philipjhamilton/App.java

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
package com.philipjhamilton;
22

3+
import com.philipjhamilton.patterns.behavioral.chainofresponsibility.ATMDispenseChain;
4+
import com.philipjhamilton.patterns.behavioral.chainofresponsibility.Currency;
5+
import com.philipjhamilton.patterns.creational.factory.API;
6+
import com.philipjhamilton.patterns.creational.factory.APIFactory;
7+
import com.philipjhamilton.patterns.creational.objectpool.UserTemp;
8+
import com.philipjhamilton.patterns.creational.objectpool.UserTempOjbectPool;
39
import com.philipjhamilton.patterns.creational.prototype.Shape;
410
import com.philipjhamilton.patterns.creational.prototype.ShapeCache;
511
import com.philipjhamilton.patterns.creational.singleton.SingletonLazyInit;
12+
import com.philipjhamilton.patterns.structural.adapter.RoundHole;
13+
import com.philipjhamilton.patterns.structural.adapter.RoundPeg;
14+
import com.philipjhamilton.patterns.structural.adapter.SquarePeg;
15+
import com.philipjhamilton.patterns.structural.adapter.SquarePegAdapter;
616

717
/**
818
* Hello world!
@@ -23,15 +33,51 @@ public static void main( String[] args )
2333

2434
Shape s = ShapeCache.getShape("0");
2535

26-
System.out.println(s.getType());
36+
System.out.println(s);
2737

2838
Shape s2 = ShapeCache.getShape("1");
2939

30-
System.out.println(s2.getType());
40+
System.out.println(s2);
3141

3242
Shape s3 = ShapeCache.getShape("2");
3343

34-
System.out.println(s3.getType());
44+
System.out.println(s3);
3545

46+
//Factory pattern
47+
API api = APIFactory.getApiInstance("v1");
48+
49+
api.doOneMoreThing();
50+
api.doSomething();
51+
52+
api = APIFactory.getApiInstance("v2");
53+
54+
api.doOneMoreThing();
55+
api.doSomething();
56+
57+
//Chain of responsibility
58+
ATMDispenseChain atmDispenser = new ATMDispenseChain();
59+
60+
atmDispenser.dispense(530);
61+
62+
//Adapter pattern
63+
RoundHole hole = new RoundHole(5);
64+
RoundPeg rpeg = new RoundPeg(5);
65+
if (hole.fits(rpeg)) {
66+
System.out.println("Round peg r5 fits round hole r5.");
67+
}
68+
69+
SquarePeg smallSqPeg = new SquarePeg(2);
70+
SquarePeg largeSqPeg = new SquarePeg(20);
71+
// hole.fits(smallSqPeg); // Won't compile.
72+
73+
// Adapter solves the problem.
74+
SquarePegAdapter smallSqPegAdapter = new SquarePegAdapter(smallSqPeg);
75+
SquarePegAdapter largeSqPegAdapter = new SquarePegAdapter(largeSqPeg);
76+
if (hole.fits(smallSqPegAdapter)) {
77+
System.out.println("Square peg w2 fits round hole r5.");
78+
}
79+
if (!hole.fits(largeSqPegAdapter)) {
80+
System.out.println("Square peg w20 does not fit into round hole r5.");
81+
}
3682
}
3783
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package com.philipjhamilton.patterns.behavioral.chainofresponsibility;
2+
3+
public class ATMDispenseChain {
4+
5+
private DispenseChain c1;
6+
7+
public ATMDispenseChain(){
8+
this.c1 = new Dollar50Dispenser();
9+
DispenseChain c2 = new Dollar20Dispenser();
10+
DispenseChain c3 = new Dollar10Dispenser();
11+
12+
c1.setNextChain(c2);
13+
c2.setNextChain(c3);
14+
}
15+
16+
public void dispense(int amount){
17+
c1.dispense(new Currency(amount));
18+
}
19+
20+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package com.philipjhamilton.patterns.behavioral.chainofresponsibility;
2+
3+
public class Currency {
4+
5+
private int amount;
6+
7+
public Currency(int amt){
8+
this.amount=amt;
9+
}
10+
11+
public int getAmount(){
12+
return this.amount;
13+
}
14+
15+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package com.philipjhamilton.patterns.behavioral.chainofresponsibility;
2+
3+
public interface DispenseChain {
4+
5+
void setNextChain(DispenseChain nextChain);
6+
7+
void dispense(Currency cur);
8+
9+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.philipjhamilton.patterns.behavioral.chainofresponsibility;
2+
3+
public class Dollar10Dispenser implements DispenseChain {
4+
5+
private DispenseChain chain;
6+
7+
@Override
8+
public void setNextChain(DispenseChain nextChain) {
9+
this.chain=nextChain;
10+
}
11+
12+
@Override
13+
public void dispense(Currency cur) {
14+
if(cur.getAmount() >= 10){
15+
int num = cur.getAmount()/10;
16+
int remainder = cur.getAmount() % 10;
17+
System.out.println("Dispensing " + num + " 10$ note");
18+
if(remainder != 0){
19+
this.chain.dispense(new Currency(remainder));
20+
}
21+
}
22+
}
23+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.philipjhamilton.patterns.behavioral.chainofresponsibility;
2+
3+
public class Dollar20Dispenser implements DispenseChain {
4+
5+
private DispenseChain chain;
6+
7+
@Override
8+
public void setNextChain(DispenseChain nextChain) {
9+
this.chain=nextChain;
10+
}
11+
12+
@Override
13+
public void dispense(Currency cur) {
14+
if(cur.getAmount() >= 20){
15+
int num = cur.getAmount()/20;
16+
int remainder = cur.getAmount() % 20;
17+
System.out.println("Dispensing " + num + " 20$ note");
18+
if(remainder != 0){
19+
this.chain.dispense(new Currency(remainder));
20+
}else{
21+
this.chain.dispense(cur);
22+
}
23+
}
24+
}
25+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.philipjhamilton.patterns.behavioral.chainofresponsibility;
2+
3+
public class Dollar50Dispenser implements DispenseChain {
4+
5+
private DispenseChain chain;
6+
7+
@Override
8+
public void setNextChain(DispenseChain nextChain) {
9+
this.chain=nextChain;
10+
}
11+
12+
@Override
13+
public void dispense(Currency cur) {
14+
if(cur.getAmount() >= 50){
15+
int num = cur.getAmount()/50;
16+
int remainder = cur.getAmount() % 50;
17+
System.out.println("Dispensing " + num + " 50$ note");
18+
if(remainder != 0){
19+
this.chain.dispense(new Currency(remainder));
20+
}else{
21+
this.chain.dispense(cur);
22+
}
23+
}
24+
}
25+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package com.philipjhamilton.patterns.creational.factory;
2+
3+
public interface API {
4+
public void doSomething();
5+
public void doOneMoreThing();
6+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.philipjhamilton.patterns.creational.factory;
2+
3+
public class APIFactory {
4+
5+
public static API getApiInstance(String version){
6+
if("v1".equalsIgnoreCase(version)) {
7+
return new ApiV1();
8+
}else if ("v2".equalsIgnoreCase(version)){
9+
return new ApiV2();
10+
}else
11+
throw new IllegalArgumentException("Passed API version does not exist.");
12+
}
13+
}

0 commit comments

Comments
 (0)