├── .gitignore ├── AdapterDesignPattern └── src │ └── main │ └── java │ ├── Bird.java │ ├── BirdAdapter.java │ ├── Client.java │ ├── Duck.java │ ├── PlasticToyBird.java │ └── WinnieThePenguin.java ├── BridgeDesignPattern └── src │ └── main │ └── java │ ├── Client.java │ ├── Color.java │ ├── GreenColor.java │ ├── Rectangle.java │ ├── RedColor.java │ ├── Shape.java │ └── Square.java ├── BuilderDesignPattern └── src │ └── main │ └── java │ ├── HumonoidRobotBuilder.java │ ├── Main.java │ ├── RCCarBotBuilder.java │ ├── Robot.java │ └── RobotBuilder.java ├── ChainOfResponsibilityDesignPattern └── src │ └── main │ └── java │ ├── AbstractHandler.java │ ├── AmbulanceHandler.java │ ├── CivilianVehicleHandler.java │ ├── FireTruckHandler.java │ ├── Handler.java │ ├── Main.java │ ├── MainWithChainOfResponsibility.java │ └── PoliceVehicleHandler.java ├── CommandDesignPattern └── src │ └── main │ └── java │ ├── Client.java │ ├── Command.java │ ├── Invoker.java │ ├── Light.java │ ├── Main.java │ ├── Motor.java │ ├── Receiver.java │ └── TurnOnCommand.java ├── DecoratorDesignPattern └── src │ └── main │ └── java │ ├── ClientDemo.java │ ├── CompressedFileReader.java │ ├── EncryptedFileReader.java │ ├── FileReader.java │ ├── ReadDecorator.java │ └── Reader.java ├── FacadeDesignPattern └── src │ └── main │ └── java │ ├── Circle.java │ ├── FacadePatternDemo.java │ ├── Rectangle.java │ ├── ShapeMakerFacade.java │ ├── ShapeSubSystem.java │ └── Square.java ├── FlyweightDesignPattern └── src │ └── main │ ├── java │ ├── Bullet.java │ ├── BulletFactory.java │ ├── BulletType.java │ ├── Gun.java │ ├── Hunter.java │ └── Shotgun.java │ └── main.iml ├── IteratorDesignPattern └── src │ └── main │ └── java │ ├── CustomIterableCollection.java │ ├── CustomIterableCollectionIterator.java │ ├── IterableCollection.java │ ├── Iterator.java │ └── Main.java ├── MediatorDesignPattern └── src │ └── main │ └── java │ ├── Component.java │ ├── ComponentA.java │ ├── ComponentB.java │ ├── ConcreteMediator.java │ ├── Main.java │ └── Mediator.java ├── MementoDesignPattern └── src │ └── main │ └── java │ ├── CareTaker.java │ ├── Main.java │ ├── Memento.java │ └── Originator.java ├── ObserverDesignPattern └── src │ └── main │ └── java │ ├── Main.java │ ├── Observer.java │ ├── Person.java │ ├── RadioStation.java │ └── Subject.java ├── PrototypeDesignPattern └── src │ └── main │ └── java │ ├── ConcretePrototype.java │ ├── Main.java │ ├── Prototype.java │ └── PrototypeRegistry.java ├── ProxyDesignPattern └── src │ └── main │ └── java │ ├── ActualService.java │ ├── Client.java │ ├── ServiceInterface.java │ └── ServiceProxy.java ├── README.md ├── StateDesignPattern └── src │ └── main │ └── java │ ├── LightBulb.java │ ├── Main.java │ ├── OffState.java │ ├── OnState.java │ └── State.java ├── StrategyDesignPattern └── src │ └── main │ └── java │ ├── BubbleSortStrategy.java │ ├── Context.java │ ├── Demo.java │ ├── MergeSortStrategy.java │ └── Strategy.java ├── TemplateDesignPattern └── src │ └── main │ └── java │ ├── Main.java │ ├── PizzaMaker.java │ └── SimpleCheeseCornPizzaMaker.java └── VisitorDesignPattern └── src └── main └── java ├── badway ├── BoardBlock.java ├── ChessComponent.java ├── Main.java └── Piece.java └── goodway ├── BoardBlock.java ├── ChessComponent.java ├── ConsoleVisitor.java ├── Main.java ├── Piece.java └── Visitor.java /.gitignore: -------------------------------------------------------------------------------- 1 | .idea 2 | out 3 | *.iml 4 | .DS_Store -------------------------------------------------------------------------------- /AdapterDesignPattern/src/main/java/Bird.java: -------------------------------------------------------------------------------- 1 | public interface Bird { 2 | void makeSound(); 3 | } -------------------------------------------------------------------------------- /AdapterDesignPattern/src/main/java/BirdAdapter.java: -------------------------------------------------------------------------------- 1 | public class BirdAdapter implements Bird { 2 | private PlasticToyBird toyBird; 3 | 4 | public BirdAdapter(PlasticToyBird toyBird) { 5 | this.toyBird = toyBird; 6 | } 7 | 8 | public void makeSound() { 9 | this.toyBird.squeak(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /AdapterDesignPattern/src/main/java/Client.java: -------------------------------------------------------------------------------- 1 | public class Client { 2 | public static void main(String[] args) { 3 | Bird duck = new Duck(); 4 | duck.makeSound(); 5 | 6 | Bird toyBird = new BirdAdapter(new WinnieThePenguin()); 7 | toyBird.makeSound(); 8 | } 9 | } -------------------------------------------------------------------------------- /AdapterDesignPattern/src/main/java/Duck.java: -------------------------------------------------------------------------------- 1 | public class Duck implements Bird { 2 | public void makeSound() { 3 | System.out.println("Duck is making sound."); 4 | } 5 | } -------------------------------------------------------------------------------- /AdapterDesignPattern/src/main/java/PlasticToyBird.java: -------------------------------------------------------------------------------- 1 | public interface PlasticToyBird { 2 | void squeak(); 3 | } -------------------------------------------------------------------------------- /AdapterDesignPattern/src/main/java/WinnieThePenguin.java: -------------------------------------------------------------------------------- 1 | public class WinnieThePenguin implements PlasticToyBird { 2 | public void squeak() { 3 | System.out.println("Squeaking."); 4 | } 5 | } -------------------------------------------------------------------------------- /BridgeDesignPattern/src/main/java/Client.java: -------------------------------------------------------------------------------- 1 | 2 | 3 | public class Client { 4 | public static void main(String[] args) { 5 | RedColor redColor = new RedColor(); 6 | GreenColor greenColor = new GreenColor(); 7 | 8 | Shape square = new Square(4, redColor); 9 | square.paintShape(); 10 | 11 | square.setColor(greenColor); 12 | square.paintShape(); 13 | 14 | Rectangle rectangle = new Rectangle(6, 3, greenColor); 15 | rectangle.paintShape(); 16 | } 17 | 18 | public static void badEg() { 19 | Shape square = new Square(4, new RedColor()); 20 | square.paintShape(); 21 | 22 | square.setColor(new GreenColor()); 23 | square.paintShape(); 24 | 25 | Rectangle rectangle = new Rectangle(4, 8, new GreenColor()); 26 | rectangle.paintShape(); 27 | } 28 | } -------------------------------------------------------------------------------- /BridgeDesignPattern/src/main/java/Color.java: -------------------------------------------------------------------------------- 1 | public interface Color { 2 | String fillColor(); 3 | } -------------------------------------------------------------------------------- /BridgeDesignPattern/src/main/java/GreenColor.java: -------------------------------------------------------------------------------- 1 | public class GreenColor implements Color { 2 | public String fillColor() { 3 | return "Solid green color"; 4 | } 5 | } -------------------------------------------------------------------------------- /BridgeDesignPattern/src/main/java/Rectangle.java: -------------------------------------------------------------------------------- 1 | public class Rectangle extends Shape { 2 | private int length; 3 | private int breadth; 4 | 5 | public Rectangle(int length, int breadth, Color colorType) { 6 | super(colorType); 7 | this.length = length; 8 | this.breadth = breadth; 9 | this.shapeType = "rectangle"; 10 | } 11 | 12 | @Override 13 | public void computeArea() { 14 | this.area = length * breadth; 15 | } 16 | } -------------------------------------------------------------------------------- /BridgeDesignPattern/src/main/java/RedColor.java: -------------------------------------------------------------------------------- 1 | public class RedColor implements Color { 2 | public RedColor() {} 3 | 4 | public String fillColor() { 5 | return "Solid red color"; 6 | } 7 | } -------------------------------------------------------------------------------- /BridgeDesignPattern/src/main/java/Shape.java: -------------------------------------------------------------------------------- 1 | public abstract class Shape { 2 | protected int area; 3 | protected String shapeType; 4 | 5 | protected Color color; 6 | 7 | public Shape(Color color) { 8 | this.color = color; 9 | } 10 | 11 | public int getArea() { 12 | return this.area; 13 | } 14 | 15 | public void setColor(Color color) { 16 | this.color = color; 17 | } 18 | 19 | public abstract void computeArea(); 20 | 21 | public void paintShape() { 22 | System.out.println("Painting " + this.shapeType + " with color " + this.color.fillColor()); 23 | } 24 | } -------------------------------------------------------------------------------- /BridgeDesignPattern/src/main/java/Square.java: -------------------------------------------------------------------------------- 1 | public class Square extends Shape { 2 | private int sideLength; 3 | 4 | public Square(int sideLength, Color colorType) { 5 | super(colorType); 6 | this.sideLength = sideLength; 7 | this.shapeType = "square"; 8 | } 9 | 10 | @Override 11 | public void computeArea() { 12 | this.area = sideLength * sideLength; 13 | } 14 | } -------------------------------------------------------------------------------- /BuilderDesignPattern/src/main/java/HumonoidRobotBuilder.java: -------------------------------------------------------------------------------- 1 | public class HumonoidRobotBuilder implements RobotBuilder { 2 | 3 | private Robot robot; 4 | 5 | public HumonoidRobotBuilder() { } 6 | 7 | @Override 8 | public void initBot() { 9 | this.robot = new Robot(); 10 | } 11 | 12 | @Override 13 | public void assembleArms() { 14 | this.robot.setArms("Setting arms"); 15 | } 16 | 17 | @Override 18 | public void assembleLegs() { 19 | this.robot.setLegs("Setting legs"); 20 | } 21 | 22 | @Override 23 | public void assembleWheels() { 24 | this.robot.setWheels("Wheels not required"); 25 | } 26 | 27 | @Override 28 | public void assembleHead() { 29 | this.robot.setHead("Setting head."); 30 | } 31 | 32 | @Override 33 | public void assembleFireArms() { 34 | this.robot.setFireArms("Setting fire-arms."); 35 | } 36 | 37 | @Override 38 | public void assembleSensors() { 39 | this.robot.setSensors("Setting sensors."); 40 | } 41 | 42 | @Override 43 | public void assembleDoors() { 44 | this.robot.setDoors("Setting doors."); 45 | } 46 | 47 | @Override 48 | public Robot build() { 49 | return this.robot; 50 | } 51 | } 52 | -------------------------------------------------------------------------------- /BuilderDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | RobotBuilder builder = new HumonoidRobotBuilder(); 4 | builder.initBot(); 5 | builder.assembleArms(); 6 | builder.assembleLegs(); 7 | builder.assembleHead(); 8 | builder.assembleFireArms(); 9 | 10 | Robot sofia = builder.build(); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /BuilderDesignPattern/src/main/java/RCCarBotBuilder.java: -------------------------------------------------------------------------------- 1 | public class RCCarBotBuilder implements RobotBuilder { 2 | 3 | private Robot robot; 4 | 5 | public RCCarBotBuilder() { 6 | 7 | } 8 | 9 | @Override 10 | public void initBot() { 11 | this.robot = new Robot(); 12 | } 13 | 14 | @Override 15 | public void assembleArms() { 16 | this.robot.setArms("No arms for rc bot required..."); 17 | } 18 | 19 | @Override 20 | public void assembleLegs() { 21 | this.robot.setLegs("No legs for rc bot required..."); 22 | } 23 | 24 | @Override 25 | public void assembleWheels() { 26 | this.robot.setWheels("Assembling wheels..."); 27 | } 28 | 29 | @Override 30 | public void assembleHead() { 31 | this.robot.setHead("No head required for rc bot..."); 32 | } 33 | 34 | @Override 35 | public void assembleFireArms() { 36 | this.robot.setFireArms("Setting firearms..."); 37 | } 38 | 39 | @Override 40 | public void assembleSensors() { 41 | this.robot.setSensors("Setting sensors..."); 42 | } 43 | 44 | @Override 45 | public void assembleDoors() { 46 | this.robot.setDoors("Setting doors..."); 47 | } 48 | 49 | @Override 50 | public Robot build() { 51 | return this.robot; 52 | } 53 | } 54 | -------------------------------------------------------------------------------- /BuilderDesignPattern/src/main/java/Robot.java: -------------------------------------------------------------------------------- 1 | public class Robot { 2 | private String arms; 3 | private String legs; 4 | private String head; 5 | private String doors; 6 | 7 | private String wheels; 8 | private String sensors; 9 | private String fireArms; 10 | 11 | public String getArms() { 12 | return this.arms; 13 | } 14 | 15 | public void setArms(String arms) { 16 | System.out.println("Setting arms"); 17 | this.arms = arms; 18 | } 19 | 20 | public String getLegs() { 21 | return this.legs; 22 | } 23 | 24 | public void setLegs(String legs) { 25 | System.out.println("Setting legs"); 26 | this.legs = legs; 27 | } 28 | 29 | public String getHead() { 30 | return this.legs; 31 | } 32 | 33 | public void setHead(String head) { 34 | System.out.println("Setting head"); 35 | this.head = head; 36 | } 37 | 38 | public String getDoors() { 39 | return this.doors; 40 | } 41 | 42 | public void setDoors(String doors) { 43 | System.out.println("Setting doors"); 44 | this.doors = doors; 45 | } 46 | 47 | public String getWheels() { 48 | return this.wheels; 49 | } 50 | 51 | public void setWheels(String wheels) { 52 | this.wheels = wheels; 53 | } 54 | 55 | public String getSensors() { 56 | return this.sensors; 57 | } 58 | 59 | public void setSensors(String sensors) { 60 | this.sensors = sensors; 61 | } 62 | 63 | public String getFireArms() { 64 | return this.fireArms; 65 | } 66 | 67 | public void setFireArms(String fireArms) { 68 | System.out.println("Setting fire-arms"); 69 | this.fireArms = fireArms; 70 | } 71 | } -------------------------------------------------------------------------------- /BuilderDesignPattern/src/main/java/RobotBuilder.java: -------------------------------------------------------------------------------- 1 | public interface RobotBuilder { 2 | void initBot(); 3 | void assembleArms(); 4 | void assembleLegs(); 5 | void assembleWheels(); 6 | void assembleHead(); 7 | void assembleFireArms(); 8 | void assembleSensors(); 9 | void assembleDoors(); 10 | Robot build(); 11 | } -------------------------------------------------------------------------------- /ChainOfResponsibilityDesignPattern/src/main/java/AbstractHandler.java: -------------------------------------------------------------------------------- 1 | public abstract class AbstractHandler implements Handler { 2 | protected Handler nextHandler; 3 | 4 | public void setNextHandler(Handler handler) { 5 | this.nextHandler = handler; 6 | } 7 | } -------------------------------------------------------------------------------- /ChainOfResponsibilityDesignPattern/src/main/java/AmbulanceHandler.java: -------------------------------------------------------------------------------- 1 | public class AmbulanceHandler extends AbstractHandler { 2 | 3 | @Override 4 | public void handle(String incomingSuspect) { 5 | 6 | if ("AMBULANCE".equals(incomingSuspect)) { 7 | System.out.println("Validate id quickly."); 8 | System.out.println("Let go."); 9 | return; 10 | } 11 | this.nextHandler.handle(incomingSuspect); 12 | } 13 | } -------------------------------------------------------------------------------- /ChainOfResponsibilityDesignPattern/src/main/java/CivilianVehicleHandler.java: -------------------------------------------------------------------------------- 1 | public class CivilianVehicleHandler extends AbstractHandler { 2 | 3 | @Override 4 | public void handle(String incomingSuspect) { 5 | 6 | if ("CIVILIAN_SUV".equals(incomingSuspect)) { 7 | System.out.println("Validate id."); 8 | System.out.println("Do exhaustive search."); 9 | System.out.println("Minor interrogation."); 10 | return; 11 | } 12 | this.nextHandler.handle(incomingSuspect); 13 | } 14 | } -------------------------------------------------------------------------------- /ChainOfResponsibilityDesignPattern/src/main/java/FireTruckHandler.java: -------------------------------------------------------------------------------- 1 | public class FireTruckHandler extends AbstractHandler { 2 | 3 | @Override 4 | public void handle(String incomingSuspect) { 5 | if ("FIRE_TRUCK".equals(incomingSuspect)) { 6 | System.out.println("Validate id."); 7 | return; 8 | } 9 | this.nextHandler.handle(incomingSuspect); 10 | } 11 | } -------------------------------------------------------------------------------- /ChainOfResponsibilityDesignPattern/src/main/java/Handler.java: -------------------------------------------------------------------------------- 1 | public interface Handler { 2 | void setNextHandler(Handler handler); 3 | void handle(String incomingSuspect); 4 | } -------------------------------------------------------------------------------- /ChainOfResponsibilityDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | String incomingSuspect = "CIVILIAN_SUV"; 4 | if (incomingSuspect.equals("CIVILIAN_SUV")) { 5 | System.out.println("Validate id."); 6 | System.out.println("Do exhaustive search."); 7 | System.out.println("Minor interrogation."); 8 | } else if (incomingSuspect.equals("FIRE_TRUCK")) { 9 | System.out.println("Validate id."); 10 | } else if (incomingSuspect.equals("AMBULANCE")) { 11 | System.out.println("Validate id quickly."); 12 | System.out.println("Let go."); 13 | } else if (incomingSuspect.equals("POLICE_SUV")) { 14 | System.out.println("validate id."); 15 | System.out.println("Give access."); 16 | } else { 17 | System.out.println("Stop right there."); 18 | } 19 | } 20 | } -------------------------------------------------------------------------------- /ChainOfResponsibilityDesignPattern/src/main/java/MainWithChainOfResponsibility.java: -------------------------------------------------------------------------------- 1 | public class MainWithChainOfResponsibility { 2 | public static void main(String[] args) { 3 | MainWithChainOfResponsibility demo = new MainWithChainOfResponsibility(); 4 | Handler vehicleHandler = demo.getValidationHandler(); 5 | String incomingSuspect = "POLICE_SUV"; 6 | vehicleHandler.handle(incomingSuspect); 7 | } 8 | 9 | public Handler getValidationHandler() { 10 | Handler civilianHandler = new CivilianVehicleHandler(); 11 | Handler fireTruckHandler = new FireTruckHandler(); 12 | Handler ambulanceHandler = new AmbulanceHandler(); 13 | Handler policHandler = new PoliceVehicleHandler(); 14 | 15 | civilianHandler.setNextHandler(fireTruckHandler); 16 | fireTruckHandler.setNextHandler(ambulanceHandler); 17 | ambulanceHandler.setNextHandler(policHandler); 18 | 19 | return civilianHandler; 20 | } 21 | } -------------------------------------------------------------------------------- /ChainOfResponsibilityDesignPattern/src/main/java/PoliceVehicleHandler.java: -------------------------------------------------------------------------------- 1 | public class PoliceVehicleHandler extends AbstractHandler { 2 | 3 | @Override 4 | public void handle(String incomingSuspect) { 5 | 6 | if ("POLICE_SUV".equals(incomingSuspect)) { 7 | System.out.println("validate id."); 8 | System.out.println("Give access."); 9 | return; 10 | } 11 | this.nextHandler.handle(incomingSuspect); 12 | } 13 | } -------------------------------------------------------------------------------- /CommandDesignPattern/src/main/java/Client.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.ArrayList; 3 | 4 | public class Client { 5 | 6 | private Invoker invoker; 7 | private List commandList; 8 | public static void main(String[] args) { 9 | Client client = new Client(); 10 | client.run(); 11 | } 12 | 13 | public void run() { 14 | this.commandList = new ArrayList<>(); 15 | Receiver receiver = new Light("Flash light"); 16 | Command command = new TurnOnCommand(receiver); 17 | 18 | commandList.add(command); 19 | 20 | invoker = new Invoker(command); 21 | invoker.executeCommand(); 22 | 23 | Receiver receiver2 = new Motor("Motor"); 24 | Command command2 = new TurnOnCommand(receiver2); 25 | commandList.add(command2); 26 | 27 | invoker.setCommand(command2); 28 | invoker.executeCommand(); 29 | 30 | commandList.forEach(commandBackup -> { 31 | invoker.setCommand(commandBackup); 32 | invoker.undoCommand(); 33 | }); 34 | } 35 | } -------------------------------------------------------------------------------- /CommandDesignPattern/src/main/java/Command.java: -------------------------------------------------------------------------------- 1 | public interface Command { 2 | void execute(); 3 | void undo(); 4 | } -------------------------------------------------------------------------------- /CommandDesignPattern/src/main/java/Invoker.java: -------------------------------------------------------------------------------- 1 | public class Invoker { 2 | private Command command; 3 | public Invoker(Command command) { 4 | this.command = command; 5 | } 6 | 7 | public void setCommand(Command command) { 8 | this.command = command; 9 | } 10 | 11 | public void executeCommand() { 12 | this.command.execute(); 13 | } 14 | 15 | public void undoCommand() { 16 | this.command.undo(); 17 | } 18 | } -------------------------------------------------------------------------------- /CommandDesignPattern/src/main/java/Light.java: -------------------------------------------------------------------------------- 1 | public class Light extends Receiver { 2 | public Light(String name) { 3 | super(name); 4 | } 5 | 6 | @Override 7 | public void on() { 8 | System.out.println("Light is on"); 9 | } 10 | 11 | @Override 12 | public void off() { 13 | System.out.println("Light is off"); 14 | } 15 | } -------------------------------------------------------------------------------- /CommandDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Chiranjivee/designpatternsjava/b9fd3d4c7b4c9baffca5ed478575144f3ee1fc5b/CommandDesignPattern/src/main/java/Main.java -------------------------------------------------------------------------------- /CommandDesignPattern/src/main/java/Motor.java: -------------------------------------------------------------------------------- 1 | public class Motor extends Receiver { 2 | public Motor(String name) { 3 | super(name); 4 | } 5 | 6 | @Override 7 | public void on() { 8 | System.out.println("Motor is on"); 9 | } 10 | 11 | @Override 12 | public void off() { 13 | System.out.println("Motor is off"); 14 | } 15 | } -------------------------------------------------------------------------------- /CommandDesignPattern/src/main/java/Receiver.java: -------------------------------------------------------------------------------- 1 | public abstract class Receiver { 2 | protected String name; 3 | 4 | public Receiver(String name) { 5 | this.name = name; 6 | } 7 | 8 | abstract void on(); 9 | abstract void off(); 10 | } -------------------------------------------------------------------------------- /CommandDesignPattern/src/main/java/TurnOnCommand.java: -------------------------------------------------------------------------------- 1 | public class TurnOnCommand implements Command { 2 | private Receiver receiver; 3 | 4 | public TurnOnCommand(Receiver receiver) { 5 | this.receiver = receiver; 6 | } 7 | 8 | public void execute() { 9 | receiver.on(); 10 | } 11 | 12 | public void undo() { 13 | receiver.off(); 14 | } 15 | } -------------------------------------------------------------------------------- /DecoratorDesignPattern/src/main/java/ClientDemo.java: -------------------------------------------------------------------------------- 1 | public class ClientDemo { 2 | public static void main(String[] args) { 3 | 4 | Reader fileReader = new FileReader("C:/test.txt"); 5 | fileReader.read(); 6 | 7 | System.out.println("============"); 8 | System.out.println(); 9 | 10 | Reader compressedFileReader = new CompressedFileReader(fileReader); 11 | compressedFileReader.read(); 12 | 13 | System.out.println("========================"); 14 | System.out.println(); 15 | 16 | Reader encryptedFileReader = new EncryptedFileReader(fileReader); 17 | encryptedFileReader.read(); 18 | 19 | System.out.println("========================"); 20 | System.out.println(); 21 | 22 | Reader encryptedCompressedFileReader = 23 | new EncryptedFileReader( 24 | new CompressedFileReader( 25 | new FileReader("/test/resources/test.txt") 26 | ) 27 | ); 28 | 29 | encryptedCompressedFileReader.read(); 30 | } 31 | } -------------------------------------------------------------------------------- /DecoratorDesignPattern/src/main/java/CompressedFileReader.java: -------------------------------------------------------------------------------- 1 | public class CompressedFileReader extends ReadDecorator { 2 | public CompressedFileReader(Reader reader) { 3 | super(reader); 4 | } 5 | 6 | public void read() { 7 | System.out.println("Decompressing the file"); 8 | this.reader.read(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /DecoratorDesignPattern/src/main/java/EncryptedFileReader.java: -------------------------------------------------------------------------------- 1 | public class EncryptedFileReader extends ReadDecorator { 2 | public EncryptedFileReader(Reader reader) { 3 | super(reader); 4 | } 5 | 6 | public void read() { 7 | System.out.println("Decrypting the file"); 8 | this.reader.read(); 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /DecoratorDesignPattern/src/main/java/FileReader.java: -------------------------------------------------------------------------------- 1 | public class FileReader implements Reader { 2 | 3 | private String filePath; 4 | 5 | public FileReader(String filePath) { 6 | this.filePath = filePath; 7 | } 8 | 9 | public void read() { 10 | System.out.println("Reading file from: " + this.filePath); 11 | } 12 | } -------------------------------------------------------------------------------- /DecoratorDesignPattern/src/main/java/ReadDecorator.java: -------------------------------------------------------------------------------- 1 | public abstract class ReadDecorator implements Reader { 2 | 3 | protected Reader reader; 4 | 5 | public ReadDecorator(Reader reader) { 6 | this.reader = reader; 7 | } 8 | } -------------------------------------------------------------------------------- /DecoratorDesignPattern/src/main/java/Reader.java: -------------------------------------------------------------------------------- 1 | public interface Reader { 2 | void read(); 3 | } -------------------------------------------------------------------------------- /FacadeDesignPattern/src/main/java/Circle.java: -------------------------------------------------------------------------------- 1 | public class Circle implements ShapeSubSystem { 2 | @Override 3 | public void draw() { 4 | System.out.println("Drawing a circle."); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /FacadeDesignPattern/src/main/java/FacadePatternDemo.java: -------------------------------------------------------------------------------- 1 | public class FacadePatternDemo { 2 | public static void main(String[] args) { 3 | ShapeMakerFacade facade = new ShapeMakerFacade(); 4 | facade.drawCircle(); 5 | facade.drawRectangle(); 6 | facade.drawSquare(); 7 | } 8 | } -------------------------------------------------------------------------------- /FacadeDesignPattern/src/main/java/Rectangle.java: -------------------------------------------------------------------------------- 1 | public class Rectangle implements ShapeSubSystem { 2 | @Override 3 | public void draw() { 4 | System.out.println("Drawing a rectangle."); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /FacadeDesignPattern/src/main/java/ShapeMakerFacade.java: -------------------------------------------------------------------------------- 1 | public class ShapeMakerFacade { 2 | private ShapeSubSystem circle; 3 | private ShapeSubSystem square; 4 | private ShapeSubSystem rectangle; 5 | 6 | public ShapeMakerFacade() { 7 | this.circle = new Circle(); 8 | this.square = new Square(); 9 | this.rectangle = new Rectangle(); 10 | } 11 | 12 | public void drawCircle() { 13 | this.circle.draw(); 14 | } 15 | 16 | public void drawSquare() { 17 | this.square.draw(); 18 | } 19 | 20 | public void drawRectangle() { 21 | this.rectangle.draw(); 22 | } 23 | } -------------------------------------------------------------------------------- /FacadeDesignPattern/src/main/java/ShapeSubSystem.java: -------------------------------------------------------------------------------- 1 | public interface ShapeSubSystem { 2 | void draw(); 3 | } -------------------------------------------------------------------------------- /FacadeDesignPattern/src/main/java/Square.java: -------------------------------------------------------------------------------- 1 | public class Square implements ShapeSubSystem { 2 | @Override 3 | public void draw() { 4 | System.out.println("Drawing a square."); 5 | } 6 | } 7 | -------------------------------------------------------------------------------- /FlyweightDesignPattern/src/main/java/Bullet.java: -------------------------------------------------------------------------------- 1 | public final class Bullet { 2 | private final String bulletSize; 3 | private final String bulletWeight; 4 | 5 | public Bullet(String size, String weight) { 6 | this.bulletSize = size; 7 | this.bulletWeight = weight; 8 | } 9 | 10 | public String getBulletSize() { 11 | return this.bulletSize; 12 | } 13 | 14 | public String getBulletWeight() { 15 | return this.bulletWeight; 16 | } 17 | } -------------------------------------------------------------------------------- /FlyweightDesignPattern/src/main/java/BulletFactory.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class BulletFactory { 5 | private static Map bulletCache = new HashMap<>(); 6 | 7 | public static Bullet getBulletByType(BulletType type) throws IllegalArgumentException 8 | { 9 | switch(type) { 10 | case PISTOL_BULLET: 11 | return new Bullet("S", "50mg"); 12 | case SHOTGUN_BULLET: 13 | return new Bullet("M", "80mg"); 14 | case SNIPER_BULLET: 15 | return new Bullet("L", "100mg"); 16 | default: 17 | throw new IllegalArgumentException("Invalid bullet type"); 18 | } 19 | } 20 | 21 | public static Bullet getBulletFromCache(BulletType type) { 22 | if (bulletCache.containsKey(type)) { 23 | return bulletCache.get(type); 24 | } 25 | 26 | Bullet bullet = getBulletByType(type); 27 | bulletCache.put(type, bullet); 28 | return bulletCache.get(type); 29 | } 30 | } -------------------------------------------------------------------------------- /FlyweightDesignPattern/src/main/java/BulletType.java: -------------------------------------------------------------------------------- 1 | public enum BulletType { 2 | PISTOL_BULLET("MG 45 Bullet"), 3 | SHOTGUN_BULLET("Coey 84 Bullet"), 4 | SNIPER_BULLET("M 14 Bullet"); 5 | 6 | private String name; 7 | 8 | BulletType(String name) { 9 | this.name = name; 10 | } 11 | } -------------------------------------------------------------------------------- /FlyweightDesignPattern/src/main/java/Gun.java: -------------------------------------------------------------------------------- 1 | public abstract class Gun { 2 | protected Bullet[] bullets; 3 | protected int capacity; 4 | protected final BulletType bulletType; 5 | 6 | public Gun(int capacity, BulletType type) { 7 | this.capacity = capacity; 8 | this.bulletType = type; 9 | this.bullets = new Bullet[capacity]; 10 | } 11 | 12 | public abstract void shoot(); 13 | 14 | public abstract void loadBullets(); 15 | 16 | public abstract void loadUncachedBullets(); 17 | } -------------------------------------------------------------------------------- /FlyweightDesignPattern/src/main/java/Hunter.java: -------------------------------------------------------------------------------- 1 | public class Hunter { 2 | public static void main(String[] args) { 3 | System.out.println("Shooting with cached bullets"); 4 | Gun shotGun = new Shotgun(8, BulletType.SHOTGUN_BULLET); 5 | shotGun.loadBullets(); 6 | shotGun.shoot(); 7 | 8 | System.out.println("Shooting with un-cached bullets"); 9 | Gun shotgunUncached = new Shotgun(8, BulletType.SHOTGUN_BULLET); 10 | shotgunUncached.loadUncachedBullets(); 11 | shotgunUncached.shoot(); 12 | } 13 | } -------------------------------------------------------------------------------- /FlyweightDesignPattern/src/main/java/Shotgun.java: -------------------------------------------------------------------------------- 1 | public class Shotgun extends Gun { 2 | public Shotgun (int capacity, BulletType type) { 3 | super(capacity, type); 4 | } 5 | 6 | @Override 7 | public void shoot() { 8 | for (int i = 0; i < this.capacity; i++) { 9 | System.out.println(this.bullets[i].toString()); 10 | } 11 | } 12 | 13 | @Override 14 | public void loadBullets() { 15 | for (int i = 0; i < this.bullets.length; i++) { 16 | bullets[i] = BulletFactory.getBulletByType(this.bulletType); 17 | } 18 | } 19 | 20 | @Override 21 | public void loadUncachedBullets() { 22 | for (int i = 0; i < this.bullets.length; i++) { 23 | bullets[i] = BulletFactory.getBulletFromCache(this.bulletType); 24 | } 25 | } 26 | } 27 | -------------------------------------------------------------------------------- /FlyweightDesignPattern/src/main/main.iml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | -------------------------------------------------------------------------------- /IteratorDesignPattern/src/main/java/CustomIterableCollection.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.ArrayList; 3 | 4 | public class CustomIterableCollection implements IterableCollection { 5 | private List internalList = new ArrayList<>(); 6 | 7 | public Iterator createIterator() { 8 | return new CustomIterableCollectionIterator(this); 9 | } 10 | 11 | public CustomIterableCollection() { 12 | internalList.add("I"); 13 | internalList.add("love"); 14 | internalList.add("binary."); 15 | } 16 | public int size() { 17 | return this.internalList.size(); 18 | } 19 | 20 | public String getStringAtIndex(int i) { 21 | return this.internalList.get(i); 22 | } 23 | } -------------------------------------------------------------------------------- /IteratorDesignPattern/src/main/java/CustomIterableCollectionIterator.java: -------------------------------------------------------------------------------- 1 | public class CustomIterableCollectionIterator implements Iterator { 2 | 3 | 4 | private CustomIterableCollection collection; 5 | private int size; 6 | private int currentIndex = -1; 7 | public CustomIterableCollectionIterator(CustomIterableCollection collection) { 8 | this.collection = collection; 9 | this.size = collection.size(); 10 | } 11 | 12 | @Override 13 | public String getNext() { 14 | currentIndex++; 15 | return this.collection.getStringAtIndex(currentIndex); 16 | } 17 | 18 | @Override 19 | public boolean hasMore() { 20 | return currentIndex + 1 < size; 21 | } 22 | } -------------------------------------------------------------------------------- /IteratorDesignPattern/src/main/java/IterableCollection.java: -------------------------------------------------------------------------------- 1 | public interface IterableCollection { 2 | Iterator createIterator(); 3 | } -------------------------------------------------------------------------------- /IteratorDesignPattern/src/main/java/Iterator.java: -------------------------------------------------------------------------------- 1 | public interface Iterator { 2 | String getNext(); 3 | boolean hasMore(); 4 | } -------------------------------------------------------------------------------- /IteratorDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String [] args) { 3 | IterableCollection coll = new CustomIterableCollection(); 4 | Iterator iter = coll.createIterator(); 5 | 6 | int i = 0; 7 | while (iter.hasMore()) { 8 | String result = iter.getNext(); 9 | System.out.println(++i + "> " + result); 10 | } 11 | } 12 | } -------------------------------------------------------------------------------- /MediatorDesignPattern/src/main/java/Component.java: -------------------------------------------------------------------------------- 1 | public abstract class Component { 2 | private String name; 3 | protected Mediator mediator; 4 | 5 | public Component(String name, Mediator m) { 6 | this.mediator = m; 7 | this.name = name; 8 | } 9 | 10 | public abstract void send(); 11 | public abstract void receive(String message); 12 | 13 | public String getName() { 14 | return this.name; 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /MediatorDesignPattern/src/main/java/ComponentA.java: -------------------------------------------------------------------------------- 1 | public class ComponentA extends Component { 2 | 3 | public ComponentA(Mediator m) { 4 | super("Component-A", m); 5 | } 6 | 7 | @Override 8 | public void send() { 9 | 10 | String message = "I am good."; 11 | System.out.println("A is sending: " + message); 12 | this.mediator.notify(this, message); 13 | } 14 | 15 | @Override 16 | public void receive(String message) { 17 | System.out.println("Component A got: " + message); 18 | } 19 | 20 | } 21 | -------------------------------------------------------------------------------- /MediatorDesignPattern/src/main/java/ComponentB.java: -------------------------------------------------------------------------------- 1 | public class ComponentB extends Component { 2 | 3 | public ComponentB(Mediator m) { 4 | super("Component-B", m); 5 | } 6 | 7 | @Override 8 | public void send() { 9 | String message = "Hey!! What's up"; 10 | 11 | System.out.println("B is sending: " + message); 12 | 13 | this.mediator.notify(this, message); 14 | } 15 | 16 | @Override 17 | public void receive(String message) { 18 | System.out.println("Component B got: " + message); 19 | } 20 | 21 | 22 | } 23 | -------------------------------------------------------------------------------- /MediatorDesignPattern/src/main/java/ConcreteMediator.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class ConcreteMediator implements Mediator { 5 | 6 | private final String COMPONENT_A = "Component-A"; 7 | private final String COMPONENT_B = "Component-B"; 8 | 9 | private Map regCompMap = 10 | new HashMap<>(); 11 | 12 | @Override 13 | public void notify(Component sender, String message) { 14 | String senderName = sender.getName(); 15 | if (COMPONENT_A.equals(senderName)) { 16 | reactOnA(message); 17 | } else if (COMPONENT_B.equals(senderName)) { 18 | reactOnB(message); 19 | } 20 | } 21 | 22 | @Override 23 | public void register(Component component) { 24 | this.regCompMap.put(component.getName(), component); 25 | } 26 | 27 | private void reactOnA(String message) { 28 | System.out.println("Mediator is in action: "); 29 | regCompMap.get(COMPONENT_B).receive(message); 30 | } 31 | 32 | private void reactOnB(String message) { 33 | System.out.println("Mediator is in action: "); 34 | regCompMap.get(COMPONENT_A).receive(message); 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /MediatorDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String [] args) { 3 | Mediator mediator = new ConcreteMediator(); 4 | 5 | Component compA = new ComponentA(mediator); 6 | Component compB = new ComponentB(mediator); 7 | 8 | mediator.register(compA); 9 | mediator.register(compB); 10 | 11 | compA.send(); 12 | 13 | System.out.println(); 14 | 15 | compB.send(); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /MediatorDesignPattern/src/main/java/Mediator.java: -------------------------------------------------------------------------------- 1 | public interface Mediator { 2 | void notify(Component sender, String message); 3 | 4 | void register(Component component); 5 | } 6 | -------------------------------------------------------------------------------- /MementoDesignPattern/src/main/java/CareTaker.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class CareTaker { 4 | 5 | private ArrayList history; 6 | private int currState = -1; 7 | 8 | public CareTaker() { 9 | this.history = new ArrayList<>(); 10 | } 11 | 12 | public void addMemento(Memento m) { 13 | this.history.add(m); 14 | currState = this.history.size() - 1; 15 | } 16 | 17 | public Memento getMemento(int index) { 18 | return history.get(index); 19 | } 20 | 21 | public Memento undo() { 22 | System.out.println("Undoing state."); 23 | if (currState <= 0) { 24 | currState = 0; 25 | return getMemento(0); 26 | } 27 | currState--; 28 | return getMemento(currState); 29 | } 30 | 31 | public Memento redo() { 32 | System.out.println("Redoing state."); 33 | if (currState >= history.size() - 1) { 34 | currState = history.size() - 1; 35 | return getMemento(currState); 36 | } 37 | currState++; 38 | return getMemento(currState); 39 | } 40 | 41 | public int getStatesCount() { 42 | return history.size(); 43 | } 44 | } 45 | -------------------------------------------------------------------------------- /MementoDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | 3 | public static void main(String [] args) { 4 | 5 | Originator originator = new Originator(); 6 | CareTaker careTaker = new CareTaker(); 7 | 8 | originator.setArticle("State 1"); 9 | careTaker.addMemento(originator.save()); 10 | 11 | printState(originator); 12 | 13 | originator.setArticle("State 2"); 14 | careTaker.addMemento(originator.save()); 15 | printState(originator); 16 | 17 | originator.restore(careTaker.undo()); 18 | printState(originator); 19 | 20 | originator.restore(careTaker.undo()); 21 | printState(originator); 22 | 23 | originator.restore(careTaker.undo()); 24 | printState(originator); 25 | 26 | originator.restore(careTaker.redo()); 27 | printState(originator); 28 | 29 | originator.restore(careTaker.redo()); 30 | printState(originator); 31 | 32 | originator.setArticle("State 3"); 33 | careTaker.addMemento(originator.save()); 34 | printState(originator); 35 | originator.setArticle("State 4"); 36 | careTaker.addMemento(originator.save()); 37 | printState(originator); 38 | 39 | originator.setArticle("State 5"); 40 | careTaker.addMemento(originator.save()); 41 | printState(originator); 42 | 43 | originator.setArticle("State 6"); 44 | careTaker.addMemento(originator.save()); 45 | printState(originator); 46 | 47 | originator.restore(careTaker.undo()); 48 | originator.restore(careTaker.undo()); 49 | originator.restore(careTaker.undo()); 50 | 51 | printState(originator); 52 | 53 | originator.restore(careTaker.redo()); 54 | printState(originator); 55 | } 56 | 57 | public static void printState(Originator o) { 58 | System.out.println("Current State: " + o.getArticle()); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /MementoDesignPattern/src/main/java/Memento.java: -------------------------------------------------------------------------------- 1 | public class Memento { 2 | 3 | private String state; 4 | 5 | public Memento(String state) { 6 | this.state = state; 7 | } 8 | 9 | public void setState(String state) { 10 | this.state = state; 11 | } 12 | 13 | public String getState() { 14 | return this.state; 15 | } 16 | 17 | } 18 | -------------------------------------------------------------------------------- /MementoDesignPattern/src/main/java/Originator.java: -------------------------------------------------------------------------------- 1 | public class Originator { 2 | 3 | private String article; 4 | 5 | public Originator() {} 6 | 7 | public void setArticle(String article) { 8 | this.article = article; 9 | } 10 | 11 | public String getArticle() { 12 | return this.article; 13 | } 14 | 15 | public Memento save() { 16 | return new Memento(this.article); 17 | } 18 | 19 | public void restore(Memento m) { 20 | this.article = m.getState(); 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /ObserverDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | Subject radio = new RadioStation(); 4 | Observer ob1 = new Person("Vlad"); 5 | Observer ob2 = new Person("Niko"); 6 | Observer ob3 = new Person("Roman"); 7 | Observer ob4 = new Person("Faustin"); 8 | 9 | radio.notifyObserver(); 10 | radio.register(ob1); 11 | radio.register(ob2); 12 | 13 | radio.notifyObserver(); 14 | radio.register(ob3); 15 | ((RadioStation) radio).updateGoldenNumber(4); 16 | 17 | radio.unregister(ob1); 18 | radio.unregister(ob2); 19 | radio.register(ob4); 20 | 21 | ((RadioStation) radio).updateGoldenNumber(10); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /ObserverDesignPattern/src/main/java/Observer.java: -------------------------------------------------------------------------------- 1 | public interface Observer { 2 | void notifyMe(int x); 3 | } -------------------------------------------------------------------------------- /ObserverDesignPattern/src/main/java/Person.java: -------------------------------------------------------------------------------- 1 | class Person implements Observer { 2 | private String name; 3 | public Person(String name) { 4 | this.name = name; 5 | } 6 | 7 | @Override 8 | public void notifyMe(int x) { 9 | System.out.println("I got notified by: " + x); 10 | } 11 | } -------------------------------------------------------------------------------- /ObserverDesignPattern/src/main/java/RadioStation.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | import java.util.ArrayList; 3 | 4 | public class RadioStation implements Subject { 5 | 6 | private List myObservers = new ArrayList<>(); 7 | private int goldenNumber = 2; 8 | 9 | @Override 10 | public void register(Observer o) { 11 | myObservers.add(o); 12 | } 13 | 14 | @Override 15 | public void unregister(Observer o) { 16 | myObservers.remove(o); 17 | } 18 | 19 | @Override 20 | public void notifyObserver() { 21 | myObservers.stream().forEach((Observer o) -> o.notifyMe(goldenNumber)); 22 | } 23 | 24 | public void updateGoldenNumber(int i) { 25 | this.goldenNumber = i; 26 | notifyObserver(); 27 | } 28 | } -------------------------------------------------------------------------------- /ObserverDesignPattern/src/main/java/Subject.java: -------------------------------------------------------------------------------- 1 | public interface Subject { 2 | void register(Observer o); 3 | void unregister(Observer o); 4 | void notifyObserver(); 5 | } -------------------------------------------------------------------------------- /PrototypeDesignPattern/src/main/java/ConcretePrototype.java: -------------------------------------------------------------------------------- 1 | public class ConcretePrototype implements Prototype { 2 | private String name; 3 | 4 | public ConcretePrototype(String name) { 5 | this.name = name; 6 | } 7 | 8 | public ConcretePrototype(ConcretePrototype prototype) { 9 | this.name = prototype.name; 10 | } 11 | 12 | public String getName() { 13 | return this.name; 14 | } 15 | 16 | @Override 17 | public Prototype clone() { 18 | return new ConcretePrototype(this); 19 | } 20 | } -------------------------------------------------------------------------------- /PrototypeDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | ConcretePrototype prototype = new ConcretePrototype("Test name"); 4 | 5 | PrototypeRegistry registory = new PrototypeRegistry(); 6 | 7 | registory.addPrototypeToRegistory("Original", prototype); 8 | 9 | ConcretePrototype clone = (ConcretePrototype) registory.getPrototypeCloneFromPrototypeRegistory("Original"); 10 | 11 | System.out.println(prototype); 12 | System.out.println(clone); 13 | } 14 | } -------------------------------------------------------------------------------- /PrototypeDesignPattern/src/main/java/Prototype.java: -------------------------------------------------------------------------------- 1 | public interface Prototype { 2 | Prototype clone(); 3 | } -------------------------------------------------------------------------------- /PrototypeDesignPattern/src/main/java/PrototypeRegistry.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class PrototypeRegistry { 5 | private Map prototypeRegistry; 6 | 7 | public PrototypeRegistry() { 8 | this.prototypeRegistry = new HashMap<>(); 9 | } 10 | 11 | public void addPrototypeToRegistory(String prototypeName, Prototype prototype) { 12 | this.prototypeRegistry.put(prototypeName, prototype); 13 | } 14 | 15 | public Prototype getPrototypeCloneFromPrototypeRegistory(String name) { 16 | return this.prototypeRegistry.get(name).clone(); 17 | } 18 | } -------------------------------------------------------------------------------- /ProxyDesignPattern/src/main/java/ActualService.java: -------------------------------------------------------------------------------- 1 | public class ActualService implements ServiceInterface { 2 | @Override 3 | public void performOperation() { 4 | System.out.println("Invoking actual operation"); 5 | } 6 | } -------------------------------------------------------------------------------- /ProxyDesignPattern/src/main/java/Client.java: -------------------------------------------------------------------------------- 1 | public class Client { 2 | public static void main(String[] args) { 3 | ServiceInterface service = new ServiceProxy(new ActualService()); 4 | service.performOperation(); 5 | } 6 | } -------------------------------------------------------------------------------- /ProxyDesignPattern/src/main/java/ServiceInterface.java: -------------------------------------------------------------------------------- 1 | public interface ServiceInterface { 2 | void performOperation(); 3 | } -------------------------------------------------------------------------------- /ProxyDesignPattern/src/main/java/ServiceProxy.java: -------------------------------------------------------------------------------- 1 | public class ServiceProxy implements ServiceInterface { 2 | ServiceInterface actualService; 3 | 4 | public ServiceProxy(ServiceInterface actualService) { 5 | this.actualService = actualService; 6 | } 7 | 8 | @Override 9 | public void performOperation() { 10 | System.out.println("Going via proxy.."); 11 | // whatever checks you want to make. 12 | 13 | // is data valid; 14 | System.out.println("Proceed invokation, data validation complete."); 15 | 16 | // at the end call the actual service. 17 | actualService.performOperation(); 18 | } 19 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Design patterns in java 2 | 3 | This repository will hold code examples for my youtube video series on design patterns in java. 4 | 5 | Behavioural Design Patterns: https://www.youtube.com/watch?v=vzCVO7B6wYw&list=PLn05u4nMKcB-IElh3WcsjJgAT0vuY6WAW 6 | 7 | Structural Design Patterns: https://www.youtube.com/watch?v=N6iMrBiPycs&list=PLn05u4nMKcB_QzKVeALuCiTyJIFGKyfkg 8 | 9 | Creational Design Patterns: https://www.youtube.com/watch?v=CfcLUxAYwCc&list=PLn05u4nMKcB-1BSfb3L-09hkcSgNZHrv7 10 | -------------------------------------------------------------------------------- /StateDesignPattern/src/main/java/LightBulb.java: -------------------------------------------------------------------------------- 1 | import java.util.HashMap; 2 | import java.util.Map; 3 | 4 | public class LightBulb { 5 | private Map lightBulbStateMap = 6 | new HashMap<>(); 7 | 8 | State currentState; 9 | 10 | public LightBulb() { 11 | State onState = new OnState(this); 12 | State offState = new OffState(this); 13 | this.lightBulbStateMap.put(onState, offState); 14 | this.lightBulbStateMap.put(offState, onState); 15 | currentState = offState; 16 | } 17 | 18 | public void setCurrentState(State state) { 19 | this.currentState = state; 20 | } 21 | 22 | public void displayState() { 23 | this.currentState.displayState(); 24 | } 25 | 26 | public void toggle() { 27 | this.currentState.transitionTo(lightBulbStateMap.get(currentState)); 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /StateDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | LightBulb bulb = new LightBulb(); 4 | bulb.displayState(); 5 | 6 | bulb.toggle(); 7 | bulb.displayState(); 8 | 9 | bulb.toggle(); 10 | bulb.displayState(); 11 | 12 | bulb.toggle(); 13 | bulb.displayState(); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /StateDesignPattern/src/main/java/OffState.java: -------------------------------------------------------------------------------- 1 | public class OffState implements State { 2 | LightBulb bulb; 3 | 4 | public OffState(LightBulb bulb) { 5 | this.bulb = bulb; 6 | } 7 | 8 | public void transitionTo(State nextState) { 9 | this.bulb.setCurrentState(nextState); 10 | } 11 | 12 | public void displayState() { 13 | System.out.println("LightBulb is turned off."); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /StateDesignPattern/src/main/java/OnState.java: -------------------------------------------------------------------------------- 1 | public class OnState implements State { 2 | LightBulb bulb; 3 | 4 | public OnState(LightBulb bulb) { 5 | this.bulb = bulb; 6 | } 7 | 8 | public void transitionTo(State nextState) { 9 | this.bulb.setCurrentState(nextState); 10 | } 11 | 12 | public void displayState() { 13 | System.out.println("LightBulb is turned on."); 14 | } 15 | } 16 | -------------------------------------------------------------------------------- /StateDesignPattern/src/main/java/State.java: -------------------------------------------------------------------------------- 1 | public interface State { 2 | void transitionTo(State state); 3 | 4 | void displayState(); 5 | } 6 | -------------------------------------------------------------------------------- /StrategyDesignPattern/src/main/java/BubbleSortStrategy.java: -------------------------------------------------------------------------------- 1 | public class BubbleSortStrategy implements Strategy { 2 | public String execute() { 3 | return "Executing merge sort algorithm"; 4 | } 5 | } -------------------------------------------------------------------------------- /StrategyDesignPattern/src/main/java/Context.java: -------------------------------------------------------------------------------- 1 | public class Context { 2 | private Strategy myStrategy; 3 | 4 | public Context (Strategy strategy) { 5 | this.myStrategy = strategy; 6 | } 7 | 8 | public void setMyStrategy(Strategy strategy) { 9 | this.myStrategy = strategy; 10 | } 11 | 12 | public String doSomething() { 13 | return this.myStrategy.execute(); 14 | } 15 | } -------------------------------------------------------------------------------- /StrategyDesignPattern/src/main/java/Demo.java: -------------------------------------------------------------------------------- 1 | public class Demo { 2 | private Context context; 3 | public Demo(Context context) { 4 | this.context = context; 5 | } 6 | 7 | public static void main(String[] args) { 8 | Context context = new Context(new BubbleSortStrategy()); 9 | Demo demo = new Demo(context); 10 | demo.execute(); 11 | } 12 | 13 | public void execute() { 14 | System.out.println(context.doSomething()); 15 | context.setMyStrategy(new MergeSortStrategy()); 16 | System.out.println(context.doSomething()); 17 | } 18 | } -------------------------------------------------------------------------------- /StrategyDesignPattern/src/main/java/MergeSortStrategy.java: -------------------------------------------------------------------------------- 1 | public class MergeSortStrategy implements Strategy { 2 | public String execute() { 3 | return "Executing merge sort algorithm"; 4 | } 5 | } -------------------------------------------------------------------------------- /StrategyDesignPattern/src/main/java/Strategy.java: -------------------------------------------------------------------------------- 1 | public interface Strategy { 2 | String execute(); 3 | } -------------------------------------------------------------------------------- /TemplateDesignPattern/src/main/java/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | PizzaMaker cheeseCornMaker = 4 | new SimpleCheeseCornPizzaMaker(); 5 | 6 | cheeseCornMaker.make(); 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /TemplateDesignPattern/src/main/java/PizzaMaker.java: -------------------------------------------------------------------------------- 1 | public abstract class PizzaMaker { 2 | 3 | public abstract void selectBread(); 4 | 5 | public abstract void chooseIngredients(); 6 | 7 | public abstract void bakeAtTemp(); 8 | 9 | public abstract void addToppings(); 10 | 11 | public abstract void addCheese(); 12 | 13 | public void make() { 14 | selectBread(); 15 | chooseIngredients(); 16 | addToppings(); 17 | addCheese(); 18 | bakeAtTemp(); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /TemplateDesignPattern/src/main/java/SimpleCheeseCornPizzaMaker.java: -------------------------------------------------------------------------------- 1 | public class SimpleCheeseCornPizzaMaker extends PizzaMaker { 2 | 3 | @Override 4 | public void selectBread() { 5 | System.out.println("Selecting Bread..."); 6 | } 7 | 8 | @Override 9 | public void chooseIngredients() { 10 | System.out.println("Selecting ingredients..."); 11 | } 12 | 13 | @Override 14 | public void bakeAtTemp() { 15 | System.out.println("Baking at temp..."); 16 | } 17 | 18 | @Override 19 | public void addToppings() { 20 | System.out.println("Adding Toppings..."); 21 | } 22 | 23 | @Override 24 | public void addCheese() { 25 | System.out.println("Adding Cheese..."); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/badway/BoardBlock.java: -------------------------------------------------------------------------------- 1 | package badway; 2 | 3 | public class BoardBlock extends ChessComponent { 4 | 5 | private String blockColor; 6 | private Piece piece; 7 | 8 | public BoardBlock( 9 | String name, 10 | String blockColor, 11 | Piece piece) 12 | { 13 | super(name); 14 | this.blockColor = blockColor; 15 | this.piece = piece; 16 | } 17 | 18 | public String getBlockColor() { 19 | return this.blockColor; 20 | } 21 | 22 | public Piece getPiece() { 23 | return this.piece; 24 | } 25 | 26 | public void visit() { 27 | System.out.println("Visiting: " + this.getComponentName() + 28 | ". This board has " + this.getBlockColor() + 29 | " and a piece with name: " + this.getPiece().getComponentName() 30 | + " and color: " + this.getPiece().getColor()); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/badway/ChessComponent.java: -------------------------------------------------------------------------------- 1 | package badway; 2 | 3 | public abstract class ChessComponent { 4 | 5 | private String componentName; 6 | 7 | public ChessComponent(String name) { 8 | this.componentName = name; 9 | } 10 | 11 | public String getComponentName() { 12 | return this.componentName; 13 | } 14 | 15 | abstract void visit(); 16 | } 17 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/badway/Main.java: -------------------------------------------------------------------------------- 1 | package badway; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | System.out.println("From bad way."); 7 | Piece piece = new Piece("Rook", "Black"); 8 | BoardBlock blackBlock = 9 | new BoardBlock("BlackBlock", "Black", piece); 10 | 11 | blackBlock.visit(); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/badway/Piece.java: -------------------------------------------------------------------------------- 1 | package badway; 2 | 3 | public class Piece extends ChessComponent { 4 | 5 | private String color; 6 | 7 | public Piece(String name, String color) { 8 | super(name); 9 | this.color = color; 10 | } 11 | 12 | public String getColor() { 13 | return this.color; 14 | } 15 | 16 | public void visit() { 17 | System.out.println("Visiting: " + this.getComponentName() + 18 | ", color: " + this.getColor()); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/goodway/BoardBlock.java: -------------------------------------------------------------------------------- 1 | package goodway; 2 | 3 | public class BoardBlock extends ChessComponent { 4 | 5 | private String blockColor; 6 | private Piece piece; 7 | 8 | public BoardBlock( 9 | String name, 10 | String blockColor, 11 | Piece piece) 12 | { 13 | super(name); 14 | this.blockColor = blockColor; 15 | this.piece = piece; 16 | } 17 | 18 | public String getBlockColor() { 19 | return this.blockColor; 20 | } 21 | 22 | public Piece getPiece() { 23 | return this.piece; 24 | } 25 | 26 | public void visit() { 27 | System.out.println("Visiting: " + this.getComponentName() + 28 | ". This board has " + this.getBlockColor() + 29 | " and a piece with name: " + this.getPiece().getComponentName() 30 | + " and color: " + this.getPiece().getColor()); 31 | } 32 | @Override 33 | void accept(Visitor v) { 34 | v.visitBlock(this); 35 | } 36 | 37 | } 38 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/goodway/ChessComponent.java: -------------------------------------------------------------------------------- 1 | package goodway; 2 | 3 | public abstract class ChessComponent { 4 | 5 | private String componentName; 6 | 7 | public ChessComponent(String name) { 8 | this.componentName = name; 9 | } 10 | 11 | public String getComponentName() { 12 | return this.componentName; 13 | } 14 | 15 | abstract void accept(Visitor v); 16 | } 17 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/goodway/ConsoleVisitor.java: -------------------------------------------------------------------------------- 1 | package goodway; 2 | 3 | public class ConsoleVisitor implements Visitor { 4 | @Override 5 | public void visitPiece(Piece p) { 6 | System.out.println("Visiting: " + p.getComponentName() + 7 | ", color: " + p.getColor()); 8 | } 9 | 10 | @Override 11 | public void visitBlock(BoardBlock b) { 12 | System.out.println("Visiting: " + b.getComponentName() + 13 | ". This board has " + b.getBlockColor() + 14 | " and a piece with name: " + b.getPiece().getComponentName() 15 | + " and color: " + b.getPiece().getColor()); 16 | } 17 | } 18 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/goodway/Main.java: -------------------------------------------------------------------------------- 1 | package goodway; 2 | 3 | public class Main { 4 | 5 | public static void main(String[] args) { 6 | System.out.println("From good way"); 7 | 8 | Piece rook = new Piece("Rook", "Black"); 9 | BoardBlock block = new BoardBlock("Block", "White", rook); 10 | 11 | Visitor v = new ConsoleVisitor(); 12 | block.accept(v); 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/goodway/Piece.java: -------------------------------------------------------------------------------- 1 | package goodway; 2 | 3 | public class Piece extends ChessComponent { 4 | 5 | private String color; 6 | 7 | public Piece(String name, String color) { 8 | super(name); 9 | this.color = color; 10 | } 11 | 12 | public String getColor() { 13 | return this.color; 14 | } 15 | 16 | @Override 17 | void accept(Visitor v) { 18 | v.visitPiece(this); 19 | } 20 | } 21 | -------------------------------------------------------------------------------- /VisitorDesignPattern/src/main/java/goodway/Visitor.java: -------------------------------------------------------------------------------- 1 | package goodway; 2 | 3 | public interface Visitor { 4 | void visitPiece(Piece p); 5 | void visitBlock(BoardBlock b); 6 | } 7 | --------------------------------------------------------------------------------