Compare commits

..

10 Commits

Author SHA1 Message Date
fa5b8bf520
added lab15 ex2 2024-11-11 13:26:18 +01:00
b964385405
added lab15 ex1 2024-11-11 11:34:23 +01:00
ca9d0ec062
added observer example 2024-11-11 11:16:34 +01:00
6eee9df1fe
added lab14 ex2 2024-11-11 10:56:52 +01:00
23829388cb
added lab14 ex1 2024-11-11 10:44:58 +01:00
64f7f56ffe
added lab13 ex2 2024-11-11 10:33:59 +01:00
51f5d352c6
added strategy example 2024-11-11 10:28:35 +01:00
7c614b0c5c
added lab13 ex1 2024-11-11 09:41:57 +01:00
3743b47887
added proxy example 2024-11-11 09:24:37 +01:00
94df35881e
added lab12 ex1 2024-11-11 09:05:04 +01:00
60 changed files with 1101 additions and 0 deletions

View File

@ -0,0 +1,29 @@
package lab12_singleton.ex1;
import lab12_singleton.ex1.sensors.*;
import lab12_singleton.ex1.managers.*;
public class CarSensorSingletonLauncher {
public CarSensorSingletonLauncher() {
}
public static void main(String[] args) {
CarSensorSingletonLauncher launcher = new CarSensorSingletonLauncher();
launcher.launch();
}
public void launch() {
this.test(SimpleSingletonSensorsManager.getInstance());
this.test(SynchSingletonSensorsManager.getInstance());
this.test(EagerSingletonSensorsManager.getInstance());
this.test(DbleCheckedSingletonSensorsManager.getInstance());
}
private void test(SingletonSensorManager manager) {
manager.addSensor(new TemperatureSensor());
manager.addSensor(new SpeedSensor());
manager.addSensor(new RoadConditionSensor());
manager.printState();
}
}

View File

@ -0,0 +1,15 @@
package lab12_singleton.ex1;
public abstract class Sensor {
private boolean ok = true;
public abstract String getName();
public void setOk(boolean ok) {
this.ok = ok;
}
@Override
public String toString() {
return getName() + ": " + (ok ? "Ok" : "Warning");
}
}

View File

@ -0,0 +1,17 @@
package lab12_singleton.ex1;
import java.util.ArrayList;
public abstract class SingletonSensorManager {
private final ArrayList<Sensor> sensors = new ArrayList<>();
protected SingletonSensorManager() {}
public void addSensor(Sensor sensor) {
sensors.add(sensor);
}
public void printState() {
System.out.println("<" + this.getClass().getSimpleName() + ": " + sensors.toString() + ">");
}
}

View File

@ -0,0 +1,18 @@
package lab12_singleton.ex1.managers;
import lab12_singleton.ex1.SingletonSensorManager;
public class DbleCheckedSingletonSensorsManager extends SingletonSensorManager {
private static DbleCheckedSingletonSensorsManager instance;
public static DbleCheckedSingletonSensorsManager getInstance() {
if (instance == null) {
synchronized (DbleCheckedSingletonSensorsManager.class) {
if (instance == null) {
instance = new DbleCheckedSingletonSensorsManager();
}
}
}
return instance;
}
}

View File

@ -0,0 +1,11 @@
package lab12_singleton.ex1.managers;
import lab12_singleton.ex1.SingletonSensorManager;
public class EagerSingletonSensorsManager extends SingletonSensorManager {
private static final EagerSingletonSensorsManager instance = new EagerSingletonSensorsManager();
public static EagerSingletonSensorsManager getInstance() {
return instance;
}
}

View File

@ -0,0 +1,14 @@
package lab12_singleton.ex1.managers;
import lab12_singleton.ex1.SingletonSensorManager;
public class SimpleSingletonSensorsManager extends SingletonSensorManager {
private static SimpleSingletonSensorsManager instance;
public static SingletonSensorManager getInstance() {
if (instance == null) {
instance = new SimpleSingletonSensorsManager();
}
return instance;
}
}

View File

