Skip to content

Commit eb39c53

Browse files
committed
Mediator design pattern - Traffic Light Project - Bonus Example
1 parent e1e2ff5 commit eb39c53

File tree

5 files changed

+141
-26
lines changed

5 files changed

+141
-26
lines changed

pattern/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

patternBonus/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.premaseem.lightSignal;
2+
3+
import java.util.Scanner;
4+
5+
public class Client {
6+
7+
public static void main(String[] args) {
8+
9+
System.out.println("Welcome to Traffic light signal program which uses Mediator ");
10+
System.out.println(" Out of 3 button only 1 could be ON and others has to in OFF state ");
11+
Scanner scan = new Scanner(System.in);
12+
13+
// Object initialization block
14+
LightMediator LightMediator = new LightMediator();
15+
Light red = new RedLight(LightMediator);
16+
Light yellow = new YellowLight(LightMediator);
17+
Light green = new GreenLight(LightMediator);
18+
19+
20+
// User input block
21+
String repeatRunFlag = "yes";
22+
while (!repeatRunFlag.equalsIgnoreCase("no")) {
23+
System.out.println("Which light you want to turn on ? ");
24+
System.out.println("press 1 for RED");
25+
System.out.println("press 2 for YELLOW ");
26+
System.out.println("press 2 for GREEN ");
27+
28+
int choice = scan.nextInt();
29+
if (choice == 1) {
30+
red.turnON();
31+
} else if(choice == 2) {
32+
yellow.turnON();
33+
} else{
34+
green.turnON();
35+
}
36+
37+
System.out.println("\n=============================");
38+
39+
System.out.println("Press No to Exit and any other character to repeat .... ");
40+
try {
41+
repeatRunFlag = scan.next();
42+
} catch (Exception e) {
43+
repeatRunFlag = "no";
44+
}
45+
46+
}
47+
48+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
49+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$$ www.premaseem.me $$$$$$$$$$$$$$$$$$$$$$ \n ");
50+
51+
}
52+
}
53+
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package com.premaseem.lightSignal;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
public abstract class Light {
10+
Light(LightMediator LightMediator){
11+
this.LightMediator = LightMediator;
12+
LightMediator.registerLight(this);
13+
}
14+
enum state {
15+
ON, OFF
16+
}
17+
18+
state currentState;
19+
LightMediator LightMediator;
20+
int id;
21+
22+
void turnON() {
23+
currentState = state.ON;
24+
System.out.printf("%s is turned %s \n", this.getClass().getSimpleName(), currentState.ON);
25+
LightMediator.turnON(this);
26+
}
27+
28+
void turnOFF() {
29+
currentState = state.OFF;
30+
System.out.printf("%s is turned %s \n", this.getClass().getSimpleName(), currentState.OFF);
31+
}
32+
33+
public void addId(int index) {
34+
id = index;
35+
}
36+
}
37+
38+
class RedLight extends Light {
39+
public RedLight(LightMediator LightMediator) {
40+
super(LightMediator);
41+
}
42+
}
43+
44+
class YellowLight extends Light {
45+
public YellowLight(LightMediator LightMediator) {
46+
super(LightMediator);
47+
}
48+
}
49+
50+
class GreenLight extends Light {
51+
public GreenLight(LightMediator LightMediator) {
52+
super(LightMediator);
53+
}
54+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.premaseem.lightSignal;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
public class LightMediator {
13+
14+
List<Light> list = new ArrayList<Light>();
15+
16+
public void registerLight(Light lite) {
17+
int index = list.size();
18+
lite.addId(index);
19+
list.add(index, lite);
20+
21+
}
22+
23+
void turnOffAllOtherLights(int ind){
24+
for (Light litt : list){
25+
if(!(litt.id==ind)){
26+
litt.turnOFF();
27+
}
28+
}
29+
}
30+
31+
public void turnON(Light light) {
32+
turnOffAllOtherLights(light.id);
33+
}
34+
}

0 commit comments

Comments
 (0)