Skip to content

Commit 243f379

Browse files
committed
feat: Adapter,Bridge,Prototype,Singleton patterns
1 parent 205c32f commit 243f379

File tree

4 files changed

+220
-0
lines changed

4 files changed

+220
-0
lines changed

Patterns/AdapterPattern.java

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
interface ILightningPhone {
2+
void recharge();
3+
void useLightning();
4+
}
5+
6+
interface IMicroUsbPhone {
7+
void recharge();
8+
void useMicroUsb();
9+
}
10+
11+
class Iphone implements ILightningPhone {
12+
private boolean connector;
13+
14+
@Override
15+
public void useLightning() {
16+
connector = true;
17+
System.out.println("Lightning connected");
18+
}
19+
20+
@Override
21+
public void recharge() {
22+
if (connector) {
23+
System.out.println("Recharge started");
24+
System.out.println("Recharge finished");
25+
} else {
26+
System.out.println("Connect Lightning first");
27+
}
28+
}
29+
}
30+
31+
class Android implements IMicroUsbPhone {
32+
private boolean connector;
33+
34+
@Override
35+
public void useMicroUsb() {
36+
connector = true;
37+
System.out.println("MicroUsb connected");
38+
}
39+
40+
@Override
41+
public void recharge() {
42+
if (connector) {
43+
System.out.println("Recharge started");
44+
System.out.println("Recharge finished");
45+
} else {
46+
System.out.println("Connect MicroUsb first");
47+
}
48+
}
49+
}
50+
/* exposing the target interface while wrapping source object */
51+
class LightningToMicroUsbAdapter implements IMicroUsbPhone {
52+
private final ILightningPhone lightningPhone;
53+
54+
public LightningToMicroUsbAdapter(ILightningPhone lightningPhone) {
55+
this.lightningPhone = lightningPhone;
56+
}
57+
58+
@Override
59+
public void useMicroUsb() {
60+
System.out.println("MicroUsb connected");
61+
lightningPhone.useLightning();
62+
}
63+
64+
@Override
65+
public void recharge() {
66+
lightningPhone.recharge();
67+
}
68+
}
69+
70+
public class AdapterPattern {
71+
static void rechargeMicroUsbPhone(IMicroUsbPhone phone) {
72+
phone.useMicroUsb();
73+
phone.recharge();
74+
}
75+
76+
static void rechargeLightningPhone(ILightningPhone phone) {
77+
phone.useLightning();
78+
phone.recharge();
79+
}
80+
81+
public static void main(String[] args) {
82+
Android android = new Android();
83+
Iphone iPhone = new Iphone();
84+
85+
System.out.println("Recharging android with MicroUsb");
86+
rechargeMicroUsbPhone(android);
87+
88+
System.out.println("Recharging iPhone with Lightning");
89+
rechargeLightningPhone(iPhone);
90+
91+
System.out.println("Recharging iPhone with MicroUsb");
92+
rechargeMicroUsbPhone(new LightningToMicroUsbAdapter (iPhone));
93+
}
94+
}

Patterns/BridgePattern.java

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Logger has two implementations: info and warning
2+
@FunctionalInterface
3+
interface Logger {
4+
void log(String message);
5+
6+
static Logger info() {
7+
return message -> System.out.println("info: " + message);
8+
}
9+
static Logger warning() {
10+
return message -> System.out.println("warning: " + message);
11+
}
12+
}
13+
14+
abstract class AbstractAccount {
15+
private Logger logger = Logger.info();
16+
17+
public void setLogger(Logger logger) {
18+
this.logger = logger;
19+
}
20+
21+
// the logging part is delegated to the Logger implementation
22+
protected void operate(String message, boolean result) {
23+
logger.log(message + " result " + result);
24+
}
25+
}
26+
27+
class SimpleAccount extends AbstractAccount {
28+
private int balance;
29+
30+
public SimpleAccount(int balance) {
31+
this.balance = balance;
32+
}
33+
34+
public boolean isBalanceLow() {
35+
return balance < 50;
36+
}
37+
38+
public void withdraw(int amount) {
39+
boolean shouldPerform = balance >= amount;
40+
if (shouldPerform) {
41+
balance -= amount;
42+
}
43+
operate("withdraw " + amount, shouldPerform);
44+
}
45+
}
46+
47+
public class BridgePattern {
48+
public static void main(String[] args) {
49+
SimpleAccount account = new SimpleAccount(100);
50+
account.withdraw(75);
51+
52+
if (account.isBalanceLow()) {
53+
// you can also change the Logger implementation at runtime
54+
account.setLogger(Logger.warning());
55+
}
56+
57+
account.withdraw(10);
58+
account.withdraw(100);
59+
}
60+
}

Patterns/PrototypePattern.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Prototype pattern
2+
abstract class Prototype implements Cloneable {
3+
public Prototype clone() throws CloneNotSupportedException{
4+
return (Prototype) super.clone();
5+
}
6+
}
7+
8+
class ConcretePrototype1 extends Prototype {
9+
@Override
10+
public Prototype clone() throws CloneNotSupportedException {
11+
return (ConcretePrototype1)super.clone();
12+
}
13+
}
14+
15+
class ConcretePrototype2 extends Prototype {
16+
@Override
17+
public Prototype clone() throws CloneNotSupportedException {
18+
return (ConcretePrototype2)super.clone();
19+
}
20+
}
21+
22+
public class PrototypePattern {
23+
24+
public static void main (String [] args) throws CloneNotSupportedException {
25+
Prototype car1 = new ConcretePrototype1();
26+
Prototype car2 = new ConcretePrototype2();
27+
28+
Prototype anotherCar1 = car1.clone();
29+
Prototype anotherCar2 = car2.clone();
30+
}
31+
}

Patterns/Singleton.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
class Coin {
2+
3+
private static final int ADD_MORE_COIN = 10;
4+
private int coin;
5+
private static Coin instance = new Coin(); // eagerly loads the singleton
6+
7+
private Coin() {
8+
// private to prevent anyone else from instantiating
9+
}
10+
11+
public static Coin getInstance() {
12+
return instance;
13+
}
14+
15+
public int getCoin() {
16+
return coin;
17+
}
18+
19+
public void addMoreCoin() {
20+
coin += ADD_MORE_COIN;
21+
}
22+
23+
public void deductCoin() {
24+
coin--;
25+
}
26+
}
27+
28+
public class Singleton {
29+
30+
public static void main(String [] args) {
31+
Coin coin = Coin.getInstance();
32+
coin.addMoreCoin();
33+
}
34+
35+
}

0 commit comments

Comments
 (0)