@ -0,0 +1,14 @@
package lab12_singleton.ex1.managers;
import lab12_singleton.ex1.SingletonSensorManager;
public class SynchSingletonSensorsManager extends SingletonSensorManager {
private static SynchSingletonSensorsManager instance;
public static synchronized SynchSingletonSensorsManager getInstance() {
if (instance == null) {
instance = new SynchSingletonSensorsManager();
}
return instance;
}
}

View File

@ -0,0 +1,10 @@
package lab12_singleton.ex1.sensors;
import lab12_singleton.ex1.Sensor;
public class RoadConditionSensor extends Sensor {
@Override
public String getName() {
return "Road Condition";
}
}

View File

@ -0,0 +1,10 @@
package lab12_singleton.ex1.sensors;
import lab12_singleton.ex1.Sensor;
public class SpeedSensor extends Sensor {
@Override
public String getName() {
return "Speed";
}
}

View File

@ -0,0 +1,10 @@
package lab12_singleton.ex1.sensors;
import lab12_singleton.ex1.Sensor;
public class TemperatureSensor extends Sensor {
@Override
public String getName() {
return "Temperature";
}
}

View File

@ -0,0 +1,27 @@
package lab13_proxy.ex1;
public class Image {
protected final String path;
protected final String resolution;
protected boolean loaded = false;
public Image(String path, String resolution) {
this.path = path;
this.resolution = resolution;
}
public void load() {
if (!loaded) {
System.out.println("Image " + path + " is loaded in " + resolution + " resolution");
loaded = true;
}
}
public boolean isLoaded() {
return loaded;
}
public void showImage(User user) {
System.out.println("Image " + path + " is shown in " + resolution + " resolution for user " + user.getName());
}
}

View File

@ -0,0 +1,24 @@
package lab13_proxy.ex1;
public class ImageProxy extends Image {
private final Image lowResImage;
private final Image highResImage;
public ImageProxy(String path) {
super(path, "high");
lowResImage = new Image(path, "low");
highResImage = new Image(path, "high");
}
public void showImage(User user) {
System.out.println(user.getName() + " selects preview image " + path + " and wants to see its full resolution.");
if (RegistrationService.isRegistered(user)) {
highResImage.load();
highResImage.showImage(user);
} else {
System.out.println("User " + user.getName() + " is not registered. Showing preview image in low resolution");
lowResImage.load();
lowResImage.showImage(user);
}
}
}

View File

@ -0,0 +1,19 @@
package lab13_proxy.ex1;
public class Main {
public static void main(String[] args) {
User jean = new User("Jean");
User paul = new User("Paul");
User pierre = new User("Pierre");
RegistrationService.register(paul);
Image highResolutionImage1 = new ImageProxy("sample/veryHighResPhoto1.jpeg");
Image highResolutionImage2 = new ImageProxy("sample/veryHighResPhoto2.jpeg");
Image highResolutionImage3 = new ImageProxy("sample/veryHighResPhoto3.jpeg");
highResolutionImage1.showImage(jean);
highResolutionImage2.showImage(paul);
highResolutionImage3.showImage(pierre);
}
}

View File

@ -0,0 +1,15 @@
package lab13_proxy.ex1;
import java.util.ArrayList;
public class RegistrationService {
private static final ArrayList<User> users = new ArrayList<>();
public static void register(User user) {
System.out.println(user.getName() + " is now registered");
users.add(user);
}
public static boolean isRegistered(User user) {
return users.contains(user);
}
}

View File

@ -0,0 +1,13 @@
package lab13_proxy.ex1;
public class User {
private final String name;
public User(String name) {
this.name = name;
}
public String getName() {
return name;
}
}

View File

@ -0,0 +1,36 @@
package lab13_proxy.ex2;
import java.util.Random;
public class Account {
private int balance;
private int accountNumber;
private String owner;
public Account(String owner, int startBalance) {
this.owner = owner;
this.balance = startBalance;
this.accountNumber = new Random().hashCode();
}
public void deposit(int amount) {
balance += amount;
}
public void withdraw(int amount) {
balance -= amount;
}
public String getOwner() {
return owner;
}
public int getBalance() {
return balance;
}
@Override
public String toString() {
return "account " + this.accountNumber + " belonging to " + this.owner;
}
}

