Skip to content

Commit 189c1ef

Browse files
authored
Add files via upload
1 parent 3a51121 commit 189c1ef

File tree

13 files changed

+1536
-1
lines changed

13 files changed

+1536
-1
lines changed

README.md

+456-1
Large diffs are not rendered by default.
+80
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package adapter_design_pattern;
2+
3+
interface WebDriver
4+
{
5+
public void getElement();
6+
public void selectElement();
7+
}
8+
9+
class ChromeDriver implements WebDriver
10+
{
11+
@Override
12+
public void getElement()
13+
{
14+
System.out.println("Get element from ChromeDriver");
15+
}
16+
17+
@Override
18+
public void selectElement()
19+
{
20+
System.out.println("Select element from ChromeDriver");
21+
22+
}
23+
24+
}
25+
26+
class IEDriver
27+
{
28+
public void findElement()
29+
{
30+
System.out.println("Find element from IEDriver");
31+
}
32+
33+
public void clickElement()
34+
{
35+
System.out.println("Click element from IEDriver");
36+
}
37+
38+
}
39+
40+
class WebDriverAdapter implements WebDriver
41+
{
42+
IEDriver ieDriver;
43+
public WebDriverAdapter(IEDriver ieDriver)
44+
{
45+
this.ieDriver = ieDriver;
46+
}
47+
48+
@Override
49+
public void getElement()
50+
{
51+
ieDriver.findElement();
52+
53+
}
54+
55+
@Override
56+
public void selectElement()
57+
{
58+
ieDriver.clickElement();
59+
}
60+
61+
}
62+
63+
public class AdapterPattern
64+
{
65+
public static void main(String[] args)
66+
{
67+
ChromeDriver a = new ChromeDriver();
68+
a.getElement();
69+
a.selectElement();
70+
71+
IEDriver e = new IEDriver();
72+
e.findElement();
73+
e.clickElement();
74+
75+
WebDriver wID = new WebDriverAdapter(e);
76+
wID.getElement();
77+
wID.selectElement();
78+
79+
}
80+
}
+118
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
package bridge_design_pattern;
2+
3+
abstract class TV
4+
{
5+
Remote remote;
6+
TV(Remote r)
7+
{
8+
this.remote = r;
9+
}
10+
abstract void on();
11+
abstract void off();
12+
}
13+
14+
class Sony extends TV
15+
{
16+
Remote remoteType;
17+
Sony(Remote r)
18+
{
19+
super(r);
20+
this.remoteType = r;
21+
}
22+
23+
public void on()
24+
{
25+
System.out.print("Sony TV ON: ");
26+
remoteType.on();
27+
}
28+
29+
public void off()
30+
{
31+
System.out.print("Sony TV OFF: ");
32+
remoteType.off();
33+
}
34+
}
35+
36+
class Philips extends TV
37+
{
38+
Remote remoteType;
39+
Philips(Remote r)
40+
{
41+
super(r);
42+
this.remoteType = r;
43+
}
44+
45+
public void on()
46+
{
47+
System.out.print("Philips TV ON: ");
48+
remoteType.on();
49+
}
50+
51+
public void off()
52+
{
53+
System.out.print("Philips TV OFF: ");
54+
remoteType.off();
55+
}
56+
}
57+
58+
interface Remote
59+
{
60+
public void on();
61+
public void off();
62+
}
63+
64+
class OldRemote implements Remote
65+
{
66+
@Override
67+
public void on()
68+
{
69+
System.out.println("ON with Old Remote");
70+
}
71+
72+
@Override
73+
public void off()
74+
{
75+
System.out.println("OFF with old Remote");
76+
}
77+
78+
}
79+
80+
class NewRemote implements Remote
81+
{
82+
@Override
83+
public void on()
84+
{
85+
System.out.println("ON with New Remote");
86+
}
87+
88+
@Override
89+
public void off()
90+
{
91+
System.out.println("OFF with New Remote");
92+
}
93+
}
94+
95+
public class BridgePattern
96+
{
97+
public static void main(String[] args)
98+
{
99+
TV sonyOldRemote = new Sony(new OldRemote());
100+
sonyOldRemote.on();
101+
sonyOldRemote.off();
102+
System.out.println();
103+
104+
TV sonyNewRemote = new Sony(new NewRemote());
105+
sonyNewRemote.on();
106+
sonyNewRemote.off();
107+
System.out.println();
108+
109+
TV philipsOldRemote = new Philips(new OldRemote());
110+
philipsOldRemote.on();
111+
philipsOldRemote.off();
112+
System.out.println();
113+
114+
TV philipsNewRemote = new Philips(new NewRemote());
115+
philipsNewRemote.on();
116+
philipsNewRemote.off();
117+
}
118+
}
+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package builder_design_pattern;
2+
3+
class Vehicle
4+
{
5+
private String engine;
6+
private int wheel;
7+
private int airbags;
8+
9+
public String getEngine()
10+
{
11+
return this.engine;
12+
}
13+
14+
public int getWheel()
15+
{
16+
return this.wheel;
17+
}
18+
19+
public int getAirbags()
20+
{
21+
return this.airbags;
22+
}
23+
24+
private Vehicle(VehicleBuilder builder)
25+
{
26+
this.engine = builder.engine;
27+
this.wheel = builder.wheel;
28+
this.airbags = builder.airbags;
29+
}
30+
31+
public static class VehicleBuilder
32+
{
33+
private String engine;
34+
private int wheel;
35+
private int airbags;
36+
37+
public VehicleBuilder(String engine, int wheel)
38+
{
39+
this.engine = engine;
40+
this.wheel = wheel;
41+
}
42+
43+
public VehicleBuilder setAirbags(int airbags)
44+
{
45+
this.airbags = airbags;
46+
return this;
47+
}
48+
49+
public Vehicle build()
50+
{
51+
return new Vehicle(this);
52+
}
53+
}
54+
}
55+
56+
public class BuilderPattern
57+
{
58+
public static void main(String[] args)
59+
{
60+
Vehicle car = new Vehicle.VehicleBuilder("1500cc", 4).setAirbags(4).build();
61+
Vehicle bike = new Vehicle.VehicleBuilder("250cc", 2).build();
62+
63+
System.out.println(car.getEngine());
64+
System.out.println(car.getWheel());
65+
System.out.println(car.getAirbags());
66+
67+
System.out.println(bike.getEngine());
68+
System.out.println(bike.getWheel());
69+
System.out.println(bike.getAirbags());
70+
}
71+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package composite_design_pattern;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
abstract class Account
7+
{
8+
public abstract float getBalance();
9+
}
10+
11+
class DepositeAccount extends Account
12+
{
13+
private String accountNo;
14+
private float accountBalance;
15+
16+
public DepositeAccount(String accountNo, float accountBalance)
17+
{
18+
super();
19+
this.accountNo = accountNo;
20+
this.accountBalance = accountBalance;
21+
}
22+
23+
public float getBalance()
24+
{
25+
return accountBalance;
26+
}
27+
}
28+
29+
class SavingAccount extends Account
30+
{
31+
private String accountNo;
32+
private float accountBalance;
33+
34+
public SavingAccount(String accountNo, float accountBalance)
35+
{
36+
super();
37+
this.accountNo = accountNo;
38+
this.accountBalance = accountBalance;
39+
}
40+
41+
public float getBalance()
42+
{
43+
return accountBalance;
44+
}
45+
}
46+
47+
class CompositeAccount extends Account
48+
{
49+
private float totalBalance;
50+
private List<Account> accountList = new ArrayList<Account>();
51+
52+
public float getBalance()
53+
{
54+
totalBalance = 0;
55+
for (Account f : accountList)
56+
{
57+
totalBalance = totalBalance + f.getBalance();
58+
}
59+
return totalBalance;
60+
}
61+
62+
public void addAccount(Account acc)
63+
{
64+
accountList.add(acc);
65+
}
66+
67+
public void removeAccount(Account acc)
68+
{
69+
accountList.add(acc);
70+
}
71+
}
72+
73+
public class CompositePattern
74+
{
75+
public static void main(String[] args)
76+
{
77+
CompositeAccount component = new CompositeAccount();
78+
79+
component.addAccount(new DepositeAccount("DA001", 100));
80+
component.addAccount(new DepositeAccount("DA002", 150));
81+
component.addAccount(new SavingAccount("SA001", 200));
82+
83+
float totalBalance = component.getBalance();
84+
System.out.println("Total Balance : " + totalBalance);
85+
}
86+
}

0 commit comments

Comments
 (0)