From 58362e77ed9d35b082b3bbfd2bea2878ef44dc1d Mon Sep 17 00:00:00 2001 From: alanbarco Date: Mon, 28 Jun 2021 22:50:26 -0500 Subject: [PATCH] Fixing error of principles SOLID --- .classpath | 11 +++ .gitignore | 1 + .project | 17 +++++ .../training/dip/exercise/EncodingModule.java | 59 ---------------- .../dip/exercise/EncodingModuleClient.java | 13 ---- .../training/dip/exercise/MyDatabase.java | 16 ----- .../dip/exercise/beforeEncryption.txt | 5 -- .../directi/training/isp/exercise/Door.java | 16 ----- .../training/isp/exercise/SensingDoor.java | 52 -------------- .../directi/training/isp/exercise/Sensor.java | 21 ------ .../training/isp/exercise/TimedDoor.java | 53 --------------- .../directi/training/isp/exercise/Timer.java | 19 ------ .../directi/training/lsp/exercise/Duck.java | 14 ---- .../training/lsp/exercise/ElectronicDuck.java | 36 ---------- .../directi/training/lsp/exercise/Pool.java | 32 --------- .../ocp/exercise/ResourceAllocator.java | 67 ------------------- .../training/ocp/exercise/ResourceType.java | 14 ---- .../directi/training/srp/exercise/Car.java | 30 --------- .../training/srp/exercise/CarManager.java | 44 ------------ 19 files changed, 29 insertions(+), 491 deletions(-) create mode 100644 .classpath create mode 100644 .project delete mode 100644 DIP/src/com/directi/training/dip/exercise/EncodingModule.java delete mode 100644 DIP/src/com/directi/training/dip/exercise/EncodingModuleClient.java delete mode 100644 DIP/src/com/directi/training/dip/exercise/MyDatabase.java delete mode 100644 DIP/src/com/directi/training/dip/exercise/beforeEncryption.txt delete mode 100644 ISP/src/com/directi/training/isp/exercise/Door.java delete mode 100644 ISP/src/com/directi/training/isp/exercise/SensingDoor.java delete mode 100644 ISP/src/com/directi/training/isp/exercise/Sensor.java delete mode 100644 ISP/src/com/directi/training/isp/exercise/TimedDoor.java delete mode 100644 ISP/src/com/directi/training/isp/exercise/Timer.java delete mode 100644 LSP/src/com/directi/training/lsp/exercise/Duck.java delete mode 100644 LSP/src/com/directi/training/lsp/exercise/ElectronicDuck.java delete mode 100644 LSP/src/com/directi/training/lsp/exercise/Pool.java delete mode 100644 OCP/src/com/directi/training/ocp/exercise/ResourceAllocator.java delete mode 100644 OCP/src/com/directi/training/ocp/exercise/ResourceType.java delete mode 100644 SRP/src/com/directi/training/srp/exercise/Car.java delete mode 100644 SRP/src/com/directi/training/srp/exercise/CarManager.java diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..1710dbe --- /dev/null +++ b/.classpath @@ -0,0 +1,11 @@ + + + + + + + + + + + diff --git a/.gitignore b/.gitignore index 8cd8cef..32501d8 100644 --- a/.gitignore +++ b/.gitignore @@ -16,3 +16,4 @@ # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml hs_err_pid* +/bin/ diff --git a/.project b/.project new file mode 100644 index 0000000..06d7edf --- /dev/null +++ b/.project @@ -0,0 +1,17 @@ + + + SOLID + + + + + + org.eclipse.jdt.core.javabuilder + + + + + + org.eclipse.jdt.core.javanature + + diff --git a/DIP/src/com/directi/training/dip/exercise/EncodingModule.java b/DIP/src/com/directi/training/dip/exercise/EncodingModule.java deleted file mode 100644 index 1aa67a4..0000000 --- a/DIP/src/com/directi/training/dip/exercise/EncodingModule.java +++ /dev/null @@ -1,59 +0,0 @@ -package com.directi.training.dip.exercise; - -import java.io.BufferedReader; -import java.io.BufferedWriter; -import java.io.FileReader; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.URL; -import java.util.Base64; - -public class EncodingModule -{ - public void encodeWithFiles() throws IOException - { - BufferedReader reader = null; - BufferedWriter writer = null; - try { - reader = new BufferedReader( - new FileReader("DIP/src/com/directi/training/dip/exercise/beforeEncryption.txt")); - writer = new BufferedWriter( - new FileWriter("DIP/src/com/directi/training/dip/exercise/afterEncryption.txt")); - String aLine; - while ((aLine = reader.readLine()) != null) { - String encodedLine = Base64.getEncoder().encodeToString(aLine.getBytes()); - writer.append(encodedLine); - } - } finally { - if (writer != null) { - writer.close(); - } - if (reader != null) { - reader.close(); - } - } - } - - public void encodeBasedOnNetworkAndDatabase() throws IOException - { - URL url; - url = new URL("http", "myfirstappwith.appspot.com", "/index.html"); - InputStream in; - in = url.openStream(); - InputStreamReader reader = new InputStreamReader(in); - StringBuilder inputString1 = new StringBuilder(); - int c; - c = reader.read(); - while (c != -1) { - inputString1.append((char) c); - c = reader.read(); - } - String inputString = inputString1.toString(); - String encodedString = Base64.getEncoder().encodeToString(inputString.getBytes()); - MyDatabase database = new MyDatabase(); - database.write(encodedString); - } -} - diff --git a/DIP/src/com/directi/training/dip/exercise/EncodingModuleClient.java b/DIP/src/com/directi/training/dip/exercise/EncodingModuleClient.java deleted file mode 100644 index 6573512..0000000 --- a/DIP/src/com/directi/training/dip/exercise/EncodingModuleClient.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.directi.training.dip.exercise; - -import java.io.IOException; - -public class EncodingModuleClient -{ - public static void main(String[] args) throws IOException - { - EncodingModule encodingModule = new EncodingModule(); - encodingModule.encodeWithFiles(); - encodingModule.encodeBasedOnNetworkAndDatabase(); - } -} diff --git a/DIP/src/com/directi/training/dip/exercise/MyDatabase.java b/DIP/src/com/directi/training/dip/exercise/MyDatabase.java deleted file mode 100644 index c16aa84..0000000 --- a/DIP/src/com/directi/training/dip/exercise/MyDatabase.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.directi.training.dip.exercise; - -import java.util.HashMap; -import java.util.Map; - -public class MyDatabase -{ - private static Map _data = new HashMap<>(); - private static int _count = 0; - - public int write(String inputString) - { - _data.put(++_count, inputString); - return _count; - } -} diff --git a/DIP/src/com/directi/training/dip/exercise/beforeEncryption.txt b/DIP/src/com/directi/training/dip/exercise/beforeEncryption.txt deleted file mode 100644 index 54ac439..0000000 --- a/DIP/src/com/directi/training/dip/exercise/beforeEncryption.txt +++ /dev/null @@ -1,5 +0,0 @@ -UserName:Brad Smith,Email:brad.m@foo.com,Password:foo -UserName:Bill Board,Email:bill.b@foo.com,Password:bar -UserName:Mike Stand,Email:mike.s@foo.com,Password:baz -UserName:Anna Curtain,Email:anna.a@foo.com,Password:qux -UserName:Kathy Smith,Email:kathy.s@foo.com,Password:corge diff --git a/ISP/src/com/directi/training/isp/exercise/Door.java b/ISP/src/com/directi/training/isp/exercise/Door.java deleted file mode 100644 index 6c8eaa8..0000000 --- a/ISP/src/com/directi/training/isp/exercise/Door.java +++ /dev/null @@ -1,16 +0,0 @@ -package com.directi.training.isp.exercise; - -public interface Door -{ - void lock(); - - void unlock(); - - void open(); - - void close(); - - void timeOutCallback(); - - void proximityCallback(); -} diff --git a/ISP/src/com/directi/training/isp/exercise/SensingDoor.java b/ISP/src/com/directi/training/isp/exercise/SensingDoor.java deleted file mode 100644 index 1b97e5a..0000000 --- a/ISP/src/com/directi/training/isp/exercise/SensingDoor.java +++ /dev/null @@ -1,52 +0,0 @@ -package com.directi.training.isp.exercise; - -import sun.reflect.generics.reflectiveObjects.NotImplementedException; - -public class SensingDoor implements Door -{ - private boolean _locked; - private boolean _opened; - - public SensingDoor(Sensor sensor) - { - sensor.register(this); - } - - @Override - public void lock() - { - _locked = true; - } - - @Override - public void unlock() - { - _locked = false; - } - - @Override - public void open() - { - if (!_locked) { - _opened = true; - } - } - - @Override - public void close() - { - _opened = false; - } - - @Override - public void timeOutCallback() - { - throw new NotImplementedException(); - } - - @Override - public void proximityCallback() - { - _opened = true; - } -} diff --git a/ISP/src/com/directi/training/isp/exercise/Sensor.java b/ISP/src/com/directi/training/isp/exercise/Sensor.java deleted file mode 100644 index faf7044..0000000 --- a/ISP/src/com/directi/training/isp/exercise/Sensor.java +++ /dev/null @@ -1,21 +0,0 @@ -package com.directi.training.isp.exercise; - -import java.util.Random; - -public class Sensor -{ - public void register(Door door) - { - while (true) { - if (isPersonClose()) { - door.proximityCallback(); - break; - } - } - } - - private boolean isPersonClose() - { - return new Random().nextBoolean(); - } -} diff --git a/ISP/src/com/directi/training/isp/exercise/TimedDoor.java b/ISP/src/com/directi/training/isp/exercise/TimedDoor.java deleted file mode 100644 index 9374f4a..0000000 --- a/ISP/src/com/directi/training/isp/exercise/TimedDoor.java +++ /dev/null @@ -1,53 +0,0 @@ -package com.directi.training.isp.exercise; - -import sun.reflect.generics.reflectiveObjects.NotImplementedException; - -public class TimedDoor implements Door -{ - private static final int TIME_OUT = 100; - private boolean _locked; - private boolean _opened; - - public TimedDoor(Timer timer) - { - timer.register(TIME_OUT, this); - } - - @Override - public void lock() - { - _locked = true; - } - - @Override - public void unlock() - { - _locked = false; - } - - @Override - public void open() - { - if (!_locked) { - _opened = true; - } - } - - @Override - public void close() - { - _opened = false; - } - - @Override - public void timeOutCallback() - { - _locked = true; - } - - @Override - public void proximityCallback() - { - throw new NotImplementedException(); - } -} diff --git a/ISP/src/com/directi/training/isp/exercise/Timer.java b/ISP/src/com/directi/training/isp/exercise/Timer.java deleted file mode 100644 index 5b27fe8..0000000 --- a/ISP/src/com/directi/training/isp/exercise/Timer.java +++ /dev/null @@ -1,19 +0,0 @@ -package com.directi.training.isp.exercise; - -import java.util.TimerTask; - -public class Timer -{ - public void register(long timeOut, final Door door) - { - java.util.Timer timerUtility = new java.util.Timer(); - timerUtility.schedule(new TimerTask() - { - @Override - public void run() - { - door.timeOutCallback(); - } - }, timeOut); - } -} diff --git a/LSP/src/com/directi/training/lsp/exercise/Duck.java b/LSP/src/com/directi/training/lsp/exercise/Duck.java deleted file mode 100644 index c0aae5e..0000000 --- a/LSP/src/com/directi/training/lsp/exercise/Duck.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.directi.training.lsp.exercise; - -public class Duck -{ - public void quack() - { - System.out.println("Quack..."); - } - - public void swim() - { - System.out.println("Swim..."); - } -} diff --git a/LSP/src/com/directi/training/lsp/exercise/ElectronicDuck.java b/LSP/src/com/directi/training/lsp/exercise/ElectronicDuck.java deleted file mode 100644 index ea00fec..0000000 --- a/LSP/src/com/directi/training/lsp/exercise/ElectronicDuck.java +++ /dev/null @@ -1,36 +0,0 @@ -package com.directi.training.lsp.exercise; - -public class ElectronicDuck extends Duck -{ - private boolean _on = false; - - @Override - public void quack() - { - if (_on) { - System.out.println("Electronic duck quack..."); - } else { - throw new RuntimeException("Can't quack when off"); - } - } - - @Override - public void swim() - { - if (_on) { - System.out.println("Electronic duck swim"); - } else { - throw new RuntimeException("Can't swim when off"); - } - } - - public void turnOn() - { - _on = true; - } - - public void turnOff() - { - _on = false; - } -} diff --git a/LSP/src/com/directi/training/lsp/exercise/Pool.java b/LSP/src/com/directi/training/lsp/exercise/Pool.java deleted file mode 100644 index 4f7f718..0000000 --- a/LSP/src/com/directi/training/lsp/exercise/Pool.java +++ /dev/null @@ -1,32 +0,0 @@ -package com.directi.training.lsp.exercise; - -public class Pool -{ - public void run() - { - Duck donaldDuck = new Duck(); - Duck electricDuck = new ElectronicDuck(); - quack(donaldDuck, electricDuck); - swim(donaldDuck, electricDuck); - } - - private void quack(Duck... ducks) - { - for (Duck duck : ducks) { - duck.quack(); - } - } - - private void swim(Duck... ducks) - { - for (Duck duck : ducks) { - duck.swim(); - } - } - - public static void main(String[] args) - { - Pool pool = new Pool(); - pool.run(); - } -} diff --git a/OCP/src/com/directi/training/ocp/exercise/ResourceAllocator.java b/OCP/src/com/directi/training/ocp/exercise/ResourceAllocator.java deleted file mode 100644 index 5d262f5..0000000 --- a/OCP/src/com/directi/training/ocp/exercise/ResourceAllocator.java +++ /dev/null @@ -1,67 +0,0 @@ -package com.directi.training.ocp.exercise; - -public class ResourceAllocator -{ - private static final int INVALID_RESOURCE_ID = -1; - - public int allocate(ResourceType resourceType) - { - int resourceId; - switch (resourceType) { - case TIME_SLOT: - resourceId = findFreeTimeSlot(); - markTimeSlotBusy(resourceId); - break; - case SPACE_SLOT: - resourceId = findFreeSpaceSlot(); - markSpaceSlotBusy(resourceId); - break; - default: - System.out.println("ERROR: Attempted to allocate invalid resource"); - resourceId = INVALID_RESOURCE_ID; - break; - } - return resourceId; - } - - public void free(ResourceType resourceType, int resourceId) - { - switch (resourceType) { - case TIME_SLOT: - markTimeSlotFree(resourceId); - break; - case SPACE_SLOT: - markSpaceSlotFree(resourceId); - break; - default: - System.out.println("ERROR: attempted to free invalid resource"); - break; - } - } - - private void markSpaceSlotFree(int resourceId) - { - } - - private void markTimeSlotFree(int resourceId) - { - } - - private void markSpaceSlotBusy(int resourceId) - { - } - - private int findFreeSpaceSlot() - { - return 0; - } - - private void markTimeSlotBusy(int resourceId) - { - } - - private int findFreeTimeSlot() - { - return 0; - } -} diff --git a/OCP/src/com/directi/training/ocp/exercise/ResourceType.java b/OCP/src/com/directi/training/ocp/exercise/ResourceType.java deleted file mode 100644 index 721b355..0000000 --- a/OCP/src/com/directi/training/ocp/exercise/ResourceType.java +++ /dev/null @@ -1,14 +0,0 @@ -package com.directi.training.ocp.exercise; - -/** - * Created by IntelliJ IDEA. - * User: goyalamit - * Date: Jul 11, 2011 - * Time: 1:17:04 PM - * To change this template use File | Settings | File Templates. - */ -public enum ResourceType -{ - TIME_SLOT, - SPACE_SLOT -} diff --git a/SRP/src/com/directi/training/srp/exercise/Car.java b/SRP/src/com/directi/training/srp/exercise/Car.java deleted file mode 100644 index 45469b6..0000000 --- a/SRP/src/com/directi/training/srp/exercise/Car.java +++ /dev/null @@ -1,30 +0,0 @@ -package com.directi.training.srp.exercise; - -public class Car -{ - private final String _id; - private final String _model; - private final String _brand; - - public Car(String id, String model, String brand) - { - _id = id; - _model = model; - _brand = brand; - } - - public String getId() - { - return _id; - } - - public String getModel() - { - return _model; - } - - public String getBrand() - { - return _brand; - } -} diff --git a/SRP/src/com/directi/training/srp/exercise/CarManager.java b/SRP/src/com/directi/training/srp/exercise/CarManager.java deleted file mode 100644 index a06a58a..0000000 --- a/SRP/src/com/directi/training/srp/exercise/CarManager.java +++ /dev/null @@ -1,44 +0,0 @@ -package com.directi.training.srp.exercise; - -import java.util.Arrays; -import java.util.List; - -public class CarManager -{ - private List _carsDb = Arrays - .asList(new Car("1", "Golf III", "Volkswagen"), new Car("2", "Multipla", "Fiat"), - new Car("3", "Megane", "Renault")); - - public Car getFromDb(final String carId) - { - for (Car car : _carsDb) { - if (car.getId().equals(carId)) { - return car; - } - } - return null; - } - - public String getCarsNames() - { - StringBuilder sb = new StringBuilder(); - for (Car car : _carsDb) { - sb.append(car.getBrand()); - sb.append(" "); - sb.append(car.getModel()); - sb.append(", "); - } - return sb.substring(0, sb.length() - 2); - } - - public Car getBestCar() - { - Car bestCar = null; - for (Car car : _carsDb) { - if (bestCar == null || car.getModel().compareTo(bestCar.getModel()) > 0) { - bestCar = car; - } - } - return bestCar; - } -}