View File

@ -0,0 +1,52 @@
package lab13_proxy.ex2;
import java.util.ArrayList;
public class Bank {
private final ArrayList<String> blacklistedClients = new ArrayList<>();
public void blacklist(String client) {
blacklistedClients.add(client);
}
public boolean isBlacklisted(String client) {
return blacklistedClients.contains(client);
}
public void deposit(String client, Account account, int amount) {
if (amount < 0) {
System.out.println("Cannot deposit a negative amount");
return;
}
if (isBlacklisted(client)) {
System.out.println(client + " is on a blacklist and does not have the right to DEPOSIT money into " + account);
return;
}
account.deposit(amount);
System.out.println(client + " has deposited " + amount + " on " + account + ". New balance is " + account.getBalance());
}
public void withdraw(String client, Account account, int amount) {
if (amount < 0) {
System.out.println("Cannot withdraw a negative amount");
return;
}
if (isBlacklisted(client)) {
System.out.println(client + " is on a blacklist and does not have the right to WITHDRAW money from " + account);
return;
}
if (!account.getOwner().equals(client)) {
System.out.println(client + " cannot WITHDRAW money from " + account + " because they are not the owner");
return;
}
if (account.getBalance() < amount) {
System.out.println(client + " cannot WITHDRAW money from " + account + " because there is not enough money on the account.");
return;
}
account.withdraw(amount);
System.out.println(client + " has withdrawn " + amount + " from " + account + ". New balance is " + account.getBalance());
}
}

View File

@ -0,0 +1,18 @@
package lab13_proxy.ex2;
public class Main {
public static void main(String[] args) {
Account account = new Account("Pascale", 16000);
Bank bank = new Bank();
bank.blacklist("Jean");
bank.blacklist("Pierre");
bank.deposit("Marcel", account, 1000);
bank.deposit("Jean", account, 1000);
bank.withdraw("Marcel", account, 1000);
bank.withdraw("Pascale", account, 1000000);
bank.withdraw("Pascale", account, 1000);
bank.withdraw("Pierre", account, 1000);
}
}

View File

@ -0,0 +1,8 @@
package lab14_strategy.ex1;
public class AxeBehavior implements WeaponBehavior {
@Override
public String useWeapon() {
return "chops with an axe";
}
}

View File

@ -0,0 +1,19 @@
package lab14_strategy.ex1;
public abstract class Character {
private WeaponBehavior weapon;
public Character(WeaponBehavior weapon) {
this.weapon = weapon;
}
public void setWeapon(WeaponBehavior weapon) {
this.weapon = weapon;
}
public abstract String getName();
public void fight() {
System.out.println(getName() + " " + weapon.useWeapon());
}
}

View File

@ -0,0 +1,25 @@
package lab14_strategy.ex1;
import lab14_strategy.ex1.characters.King;
import lab14_strategy.ex1.characters.Knight;
import lab14_strategy.ex1.characters.Troll;
import lab14_strategy.ex1.characters.Queen;
import lab14_strategy.ex1.weapons.BowAndArrowBehavior;
import lab14_strategy.ex1.weapons.KnifeBehavior;
public class Game {
public static void main(String[] args) {
Character king = new King(new KnifeBehavior());
Character queen = new Queen(new BowAndArrowBehavior());
Character knight = new Knight(new SwordBehavior());
Character troll = new Troll(new AxeBehavior());
king.fight();
queen.fight();
knight.fight();
troll.fight();
king.setWeapon(new SwordBehavior());
king.fight();
}
}

View File

@ -0,0 +1,8 @@
package lab14_strategy.ex1;
public class SwordBehavior implements WeaponBehavior {
@Override
public String useWeapon() {
return "swings a sword";
}
}

View File

@ -0,0 +1,5 @@
package lab14_strategy.ex1;
public interface WeaponBehavior {
String useWeapon();
}

View File

