Compare commits
7 Commits
ddc6253386
...
fd99c16e89
| Author | SHA1 | Date | |
|---|---|---|---|
|
fd99c16e89
|
|||
|
8f38c89d7c
|
|||
|
8cefde97d3
|
|||
|
876ce99c77
|
|||
|
6acdcaffa7
|
|||
|
061a475a6b
|
|||
|
312a5276d7
|
13
src/lab5_adapter/ex1/Engineer.java
Normal file
13
src/lab5_adapter/ex1/Engineer.java
Normal file
@@ -0,0 +1,13 @@
|
|||||||
|
package lab5_adapter.ex1;
|
||||||
|
|
||||||
|
public class Engineer {
|
||||||
|
private String name;
|
||||||
|
public Engineer(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Engineer " + name;
|
||||||
|
}
|
||||||
|
}
|
||||||
10
src/lab5_adapter/ex1/Machine.java
Normal file
10
src/lab5_adapter/ex1/Machine.java
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
package lab5_adapter.ex1;
|
||||||
|
|
||||||
|
public interface Machine {
|
||||||
|
int getNumberOfCapsules();
|
||||||
|
boolean isCompletelyUp();
|
||||||
|
void stop();
|
||||||
|
|
||||||
|
void setNumberOfCapsules(int capsules);
|
||||||
|
void setCompletelyUp(boolean completelyUp);
|
||||||
|
}
|
||||||
44
src/lab5_adapter/ex1/MachineAdapter.java
Normal file
44
src/lab5_adapter/ex1/MachineAdapter.java
Normal file
@@ -0,0 +1,44 @@
|
|||||||
|
package lab5_adapter.ex1;
|
||||||
|
|
||||||
|
public class MachineAdapter implements NewMachineInterface {
|
||||||
|
private OldMachine machine;
|
||||||
|
|
||||||
|
public MachineAdapter(OldMachine machine) {
|
||||||
|
this.machine = machine;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNumberOfCapsules() {
|
||||||
|
return machine.getNumberOfCapsules();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCompletelyUp() {
|
||||||
|
return machine.isCompletelyUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
machine.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setNumberOfCapsules(int capsules) {
|
||||||
|
machine.setNumberOfCapsules(capsules);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCompletelyUp(boolean completelyUp) {
|
||||||
|
machine.setCompletelyUp(completelyUp);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Engineer getEngineer() {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return machine.toString() + ", supervised by engineer " + getEngineer();
|
||||||
|
}
|
||||||
|
}
|
||||||
27
src/lab5_adapter/ex1/Main.java
Normal file
27
src/lab5_adapter/ex1/Main.java
Normal file
@@ -0,0 +1,27 @@
|
|||||||
|
package lab5_adapter.ex1;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Engineer engineer1 = new Engineer("Jean");
|
||||||
|
Engineer engineer2 = new Engineer("Maria");
|
||||||
|
Machine machine1 = new NewMachine("1159190947", engineer1);
|
||||||
|
Machine machine2 = new NewMachine("1247233941", engineer1);
|
||||||
|
Machine machine3 = new NewMachine("258952499", engineer2);
|
||||||
|
OldMachine oldMachine = new OldMachine("603742814");
|
||||||
|
Machine machine4 = new MachineAdapter(oldMachine);
|
||||||
|
|
||||||
|
machine1.setNumberOfCapsules(30);
|
||||||
|
machine2.setNumberOfCapsules(20);
|
||||||
|
machine3.setNumberOfCapsules(40);
|
||||||
|
oldMachine.setNumberOfCapsules(80);
|
||||||
|
//Machine 1159190947 is currently processing 30 capsules, supervised by engineer Engineer Jean
|
||||||
|
//Machine 1247233941 is currently processing 20 capsules, supervised by engineer Engineer Jean
|
||||||
|
//Machine 258952499 is currently processing 40 capsules, supervised by engineer Engineer Maria
|
||||||
|
//Machine 603742814 is currently processing 80 capsules, supervised by engineer null
|
||||||
|
|
||||||
|
System.out.println(machine1);
|
||||||
|
System.out.println(machine2);
|
||||||
|
System.out.println(machine3);
|
||||||
|
System.out.println(machine4);
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/lab5_adapter/ex1/NewMachine.java
Normal file
19
src/lab5_adapter/ex1/NewMachine.java
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
package lab5_adapter.ex1;
|
||||||
|
|
||||||
|
public class NewMachine extends OldMachine implements NewMachineInterface {
|
||||||
|
private Engineer engineer;
|
||||||
|
|
||||||
|
public NewMachine(String name, Engineer engineer) {
|
||||||
|
super(name);
|
||||||
|
this.engineer = engineer;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Engineer getEngineer() {
|
||||||
|
return engineer;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return super.toString() + ", supervised by engineer " + getEngineer();
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/lab5_adapter/ex1/NewMachineInterface.java
Normal file
5
src/lab5_adapter/ex1/NewMachineInterface.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package lab5_adapter.ex1;
|
||||||
|
|
||||||
|
public interface NewMachineInterface extends Machine {
|
||||||
|
Engineer getEngineer();
|
||||||
|
}
|
||||||
39
src/lab5_adapter/ex1/OldMachine.java
Normal file
39
src/lab5_adapter/ex1/OldMachine.java
Normal file
@@ -0,0 +1,39 @@
|
|||||||
|
package lab5_adapter.ex1;
|
||||||
|
|
||||||
|
public class OldMachine implements Machine {
|
||||||
|
private String name;
|
||||||
|
private int capsules = 0;
|
||||||
|
private boolean completelyUp = false;
|
||||||
|
|
||||||
|
public OldMachine(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setNumberOfCapsules(int capsules) {
|
||||||
|
this.capsules = capsules;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCompletelyUp(boolean completelyUp) {
|
||||||
|
this.completelyUp = completelyUp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getNumberOfCapsules() {
|
||||||
|
return capsules;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCompletelyUp() {
|
||||||
|
return completelyUp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stop() {
|
||||||
|
System.out.println("Stopping machine " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Machine " + name + " is currently processing " + getNumberOfCapsules() + " capsules";
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/lab6_template_method/ex1/HellsKitchen.java
Normal file
32
src/lab6_template_method/ex1/HellsKitchen.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package lab6_template_method.ex1;
|
||||||
|
|
||||||
|
public class HellsKitchen extends Restaurant {
|
||||||
|
public HellsKitchen(String name) {
|
||||||
|
super("HK " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareVegetables() {
|
||||||
|
log("Preparing nice salad");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareMainMenu() {
|
||||||
|
log("Creating flavorful dish");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareCheese() {
|
||||||
|
log("Preparing finest cheese");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareDessert() {
|
||||||
|
log("Giving birth to awesome dessert");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareCoffee() {
|
||||||
|
log("Brewing the best coffee");
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/lab6_template_method/ex1/MacDonalds.java
Normal file
32
src/lab6_template_method/ex1/MacDonalds.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package lab6_template_method.ex1;
|
||||||
|
|
||||||
|
public class MacDonalds extends Restaurant {
|
||||||
|
public MacDonalds(String name) {
|
||||||
|
super("McDonalds " + name);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareVegetables() {
|
||||||
|
log("Vegetables ? What's that ?");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareMainMenu() {
|
||||||
|
log("McChicken incoming");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareCheese() {
|
||||||
|
log("Hmmm, creamy cheese");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareDessert() {
|
||||||
|
log("Preparing McFlurry");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void prepareCoffee() {
|
||||||
|
log("Preparing short and hot coffee");
|
||||||
|
}
|
||||||
|
}
|
||||||
11
src/lab6_template_method/ex1/Main.java
Normal file
11
src/lab6_template_method/ex1/Main.java
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
package lab6_template_method.ex1;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Restaurant r1 = new MacDonalds("Sion");
|
||||||
|
Restaurant r2 = new HellsKitchen("London");
|
||||||
|
|
||||||
|
r1.prepareDinner();
|
||||||
|
r2.prepareDinner();
|
||||||
|
}
|
||||||
|
}
|
||||||
36
src/lab6_template_method/ex1/Restaurant.java
Normal file
36
src/lab6_template_method/ex1/Restaurant.java
Normal file
@@ -0,0 +1,36 @@
|
|||||||
|
package lab6_template_method.ex1;
|
||||||
|
|
||||||
|
public abstract class Restaurant {
|
||||||
|
private String name;
|
||||||
|
public Restaurant(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public final void prepareDinner() {
|
||||||
|
serveWater();
|
||||||
|
prepareVegetables();
|
||||||
|
prepareMainMenu();
|
||||||
|
prepareCheese();
|
||||||
|
prepareDessert();
|
||||||
|
prepareCoffee();
|
||||||
|
cleanTable();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void log(String msg) {
|
||||||
|
System.out.println("[" + name + "] " + msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final void serveWater() {
|
||||||
|
log("Serving water");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected final void cleanTable() {
|
||||||
|
log("Cleaning table");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected abstract void prepareVegetables();
|
||||||
|
protected abstract void prepareMainMenu();
|
||||||
|
protected abstract void prepareCheese();
|
||||||
|
protected abstract void prepareDessert();
|
||||||
|
protected abstract void prepareCoffee();
|
||||||
|
}
|
||||||
101
src/lab7_state/ex1/Machine.java
Normal file
101
src/lab7_state/ex1/Machine.java
Normal file
@@ -0,0 +1,101 @@
|
|||||||
|
package lab7_state.ex1;
|
||||||
|
|
||||||
|
import lab7_state.ex1.states.*;
|
||||||
|
|
||||||
|
public class Machine {
|
||||||
|
private MachineState currentState;
|
||||||
|
private final MachineState offState;
|
||||||
|
private final MachineState idleState;
|
||||||
|
private final MachineState choiceState;
|
||||||
|
private final MachineState serviceNeededState;
|
||||||
|
private final MachineState coffeeReadyState;
|
||||||
|
|
||||||
|
private int cups;
|
||||||
|
private boolean jammed = false;
|
||||||
|
|
||||||
|
public Machine() {
|
||||||
|
offState = new OffState(this);
|
||||||
|
idleState = new IdleState(this);
|
||||||
|
choiceState = new ChoiceState(this);
|
||||||
|
serviceNeededState = new ServiceNeededState(this);
|
||||||
|
coffeeReadyState = new CoffeeReadyState(this);
|
||||||
|
currentState = offState;
|
||||||
|
this.setCups(3);
|
||||||
|
}
|
||||||
|
|
||||||
|
public MachineState getOffState() {
|
||||||
|
return offState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MachineState getIdleState() {
|
||||||
|
return idleState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MachineState getChoiceState() {
|
||||||
|
return choiceState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MachineState getServiceNeededState() {
|
||||||
|
return serviceNeededState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public MachineState getCoffeeReadyState() {
|
||||||
|
return coffeeReadyState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void returnMoney(double value) {
|
||||||
|
System.out.println("Returning CHF " + value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentState(MachineState currentState) {
|
||||||
|
System.out.println(this.currentState.toString() + " -> " + currentState);
|
||||||
|
this.currentState = currentState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void powerUp() {
|
||||||
|
currentState.powerUp();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void insertCoin(double value) {
|
||||||
|
currentState.insertCoin(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void returnCoin() {
|
||||||
|
currentState.returnCoin();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void pushButton() {
|
||||||
|
currentState.pushButton();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void resetButton() {
|
||||||
|
currentState.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void removeCup() {
|
||||||
|
currentState.removeCup();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCups(int cups) {
|
||||||
|
this.cups = cups;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasCups() {
|
||||||
|
return cups != 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean hasCoffee() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isJammed() {
|
||||||
|
return jammed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void makeCoffee() {
|
||||||
|
cups--;
|
||||||
|
if (Math.random() < 0.2) {
|
||||||
|
jammed = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
21
src/lab7_state/ex1/MachineState.java
Normal file
21
src/lab7_state/ex1/MachineState.java
Normal file
@@ -0,0 +1,21 @@
|
|||||||
|
package lab7_state.ex1;
|
||||||
|
|
||||||
|
public abstract class MachineState {
|
||||||
|
protected Machine machine;
|
||||||
|
|
||||||
|
public MachineState(Machine machine) {
|
||||||
|
this.machine = machine;
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void powerUp() {}
|
||||||
|
protected void insertCoin(double value) {}
|
||||||
|
protected void returnCoin() {}
|
||||||
|
protected void reset() {}
|
||||||
|
protected void pushButton() {}
|
||||||
|
protected void removeCup() {}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return getClass().getSimpleName();
|
||||||
|
}
|
||||||
|
}
|
||||||
29
src/lab7_state/ex1/Main.java
Normal file
29
src/lab7_state/ex1/Main.java
Normal file
@@ -0,0 +1,29 @@
|
|||||||
|
package lab7_state.ex1;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Machine machine = new Machine();
|
||||||
|
System.out.println("A");
|
||||||
|
machine.powerUp();
|
||||||
|
machine.insertCoin(1);
|
||||||
|
|
||||||
|
System.out.println("B");
|
||||||
|
machine.insertCoin(0.25);
|
||||||
|
machine.returnCoin();
|
||||||
|
|
||||||
|
System.out.println("C");
|
||||||
|
machine.insertCoin(0.25);
|
||||||
|
machine.pushButton();
|
||||||
|
machine.removeCup();
|
||||||
|
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
System.out.println("D" + i);
|
||||||
|
machine.insertCoin(0.25);
|
||||||
|
machine.pushButton();
|
||||||
|
machine.removeCup();
|
||||||
|
}
|
||||||
|
|
||||||
|
System.out.println("E");
|
||||||
|
machine.resetButton();
|
||||||
|
}
|
||||||
|
}
|
||||||
32
src/lab7_state/ex1/states/ChoiceState.java
Normal file
32
src/lab7_state/ex1/states/ChoiceState.java
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
package lab7_state.ex1.states;
|
||||||
|
|
||||||
|
import lab7_state.ex1.Machine;
|
||||||
|
import lab7_state.ex1.MachineState;
|
||||||
|
|
||||||
|
public class ChoiceState extends MachineState {
|
||||||
|
public ChoiceState(Machine machine) {
|
||||||
|
super(machine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void returnCoin() {
|
||||||
|
machine.returnMoney(0.25);
|
||||||
|
machine.setCurrentState(machine.getIdleState());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void pushButton() {
|
||||||
|
if (!machine.hasCups()) {
|
||||||
|
System.out.println("The machine is out of cups");
|
||||||
|
} else if (!machine.hasCoffee()) {
|
||||||
|
System.out.println("The machine is out of coffee beans");
|
||||||
|
} else if (machine.isJammed()) {
|
||||||
|
System.out.println("The machine is jammed");
|
||||||
|
} else {
|
||||||
|
machine.makeCoffee();
|
||||||
|
machine.setCurrentState(machine.getCoffeeReadyState());
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
machine.setCurrentState(machine.getServiceNeededState());
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/lab7_state/ex1/states/CoffeeReadyState.java
Normal file
15
src/lab7_state/ex1/states/CoffeeReadyState.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package lab7_state.ex1.states;
|
||||||
|
|
||||||
|
import lab7_state.ex1.Machine;
|
||||||
|
import lab7_state.ex1.MachineState;
|
||||||
|
|
||||||
|
public class CoffeeReadyState extends MachineState {
|
||||||
|
public CoffeeReadyState(Machine machine) {
|
||||||
|
super(machine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void removeCup() {
|
||||||
|
machine.setCurrentState(machine.getIdleState());
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/lab7_state/ex1/states/IdleState.java
Normal file
20
src/lab7_state/ex1/states/IdleState.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package lab7_state.ex1.states;
|
||||||
|
|
||||||
|
import lab7_state.ex1.Machine;
|
||||||
|
import lab7_state.ex1.MachineState;
|
||||||
|
|
||||||
|
public class IdleState extends MachineState {
|
||||||
|
public IdleState(Machine machine) {
|
||||||
|
super(machine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void insertCoin(double value) {
|
||||||
|
if (value != 0.25) {
|
||||||
|
machine.returnMoney(value);
|
||||||
|
} else {
|
||||||
|
System.out.println("Please choose your coffee");
|
||||||
|
machine.setCurrentState(machine.getChoiceState());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/lab7_state/ex1/states/OffState.java
Normal file
15
src/lab7_state/ex1/states/OffState.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package lab7_state.ex1.states;
|
||||||
|
|
||||||
|
import lab7_state.ex1.Machine;
|
||||||
|
import lab7_state.ex1.MachineState;
|
||||||
|
|
||||||
|
public class OffState extends MachineState {
|
||||||
|
public OffState(Machine machine) {
|
||||||
|
super(machine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void powerUp() {
|
||||||
|
machine.setCurrentState(machine.getIdleState());
|
||||||
|
}
|
||||||
|
}
|
||||||
15
src/lab7_state/ex1/states/ServiceNeededState.java
Normal file
15
src/lab7_state/ex1/states/ServiceNeededState.java
Normal file
@@ -0,0 +1,15 @@
|
|||||||
|
package lab7_state.ex1.states;
|
||||||
|
|
||||||
|
import lab7_state.ex1.Machine;
|
||||||
|
import lab7_state.ex1.MachineState;
|
||||||
|
|
||||||
|
public class ServiceNeededState extends MachineState {
|
||||||
|
public ServiceNeededState(Machine machine) {
|
||||||
|
super(machine);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void reset() {
|
||||||
|
machine.setCurrentState(machine.getIdleState());
|
||||||
|
}
|
||||||
|
}
|
||||||
25
src/lab7_state/ex2/Main.java
Normal file
25
src/lab7_state/ex2/Main.java
Normal file
@@ -0,0 +1,25 @@
|
|||||||
|
package lab7_state.ex2;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
User user = new User("Baryhobal");
|
||||||
|
user.connect();
|
||||||
|
user.enter();
|
||||||
|
user.leave();
|
||||||
|
|
||||||
|
user.enter();
|
||||||
|
user.ask();
|
||||||
|
user.leave();
|
||||||
|
|
||||||
|
user.enter();
|
||||||
|
user.ask();
|
||||||
|
user.handOver();
|
||||||
|
user.leave();
|
||||||
|
|
||||||
|
user.enter();
|
||||||
|
user.ask();
|
||||||
|
user.handOver();
|
||||||
|
user.over();
|
||||||
|
user.leave();
|
||||||
|
}
|
||||||
|
}
|
||||||
71
src/lab7_state/ex2/User.java
Normal file
71
src/lab7_state/ex2/User.java
Normal file
@@ -0,0 +1,71 @@
|
|||||||
|
package lab7_state.ex2;
|
||||||
|
|
||||||
|
import lab7_state.ex2.states.*;
|
||||||
|
|
||||||
|
public class User {
|
||||||
|
private String name;
|
||||||
|
private UserState currentState;
|
||||||
|
private UserState unregisteredState;
|
||||||
|
private UserState registeredState;
|
||||||
|
private UserState inMeetingState;
|
||||||
|
private UserState waitingToSpeakState;
|
||||||
|
private UserState speakingState;
|
||||||
|
|
||||||
|
public User(String name) {
|
||||||
|
this.name = name;
|
||||||
|
unregisteredState = new UnregisteredState(this);
|
||||||
|
registeredState = new RegisteredState(this);
|
||||||
|
inMeetingState = new InMeetingState(this);
|
||||||
|
waitingToSpeakState = new WaitingToSpeakState(this);
|
||||||
|
speakingState = new SpeakingState(this);
|
||||||
|
currentState = unregisteredState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentState(UserState currentState) {
|
||||||
|
this.currentState = currentState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserState getUnregisteredState() {
|
||||||
|
return unregisteredState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserState getRegisteredState() {
|
||||||
|
return registeredState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserState getInMeetingState() {
|
||||||
|
return inMeetingState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserState getWaitingToSpeakState() {
|
||||||
|
return waitingToSpeakState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public UserState getSpeakingState() {
|
||||||
|
return speakingState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect() {
|
||||||
|
currentState.connect();
|
||||||
|
}
|
||||||
|
public void enter() {
|
||||||
|
currentState.enter();
|
||||||
|
}
|
||||||
|
public void leave() {
|
||||||
|
currentState.leave();
|
||||||
|
}
|
||||||
|
public void ask() {
|
||||||
|
currentState.ask();
|
||||||
|
}
|
||||||
|
public void handOver() {
|
||||||
|
currentState.handOver();
|
||||||
|
}
|
||||||
|
public void over() {
|
||||||
|
currentState.over();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "User " + name;
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/lab7_state/ex2/UserState.java
Normal file
18
src/lab7_state/ex2/UserState.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package lab7_state.ex2;
|
||||||
|
|
||||||
|
public abstract class UserState {
|
||||||
|
protected User user;
|
||||||
|
public UserState(User user) {
|
||||||
|
this.user = user;
|
||||||
|
}
|
||||||
|
protected void log(String msg) {
|
||||||
|
System.out.println("[" + user + "] " + msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void connect() {};
|
||||||
|
public void enter() {};
|
||||||
|
public void leave() {};
|
||||||
|
public void ask() {};
|
||||||
|
public void handOver() {};
|
||||||
|
public void over() {};
|
||||||
|
}
|
||||||
22
src/lab7_state/ex2/states/InMeetingState.java
Normal file
22
src/lab7_state/ex2/states/InMeetingState.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package lab7_state.ex2.states;
|
||||||
|
|
||||||
|
import lab7_state.ex2.User;
|
||||||
|
import lab7_state.ex2.UserState;
|
||||||
|
|
||||||
|
public class InMeetingState extends UserState {
|
||||||
|
public InMeetingState(User user) {
|
||||||
|
super(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void leave() {
|
||||||
|
user.setCurrentState(user.getRegisteredState());
|
||||||
|
log("Left meeting");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void ask() {
|
||||||
|
user.setCurrentState(user.getWaitingToSpeakState());
|
||||||
|
log("Asking to speak");
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/lab7_state/ex2/states/RegisteredState.java
Normal file
16
src/lab7_state/ex2/states/RegisteredState.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package lab7_state.ex2.states;
|
||||||
|
|
||||||
|
import lab7_state.ex2.User;
|
||||||
|
import lab7_state.ex2.UserState;
|
||||||
|
|
||||||
|
public class RegisteredState extends UserState {
|
||||||
|
public RegisteredState(User user) {
|
||||||
|
super(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void enter() {
|
||||||
|
user.setCurrentState(user.getInMeetingState());
|
||||||
|
log("Entered meeting");
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/lab7_state/ex2/states/SpeakingState.java
Normal file
22
src/lab7_state/ex2/states/SpeakingState.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package lab7_state.ex2.states;
|
||||||
|
|
||||||
|
import lab7_state.ex2.User;
|
||||||
|
import lab7_state.ex2.UserState;
|
||||||
|
|
||||||
|
public class SpeakingState extends UserState {
|
||||||
|
public SpeakingState(User user) {
|
||||||
|
super(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void over() {
|
||||||
|
user.setCurrentState(user.getInMeetingState());
|
||||||
|
log("Finished speaking");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void leave() {
|
||||||
|
user.setCurrentState(user.getRegisteredState());
|
||||||
|
log("Finished speaking, left meeting");
|
||||||
|
}
|
||||||
|
}
|
||||||
16
src/lab7_state/ex2/states/UnregisteredState.java
Normal file
16
src/lab7_state/ex2/states/UnregisteredState.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package lab7_state.ex2.states;
|
||||||
|
|
||||||
|
import lab7_state.ex2.User;
|
||||||
|
import lab7_state.ex2.UserState;
|
||||||
|
|
||||||
|
public class UnregisteredState extends UserState {
|
||||||
|
public UnregisteredState(User user) {
|
||||||
|
super(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void connect() {
|
||||||
|
user.setCurrentState(user.getRegisteredState());
|
||||||
|
log("Registered");
|
||||||
|
}
|
||||||
|
}
|
||||||
22
src/lab7_state/ex2/states/WaitingToSpeakState.java
Normal file
22
src/lab7_state/ex2/states/WaitingToSpeakState.java
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package lab7_state.ex2.states;
|
||||||
|
|
||||||
|
import lab7_state.ex2.User;
|
||||||
|
import lab7_state.ex2.UserState;
|
||||||
|
|
||||||
|
public class WaitingToSpeakState extends UserState {
|
||||||
|
public WaitingToSpeakState(User user) {
|
||||||
|
super(user);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void leave() {
|
||||||
|
user.setCurrentState(user.getRegisteredState());
|
||||||
|
log("Lost patience, left meeting");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void handOver() {
|
||||||
|
user.setCurrentState(user.getSpeakingState());
|
||||||
|
log("Starts speaking");
|
||||||
|
}
|
||||||
|
}
|
||||||
7
src/learn/simple_adapter/Adaptee.java
Normal file
7
src/learn/simple_adapter/Adaptee.java
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
package learn.simple_adapter;
|
||||||
|
|
||||||
|
public class Adaptee {
|
||||||
|
public void specificRequest() {
|
||||||
|
System.out.println("Specific Request");
|
||||||
|
}
|
||||||
|
}
|
||||||
14
src/learn/simple_adapter/Adapter.java
Normal file
14
src/learn/simple_adapter/Adapter.java
Normal file
@@ -0,0 +1,14 @@
|
|||||||
|
package learn.simple_adapter;
|
||||||
|
|
||||||
|
public class Adapter implements Target {
|
||||||
|
private Adaptee adaptee;
|
||||||
|
|
||||||
|
public Adapter(Adaptee adaptee) {
|
||||||
|
this.adaptee = adaptee;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void request() {
|
||||||
|
adaptee.specificRequest();
|
||||||
|
}
|
||||||
|
}
|
||||||
9
src/learn/simple_adapter/Main.java
Normal file
9
src/learn/simple_adapter/Main.java
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
package learn.simple_adapter;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Adaptee adaptee = new Adaptee();
|
||||||
|
Target adapter = new Adapter(adaptee);
|
||||||
|
adapter.request();
|
||||||
|
}
|
||||||
|
}
|
||||||
5
src/learn/simple_adapter/Target.java
Normal file
5
src/learn/simple_adapter/Target.java
Normal file
@@ -0,0 +1,5 @@
|
|||||||
|
package learn.simple_adapter;
|
||||||
|
|
||||||
|
public interface Target {
|
||||||
|
void request();
|
||||||
|
}
|
||||||
16
src/learn/simple_builder/Builder.java
Normal file
16
src/learn/simple_builder/Builder.java
Normal file
@@ -0,0 +1,16 @@
|
|||||||
|
package learn.simple_builder;
|
||||||
|
|
||||||
|
public abstract class Builder {
|
||||||
|
protected Product product;
|
||||||
|
|
||||||
|
public void setProduct(Product product) {
|
||||||
|
this.product = product;
|
||||||
|
}
|
||||||
|
public Product getProduct() {
|
||||||
|
return product;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract void buildPartA();
|
||||||
|
public abstract void buildPartB();
|
||||||
|
public abstract void buildPartC();
|
||||||
|
}
|
||||||
18
src/learn/simple_builder/ConcreteBuilder.java
Normal file
18
src/learn/simple_builder/ConcreteBuilder.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package learn.simple_builder;
|
||||||
|
|
||||||
|
public class ConcreteBuilder extends Builder {
|
||||||
|
@Override
|
||||||
|
public void buildPartA() {
|
||||||
|
product.setPartA("material 1");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void buildPartB() {
|
||||||
|
product.setPartB("material 2");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void buildPartC() {
|
||||||
|
product.setPartC("material 3");
|
||||||
|
}
|
||||||
|
}
|
||||||
18
src/learn/simple_builder/Director.java
Normal file
18
src/learn/simple_builder/Director.java
Normal file
@@ -0,0 +1,18 @@
|
|||||||
|
package learn.simple_builder;
|
||||||
|
|
||||||
|
public class Director {
|
||||||
|
private Builder builder;
|
||||||
|
private Product product;
|
||||||
|
|
||||||
|
public void setBuilder(Builder builder) {
|
||||||
|
this.builder = builder;
|
||||||
|
product = new Product();
|
||||||
|
this.builder.setProduct(product);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void construct() {
|
||||||
|
builder.buildPartA();
|
||||||
|
builder.buildPartB();
|
||||||
|
builder.buildPartC();
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/learn/simple_builder/Main.java
Normal file
12
src/learn/simple_builder/Main.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package learn.simple_builder;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Builder builder = new ConcreteBuilder();
|
||||||
|
Director director = new Director();
|
||||||
|
director.setBuilder(builder);
|
||||||
|
director.construct();
|
||||||
|
Product finalProduct = builder.getProduct();
|
||||||
|
System.out.println(finalProduct);
|
||||||
|
}
|
||||||
|
}
|
||||||
28
src/learn/simple_builder/Product.java
Normal file
28
src/learn/simple_builder/Product.java
Normal file
@@ -0,0 +1,28 @@
|
|||||||
|
package learn.simple_builder;
|
||||||
|
|
||||||
|
public class Product {
|
||||||
|
private String partA;
|
||||||
|
private String partB;
|
||||||
|
private String partC;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return "Product{" +
|
||||||
|
"partA='" + partA + '\'' +
|
||||||
|
", partB='" + partB + '\'' +
|
||||||
|
", partC='" + partC + '\'' +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPartA(String partA) {
|
||||||
|
this.partA = partA;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPartB(String partB) {
|
||||||
|
this.partB = partB;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setPartC(String partC) {
|
||||||
|
this.partC = partC;
|
||||||
|
}
|
||||||
|
}
|
||||||
34
src/learn/simple_state/Context.java
Normal file
34
src/learn/simple_state/Context.java
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public class Context {
|
||||||
|
private State onState;
|
||||||
|
private State offState;
|
||||||
|
|
||||||
|
private State currentState;
|
||||||
|
|
||||||
|
public Context() {
|
||||||
|
onState = new OnState(this);
|
||||||
|
offState = new OffState(this);
|
||||||
|
currentState = offState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCurrentState(State currentState) {
|
||||||
|
this.currentState = currentState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getOffState() {
|
||||||
|
return offState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public State getOnState() {
|
||||||
|
return onState;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void on() {
|
||||||
|
currentState.on();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void off() {
|
||||||
|
currentState.off();
|
||||||
|
}
|
||||||
|
}
|
||||||
12
src/learn/simple_state/Main.java
Normal file
12
src/learn/simple_state/Main.java
Normal file
@@ -0,0 +1,12 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public class Main {
|
||||||
|
public static void main(String[] args) {
|
||||||
|
Context context = new Context();
|
||||||
|
context.off();
|
||||||
|
context.off();
|
||||||
|
context.on();
|
||||||
|
context.on();
|
||||||
|
context.off();
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/learn/simple_state/OffState.java
Normal file
20
src/learn/simple_state/OffState.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public class OffState implements State {
|
||||||
|
private Context context;
|
||||||
|
|
||||||
|
public OffState(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void on() {
|
||||||
|
context.setCurrentState(context.getOnState());
|
||||||
|
System.out.println("Transition to on");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void off() {
|
||||||
|
System.out.println("Already off");
|
||||||
|
}
|
||||||
|
}
|
||||||
20
src/learn/simple_state/OnState.java
Normal file
20
src/learn/simple_state/OnState.java
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public class OnState implements State {
|
||||||
|
private Context context;
|
||||||
|
|
||||||
|
public OnState(Context context) {
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void on() {
|
||||||
|
System.out.println("Already on");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void off() {
|
||||||
|
context.setCurrentState(context.getOffState());
|
||||||
|
System.out.println("Transition to off");
|
||||||
|
}
|
||||||
|
}
|
||||||
6
src/learn/simple_state/State.java
Normal file
6
src/learn/simple_state/State.java
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
package learn.simple_state;
|
||||||
|
|
||||||
|
public interface State {
|
||||||
|
void on();
|
||||||
|
void off();
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user