├── .classpath
├── .gitignore
├── .project
├── README.md
└── src
└── fr
└── iutvalence
└── info
└── dut
└── m3105
└── preamble
├── Main.java
├── TrafficSignal.java
└── TrafficSignalState.java
/.classpath:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | /bin/
2 |
--------------------------------------------------------------------------------
/.project:
--------------------------------------------------------------------------------
1 |
2 |
3 | TrafficSignal-Java-Design-Pattern-Behavioral
4 |
5 |
6 |
7 |
8 |
9 | org.eclipse.jdt.core.javabuilder
10 |
11 |
12 |
13 |
14 |
15 | org.eclipse.jdt.core.javanature
16 |
17 |
18 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # TrafficSignal-Java-Design-Pattern-Behavioral
2 | Behavioral Design Pattern training exercice
3 |
4 | ## Preamble
5 |
6 | The given source code tree simulate a (GREEN->ORANGE->RED) traffic signal including call button.
7 | States are represented as an enumeration, the traffic signal behavior being implemented in the `TrafficSignal` class.
8 |
9 | ## STATE pattern
10 |
11 | Refactor this previous application using the STATE pattern so that the traffic signal behavior gets distributed among individual states.
12 |
13 | ## OBSERVER pattern
14 |
15 | Refactor the previous application (including STATE pattern) using the OBSERVER pattern so that:
16 |
17 | - traffic signal updates (color changed or button pressed) are notified to a separate object (instance of `TrafficSignalConsoleDisplay` class)
18 | - button pressed events are simulated by a separate thread and notified to the traffic signal object
19 |
20 | *Proper interfaces have to be defined for observers. It may also be considered that each observed object could have more than a single observer.*
21 |
22 | Once done, rewrite the application using Observer/Observable interfaces as defined in Java standard library.
23 |
24 |
25 |
--------------------------------------------------------------------------------
/src/fr/iutvalence/info/dut/m3105/preamble/Main.java:
--------------------------------------------------------------------------------
1 | package fr.iutvalence.info.dut.m3105.preamble;
2 |
3 | import java.util.Random;
4 |
5 | public class Main
6 | {
7 |
8 | public static void main(String[] args)
9 | {
10 | Random rng = new Random();
11 |
12 | TrafficSignal trafficSignal = new TrafficSignal();
13 | trafficSignal.start();
14 |
15 | while (true)
16 | {
17 | int delay = rng.nextInt(10);
18 | try
19 | {
20 | Thread.sleep(delay*1000);
21 | }
22 | catch (InterruptedException e)
23 | {
24 | }
25 | trafficSignal.pressButton();
26 | }
27 | }
28 |
29 | }
30 |
--------------------------------------------------------------------------------
/src/fr/iutvalence/info/dut/m3105/preamble/TrafficSignal.java:
--------------------------------------------------------------------------------
1 | package fr.iutvalence.info.dut.m3105.preamble;
2 |
3 | public class TrafficSignal extends Thread
4 | {
5 | private final static int BUTTON_THRESHOLD_IN_SECONDS = 2;
6 |
7 | private TrafficSignalState state;
8 | private int stateSecondsRemaining;
9 |
10 | public TrafficSignal()
11 | {
12 | super();
13 | this.switchToState(TrafficSignalState.GREEN);
14 | }
15 |
16 | public void pressButton()
17 | {
18 | System.out.println("Button pressed!");
19 | System.out.flush();
20 | switch (this.state)
21 | {
22 | case ORANGE:
23 | case RED: return;
24 | case GREEN:
25 | {
26 | if (this.stateSecondsRemaining > BUTTON_THRESHOLD_IN_SECONDS)
27 | this.stateSecondsRemaining = BUTTON_THRESHOLD_IN_SECONDS;
28 | }
29 | }
30 | }
31 |
32 | public void run()
33 | {
34 | while (true)
35 | {
36 | try
37 | {
38 | Thread.sleep(1000);
39 | this.secondEllapsed();
40 | }
41 | catch (InterruptedException e)
42 | {
43 | break;
44 | }
45 | }
46 | }
47 |
48 | private void secondEllapsed()
49 | {
50 | this.stateSecondsRemaining--;
51 | System.out.println(this.stateSecondsRemaining);
52 | System.out.flush();
53 | if (this.stateSecondsRemaining == 0)
54 | {
55 | switch(this.state)
56 | {
57 | case GREEN:
58 | this.switchToState(TrafficSignalState.ORANGE);
59 | break;
60 | case ORANGE:
61 | this.switchToState(TrafficSignalState.RED);
62 | break;
63 | case RED:
64 | this.switchToState(TrafficSignalState.GREEN);
65 | break;
66 | }
67 | }
68 | }
69 |
70 | private void switchToState(TrafficSignalState state)
71 | {
72 | System.out.println("Traffic signal turns "+state);
73 | System.out.flush();
74 | this.state = state;
75 | this.stateSecondsRemaining = this.state.getDurationInSeconds();
76 | }
77 | }
78 |
--------------------------------------------------------------------------------
/src/fr/iutvalence/info/dut/m3105/preamble/TrafficSignalState.java:
--------------------------------------------------------------------------------
1 | package fr.iutvalence.info.dut.m3105.preamble;
2 |
3 | public enum TrafficSignalState
4 | {
5 | GREEN(10), ORANGE(2), RED(10);
6 |
7 | private final int durationInSeconds;
8 |
9 | private TrafficSignalState(int durationInSeconds)
10 | {
11 | this.durationInSeconds = durationInSeconds;
12 | }
13 |
14 | public int getDurationInSeconds()
15 | {
16 | return this.durationInSeconds;
17 | }
18 | }
19 |
--------------------------------------------------------------------------------