@ -0,0 +1,15 @@
package lab14_strategy.ex1.characters;
import lab14_strategy.ex1.Character;
import lab14_strategy.ex1.WeaponBehavior;
public class King extends Character {
public King(WeaponBehavior weapon) {
super(weapon);
}
@Override
public String getName() {
return "King";
}
}

View File

@ -0,0 +1,15 @@
package lab14_strategy.ex1.characters;
import lab14_strategy.ex1.Character;
import lab14_strategy.ex1.WeaponBehavior;
public class Knight extends Character {
public Knight(WeaponBehavior weapon) {
super(weapon);
}
@Override
public String getName() {
return "Knight";
}
}

View File

@ -0,0 +1,15 @@
package lab14_strategy.ex1.characters;
import lab14_strategy.ex1.Character;
import lab14_strategy.ex1.WeaponBehavior;
public class Queen extends Character {
public Queen(WeaponBehavior weapon) {
super(weapon);
}
@Override
public String getName() {
return "Queen";
}
}

View File

@ -0,0 +1,15 @@
package lab14_strategy.ex1.characters;
import lab14_strategy.ex1.Character;
import lab14_strategy.ex1.WeaponBehavior;
public class Troll extends Character {
public Troll(WeaponBehavior weapon) {
super(weapon);
}
@Override
public String getName() {
return "Troll";
}
}

View File

@ -0,0 +1,10 @@
package lab14_strategy.ex1.weapons;
import lab14_strategy.ex1.WeaponBehavior;
public class BowAndArrowBehavior implements WeaponBehavior {
@Override
public String useWeapon() {
return "shoots an arrow with a bow";
}
}

View File

@ -0,0 +1,10 @@
package lab14_strategy.ex1.weapons;
import lab14_strategy.ex1.WeaponBehavior;
public class KnifeBehavior implements WeaponBehavior {
@Override
public String useWeapon() {
return "cuts with a knife";
}
}

View File

@ -0,0 +1,5 @@
package lab14_strategy.ex2;
public interface SortAlgorithm {
void sort(int[] vector);
}

View File

@ -0,0 +1,21 @@
package lab14_strategy.ex2;
public class Sorter {
private SortAlgorithm algorithm;
public void sort(int[] vector) {
algorithm.sort(vector);
}
public void setAlgorithm(SortAlgorithm algorithm) {
this.algorithm = algorithm;
}
public void showVectorData(int[] vector) {
StringBuilder sb = new StringBuilder();
for (int j : vector) {
sb.append(j).append("\t");
}
System.out.println(sb);
}
}

View File

@ -0,0 +1,35 @@
package lab14_strategy.ex2;
import lab14_strategy.ex2.algorithms.BubbleSort;
import lab14_strategy.ex2.algorithms.InsertionSort;
import lab14_strategy.ex2.algorithms.SelectionSort;
public class StrategySortLauncher {
public static void main(String[] args) {
StrategySortLauncher launcher = new StrategySortLauncher();
launcher.test();
}
public void test() {
int[] tab1 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
int[] tab2 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
int[] tab3 = { 99, 11, 2, 33, 12, 1, 0, 99, 34, 35 };
Sorter sorter = new Sorter();
System.out.println("test bubble sort");
sorter.setAlgorithm(new BubbleSort());
sorter.sort(tab1);
sorter.showVectorData(tab1);
System.out.println("test insert sort");
sorter.setAlgorithm(new InsertionSort());
sorter.sort(tab2);
sorter.showVectorData(tab2);
System.out.println("test selection sort");
sorter.setAlgorithm(new SelectionSort());
sorter.sort(tab3);
sorter.showVectorData(tab3);
}
}

View File

@ -0,0 +1,23 @@
package lab14_strategy.ex2.algorithms;
import lab14_strategy.ex2.SortAlgorithm;
public class BubbleSort implements SortAlgorithm {
@Override
public void sort(int[] vector) {
int temp;
int nbrePermutation = -1;
int nbreIteration = 0;
while (nbrePermutation != 0) {
nbrePermutation = 0;
for (int i=0; i<vector.length - nbreIteration - 1; i++) {
if (vector[i] > vector[i + 1]) {
nbrePermutation++;
temp = vector[i + 1];
vector[i + 1] = vector[i];
vector[i] = temp;
}
}
}
}
}

