Skip to content

Commit cec55ee

Browse files
committed
14:16 13/10/2019
1 parent afbe8d8 commit cec55ee

62 files changed

Lines changed: 2282 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
32.9 KB
Loading
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package adapter;
2+
3+
/**
4+
* @author Khalid Elshafie <abolkog@gmail.com>
5+
* @created 27/02/2018.
6+
*/
7+
8+
//Adaptee
9+
public class Bicycle {
10+
11+
public void pedal(){
12+
System.out.println("Bic starts to move");
13+
}
14+
15+
public void stop() {
16+
System.out.println("Bic stopped");
17+
}
18+
19+
public void ringBell() {
20+
System.out.println("Ring ring");
21+
}
22+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package adapter;
2+
3+
/**
4+
* @author Khalid Elshafie <abolkog@gmail.com>
5+
* @created 27/02/2018.
6+
*/
7+
8+
//Adapter
9+
public class BicycleAdapter implements Vehicle {
10+
private Bicycle bicycle;
11+
12+
13+
public BicycleAdapter(Bicycle bicycle) {
14+
this.bicycle = bicycle;
15+
}
16+
17+
@Override
18+
public void accelerate() {
19+
bicycle.pedal();
20+
}
21+
22+
@Override
23+
public void pushBreak() {
24+
bicycle.stop();
25+
}
26+
27+
@Override
28+
public void soundHorn() {
29+
bicycle.ringBell();
30+
}
31+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package adapter;
2+
3+
/**
4+
* @author Khalid Elshafie <abolkog@gmail.com>
5+
* @created 27/02/2018.
6+
*/
7+
public class Car implements Vehicle {
8+
9+
@Override
10+
public void accelerate() {
11+
System.out.println("Car starts to move");
12+
}
13+
14+
@Override
15+
public void pushBreak() {
16+
System.out.println("Car Stopped");
17+
}
18+
19+
@Override
20+
public void soundHorn() {
21+
System.out.println("Beeb Beep");
22+
}
23+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package adapter;
2+
3+
/**
4+
* @author Khalid Elshafie <abolkog@gmail.com>
5+
* @created 27/02/2018.
6+
*/
7+
8+
//Client
9+
public class Client {
10+
11+
public static void main(String[] args) {
12+
Vehicle car = new Car();
13+
14+
Vehicle bicycle = new BicycleAdapter(new Bicycle());
15+
16+
playWithVehicle(car);
17+
18+
playWithVehicle(bicycle);
19+
}
20+
21+
private static void playWithVehicle(Vehicle vehicle) {
22+
23+
vehicle.accelerate();
24+
vehicle.pushBreak();
25+
vehicle.soundHorn();
26+
System.out.println("\n");
27+
}
28+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package adapter;
2+
3+
/**
4+
* @author Khalid Elshafie <abolkog@gmail.com>
5+
* @created 27/02/2018.
6+
*/
7+
8+
//Target
9+
public interface Vehicle {
10+
11+
void accelerate();
12+
void pushBreak();
13+
void soundHorn();
14+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<class-diagram version="1.2.3" icons="true" always-add-relationships="true" generalizations="true" realizations="true"
3+
associations="true" dependencies="true" nesting-relationships="true" router="FAN">
4+
<class id="1" language="java" name="adapter.Bicycle" project="java-design-patterns"
5+
file="/java-design-patterns/src/adapter/Bicycle.java" binary="false" corner="BOTTOM_RIGHT">
6+
<position height="-1" width="-1" x="1166" y="378"/>
7+
<display autosize="true" stereotype="false" package="false" initial-value="false" signature="true"
8+
sort-features="false" accessors="true" visibility="true">
9+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
10+
<operations public="true" package="true" protected="true" private="true" static="true"/>
11+
</display>
12+
</class>
13+
<class id="2" language="java" name="adapter.Car" project="java-design-patterns"
14+
file="/java-design-patterns/src/adapter/Car.java" binary="false" corner="BOTTOM_RIGHT">
15+
<position height="-1" width="-1" x="273" y="399"/>
16+
<display autosize="true" stereotype="false" package="false" initial-value="false" signature="true"
17+
sort-features="false" accessors="true" visibility="true">
18+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
19+
<operations public="true" package="true" protected="true" private="true" static="true"/>
20+
</display>
21+
</class>
22+
<interface id="3" language="java" name="adapter.Vehicle" project="java-design-patterns"
23+
file="/java-design-patterns/src/adapter/Vehicle.java" binary="false" corner="BOTTOM_RIGHT">
24+
<position height="-1" width="-1" x="620" y="138"/>
25+
<display autosize="true" stereotype="false" package="false" initial-value="false" signature="true"
26+
sort-features="false" accessors="true" visibility="true">
27+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
28+
<operations public="true" package="true" protected="true" private="true" static="true"/>
29+
</display>
30+
</interface>
31+
<class id="4" language="java" name="adapter.BicycleAdapter" project="java-design-patterns"
32+
file="/java-design-patterns/src/adapter/BicycleAdapter.java" binary="false" corner="BOTTOM_RIGHT">
33+
<position height="-1" width="-1" x="774" y="384"/>
34+
<display autosize="true" stereotype="false" package="false" initial-value="false" signature="true"
35+
sort-features="false" accessors="true" visibility="true">
36+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
37+
<operations public="true" package="true" protected="true" private="true" static="true"/>
38+
</display>
39+
</class>
40+
<realization id="5">
41+
<end type="SOURCE" refId="2"/>
42+
<end type="TARGET" refId="3"/>
43+
</realization>
44+
<realization id="6">
45+
<end type="SOURCE" refId="4"/>
46+
<end type="TARGET" refId="3"/>
47+
</realization>
48+
<association id="7">
49+
<bendpoint x="986" y="382"/>
50+
<end type="SOURCE" refId="4" navigable="false">
51+
<attribute id="8" name="bicycle"/>
52+
<multiplicity id="9" minimum="0" maximum="1"/>
53+
</end>
54+
<end type="TARGET" refId="1" navigable="true"/>
55+
<display labels="true" multiplicity="true"/>
56+
</association>
57+
<classifier-display autosize="true" stereotype="false" package="false" initial-value="false" signature="true"
58+
sort-features="false" accessors="true" visibility="true">
59+
<attributes public="true" package="true" protected="true" private="true" static="true"/>
60+
<operations public="true" package="true" protected="true" private="true" static="true"/>
61+
</classifier-display>
62+
<association-display labels="true" multiplicity="true"/>
63+
</class-diagram>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package command;
2+
3+
/**
4+
* @author Khalid Elshafie <abolkog@gmail.com>
5+
* @created 14/03/2018.
6+
*/
7+
public class Client {
8+
9+
public static void main(String[] args) {
10+
RemoteControl remoteControl = new RemoteControl();
11+
12+
Light light = new Light();
13+
TurnOnLightCommand turnOnLightCommand = new TurnOnLightCommand(light);
14+
TurnOffLightCommand turnOffLightCommand = new TurnOffLightCommand(light);
15+
16+
TV tv = new TV();
17+
TurnOnTVCommand turnOnTVCommand = new TurnOnTVCommand(tv);
18+
TurnOffTVCommand turnOffTVCommand = new TurnOffTVCommand(tv);
19+
20+
MusicPlayer musicPlayer = new MusicPlayer();
21+
TurnOnMusicCommand turnOnMusicCommand = new TurnOnMusicCommand(musicPlayer);
22+
TurnOffMusicCommand turnOffMusicCommand = new TurnOffMusicCommand(musicPlayer);
23+
24+
remoteControl.addCommand(0, turnOnLightCommand, turnOffLightCommand);
25+
remoteControl.addCommand(1, turnOnTVCommand, turnOffTVCommand);
26+
remoteControl.addCommand(2, turnOnMusicCommand, turnOffMusicCommand);
27+
28+
29+
System.out.println(remoteControl);
30+
31+
32+
System.out.println("------ Executing Commands ------");
33+
remoteControl.onButtonPressed(0);
34+
remoteControl.onButtonPressed(1);
35+
36+
remoteControl.offButtonPressed(0);
37+
remoteControl.offButtonPressed(1);
38+
39+
remoteControl.onButtonPressed(2);
40+
41+
System.out.println("------ Undoing Commands ------");
42+
43+
remoteControl.undoButtonPressed();
44+
remoteControl.undoButtonPressed();
45+
remoteControl.undoButtonPressed();
46+
remoteControl.undoButtonPressed();
47+
remoteControl.undoButtonPressed();
48+
remoteControl.undoButtonPressed();
49+
remoteControl.undoButtonPressed();
50+
51+
}
52+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package command;
2+
3+
/**
4+
* @author Khalid Elshafie <abolkog@gmail.com>
5+
* @created 14/03/2018.
6+
*/
7+
public interface Command {
8+
9+
void execute();
10+
11+
void undo();
12+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package command;
2+
3+
/**
4+
* @author Khalid Elshafie <abolkog@gmail.com>
5+
* @created 14/03/2018.
6+
*/
7+
8+
//Receiver
9+
public class Light {
10+
11+
public void turnOn() {
12+
System.out.println("Light is on");
13+
}
14+
15+
public void turnOff() {
16+
System.out.println("Light Is Off");
17+
}
18+
}

0 commit comments

Comments
 (0)