View File

@ -0,0 +1,22 @@
package lab14_strategy.ex2.algorithms;
import lab14_strategy.ex2.SortAlgorithm;
public class InsertionSort implements SortAlgorithm {
@Override
public void sort(int[] vector) {
int temp;
for (int i=1; i<vector.length; i++) {
temp = vector[i];
for (int j = i - 1; j >= 0; j--) {
if (vector[j] > temp) {
vector[j + 1] = vector[j];
vector[j] = temp;
} else {
vector[j + 1] = temp;
break;
}
}
}
}
}

View File

@ -0,0 +1,22 @@
package lab14_strategy.ex2.algorithms;
import lab14_strategy.ex2.SortAlgorithm;
public class SelectionSort implements SortAlgorithm {
@Override
public void sort(int[] vector) {
int temp, cursor = 0;
for (int i=0; i<vector.length; i++) {
temp = vector[i];
cursor = i;
for (int j = i; j < vector.length; j++) {
if (vector[j] < temp) {
temp = vector[j];
cursor = j;
}
}
vector[cursor] = vector[i];
vector[i] = temp;
}
}
}

View File

@ -0,0 +1,24 @@
package lab15_observer.ex1;
public class Main {
public static void main(String[] args) {
PatientMonitoring pm = new PatientMonitoring(1, 140, 85);
MedicalEmployee jean = new MedicalEmployee("Jean", pm);
MedicalEmployee pauline = new MedicalEmployee("Pauline", pm);
MedicalEmployee matthieu = new MedicalEmployee("Matthieu", pm);
MedicalEmployee symeon = new MedicalEmployee("Symeon", pm);
pm.setBloodPressure(110);
pm.setPosition(3);
pm.setPulseOximetry(90);
pm.setPulseOximetry(70);
pm.setPosition(7);
pm.setBloodPressure(150);
pm.removeObserver(matthieu);
pm.setBloodPressure(145);
pm.setPosition(9);
}
}

View File

@ -0,0 +1,19 @@
package lab15_observer.ex1;
public class MedicalEmployee implements PatientObserver {
private final String name;
public MedicalEmployee(String name, PatientMonitoring pm) {
this.name = name;
pm.registerObserver(this);
}
public String getName() {
return name;
}
@Override
public void update(Problem problem, PatientMonitoring pm) {
System.out.println(getName() + " has been notified of " + problem + " for patient " + pm);
}
}

View File

@ -0,0 +1,70 @@
package lab15_observer.ex1;
import java.util.ArrayList;
import java.util.List;
public class PatientMonitoring {
private final List<PatientObserver> observers = new ArrayList<>();
private int position;
private int bloodPressure;
private int pulseOximetry;
public PatientMonitoring(int position, int bloodPressure, int pulseOximetry) {
this.position = position;
this.bloodPressure = bloodPressure;
this.pulseOximetry = pulseOximetry;
}
public void registerObserver(PatientObserver o) {
observers.add(o);
}
public void removeObserver(PatientObserver o) {
observers.remove(o);
}
private void notifyObservers(Problem problem) {
for (PatientObserver o : observers) {
o.update(problem, this);
}
}
public int getPosition() {
return position;
}
public int getBloodPressure() {
return bloodPressure;
}
public int getPulseOximetry() {
return pulseOximetry;
}
public void setPosition(int position) {
this.position = position;
notifyObservers(Problem.NO_PROBLEM);
}
public void setBloodPressure(int bloodPressure) {
this.bloodPressure = bloodPressure;
if (getBloodPressure() > 145) {
notifyObservers(Problem.BLOOD_PRESSURE);
} else {
notifyObservers(Problem.NO_PROBLEM);
}
}
public void setPulseOximetry(int pulseOximetry) {
this.pulseOximetry = pulseOximetry;
if (getPulseOximetry() > 85) {
notifyObservers(Problem.OXIMETRY);
} else {
notifyObservers(Problem.NO_PROBLEM);
}
}
@Override
public String toString() {
return "<Patient Position: " + position + ", BP: " + bloodPressure + ", PO: " + pulseOximetry + ">";
}
}

View File

@ -0,0 +1,5 @@
package lab15_observer.ex1;
public interface PatientObserver {
void update(Problem problem, PatientMonitoring pm);
}

View File

@ -0,0 +1,7 @@
package lab15_observer.ex1;
public enum Problem {
NO_PROBLEM,
BLOOD_PRESSURE,
OXIMETRY
}

View File

@ -0,0 +1,103 @@
package lab15_observer.ex2;
public class AnalogTimer implements TimerObserver {
private final MyTimer timer;
private static final String[][] digits = {
{
"┌─┐",
"│ │",
"└─┘"
},
{
"",
"",
""
},
{
"╶─┐",
"┌─┘",
"└─╴"
},
{
"╶─┐",
" ─┤",
"╶─┘"
},
{
"╷ ╷",
"└─┤",
""
},
{
"┌─╴",
"└─┐",
"╶─┘"
},
{
"┌─╴",
"├─┐",
"└─┘"
},
{
"╶─┐",
"",
""
},
{
"┌─┐",
"├─┤",
"└─┘"
},
{
"┌─┐",
"└─┤",
"╶─┘"
},
};
public AnalogTimer(MyTimer timer) {
this.timer = timer;
timer.registerObserver(this);
}
public void update(int hours, int minutes, int seconds) {
String[] lines = {"", "", ""};
addDigit(lines, hours / 10);
addSpace(lines);
addDigit(lines, hours % 10);
addSep(lines);
addDigit(lines, minutes / 10);
addSpace(lines);
addDigit(lines, minutes % 10);
addSep(lines);
addDigit(lines, seconds / 10);
addSpace(lines);
addDigit(lines, seconds % 10);
System.out.println(lines[0]);
System.out.println(lines[1]);
System.out.println(lines[2]);
}
private void addDigit(String[] lines, int digit) {
String[] parts = digits[digit];
for (int i = 0; i < 3; i++) {
lines[i] += parts[i];
}
}
private void addSpace(String[] lines) {
lines[0] += " ";
lines[1] += " ";
lines[2] += " ";
}
private void addSep(String[] lines) {
lines[0] += " o ";
lines[1] += " ";
lines[2] += " o ";
}
}

View File

@ -0,0 +1,8 @@
package lab15_observer.ex2;
public interface ClockTimer {
int getHour();
int getMinute();
int getSecond();
void tick();
}

View File

@ -0,0 +1,15 @@
package lab15_observer.ex2;
public class DigitalTimer implements TimerObserver {
private final MyTimer timer;
public DigitalTimer(MyTimer timer) {
this.timer = timer;
timer.registerObserver(this);
}
@Override
public void update(int hours, int minutes, int seconds) {
System.out.printf("Time: %02d:%02d:%02d%n", hours, minutes, seconds);
}
}

View File

@ -0,0 +1,10 @@
package lab15_observer.ex2;
public class Main {
public static void main(String[] args) {
MyTimer clockTimer = new MyTimer();
new AnalogTimer(clockTimer);
new DigitalTimer(clockTimer);
}
}

View File

@ -0,0 +1,53 @@
package lab15_observer.ex2;
import java.util.*;
public class MyTimer implements ClockTimer {
private final List<TimerObserver> observers = new ArrayList<>();
private Calendar cal;
private Timer timer;
public MyTimer() {
cal = Calendar.getInstance();
timer = new Timer();
timer.schedule(new TimerAction(), 0, 1000);
}
class TimerAction extends TimerTask {
@Override
public void run() {
cal = Calendar.getInstance();
tick();
}
}
@Override
public int getHour() {
return cal.get(Calendar.HOUR_OF_DAY);
}
@Override
public int getMinute() {
return cal.get(Calendar.MINUTE);
}
@Override
public int getSecond() {
return cal.get(Calendar.SECOND);
}
@Override
public void tick() {
for (TimerObserver o : observers) {
o.update(getHour(), getMinute(), getSecond());
}
}
public void registerObserver(TimerObserver o) {
observers.add(o);
}
public void removeObserver(TimerObserver o) {
observers.remove(o);
}
}

View File

@ -0,0 +1,5 @@
package lab15_observer.ex2;
public interface TimerObserver{
void update(int hours, int minutes, int seconds);
}

View File

@ -0,0 +1,15 @@
package learn.simple_observer;
public class ConcreteObserver implements Observer {
private ConcreteSubject subject;
public ConcreteObserver(ConcreteSubject subject) {
this.subject = subject;
subject.registerObserver(this);
}
@Override
public void update() {
System.out.println("Observer was notified: subject has new state: " + subject.getState());
}
}

View File

@ -0,0 +1,37 @@
package learn.simple_observer;
import java.util.ArrayList;
import java.util.List;
public class ConcreteSubject implements Subject {
private List<Observer> observers = new ArrayList<>();
int state;
@Override
public void registerObserver(Observer o) {
observers.add(o);
}
@Override
public void removeObserver(Observer o) {
observers.remove(o);
}
@Override
public void notifyObservers() {
for (Observer o : observers) {
o.update();
}
}
public void setState(int state) {
this.state = state;
if (state > 50) {
notifyObservers();
}
}
public int getState() {
return state;
}
}

View File

@ -0,0 +1,12 @@
package learn.simple_observer;
public class Main {
public static void main(String[] args) {
ConcreteSubject subject = new ConcreteSubject();
Observer observer = new ConcreteObserver(subject);
subject.setState(40);
subject.setState(100);
Observer observer2 = new ConcreteObserver(subject);
subject.setState(200);
}
}

View File

@ -0,0 +1,5 @@
package learn.simple_observer;
public interface Observer {
void update();
}

View File

@ -0,0 +1,7 @@
package learn.simple_observer;
public interface Subject {
void registerObserver(Observer o);
void removeObserver(Observer o);
void notifyObservers();
}

View File

@ -0,0 +1,8 @@
package learn.simple_proxy;
public class Main {
public static void main(String[] args) {
Subject subject = new Proxy();
subject.request();
}
}

View File

@ -0,0 +1,11 @@
package learn.simple_proxy;
public class Proxy implements Subject {
private RealSubject realSubject = new RealSubject();
@Override
public void request() {
System.out.println("Proxy does some preliminary job and checks");
realSubject.request();
}
}

View File

@ -0,0 +1,8 @@
package learn.simple_proxy;
public class RealSubject implements Subject {
@Override
public void request() {
System.out.println("Real subject action");
}
}

View File

@ -0,0 +1,5 @@
package learn.simple_proxy;
public interface Subject {
public void request();
}

View File

@ -0,0 +1,8 @@
package learn.simple_strategy;
public class ConcreteStrategyA implements Strategy {
@Override
public void algorithm() {
System.out.println("nice strategy A");
}
}

View File

@ -0,0 +1,8 @@
package learn.simple_strategy;
public class ConcreteStrategyB implements Strategy {
@Override
public void algorithm() {
System.out.println("nice strategy B");
}
}

View File

@ -0,0 +1,17 @@
package learn.simple_strategy;
public class Context {
private Strategy currentStrategy;
public Context(Strategy currentStrategy) {
this.currentStrategy = currentStrategy;
}
public void setCurrentStrategy(Strategy currentStrategy) {
this.currentStrategy = currentStrategy;
}
public void doSomeJob() {
currentStrategy.algorithm();
}
}

View File

@ -0,0 +1,11 @@
package learn.simple_strategy;
public class Main {
public static void main(String[] args) {
Context context = new Context(new ConcreteStrategyB());
context.doSomeJob();
context.setCurrentStrategy(new ConcreteStrategyA());
context.doSomeJob();
}
}

View File

@ -0,0 +1,5 @@
package learn.simple_strategy;
public interface Strategy {
void algorithm();
}