├── Derek Banas ├── Observer Design Pattern │ ├── README.md │ └── com.derekbanas.observer │ │ ├── GrabStocks.java │ │ ├── Observer.java │ │ ├── README.md │ │ ├── StockGrabber.java │ │ ├── StockObserver.java │ │ └── Subject.java └── Strategy Design Pattern │ ├── README.md │ └── com.derekbanas.strategy │ ├── Animal.java │ ├── AnimalPlay.java │ ├── Bird.java │ ├── CantFly.java │ ├── Dog.java │ ├── Flys.java │ └── ItFlys.java ├── Others ├── Adapter Design Pattern │ ├── Adapter Design Pattern - Coding Simplified │ │ └── com.codingsimplified.adapter │ │ │ └── WebDriverExample.java │ └── README.md ├── Bridge Design Pattern │ ├── Bridge Design Pattern - Coding Simplified │ │ └── com.codingsimplified.bridge │ │ │ └── Client.java │ └── README.md ├── Builder Design Pattern │ ├── Builder Design Pattern - Coding Simplified │ │ └── com.codingsimplified.builder │ │ │ └── VehicleBuilderExample.java │ └── README.md ├── Composite Design Pattern │ ├── Composite Design Pattern - Coding Simplified │ │ └── CompositeExample.java │ └── README.md ├── Decorator Design Pattern │ ├── Decorator Design Pattern - Coding Simplified │ │ └── com.codingsimplified.decorator │ │ │ └── DressDecoratorExample.java │ └── README.md ├── Facade Design Pattern │ ├── Facade Design Pattern - Coding Simplified │ │ └── com.codingsimplified.facade │ │ │ └── WebExplorerHelperFacade.java │ └── README.md ├── Factory Design Pattern │ ├── Factory Design Pattern - Coding Simplified │ │ ├── README.md │ │ └── com.codingsimplified.factory │ │ │ └── FactoryPatternExample.java │ ├── Factory Design Pattern - javaTpoint │ │ ├── README.md │ │ └── com.javaTpoint.factory │ │ │ ├── CommercialPlan.java │ │ │ ├── DomesticPlan.java │ │ │ ├── GenerateBill.java │ │ │ ├── GetPlanFactory.java │ │ │ ├── InstitutionalPlan.java │ │ │ └── Plan.java │ └── README.md ├── Flyweight Design Pattern │ ├── Flyweight Design Pattern - Coding Simplified │ │ └── com.codingsimplified.flyweight │ │ │ └── EngineeringFlyweight.java │ └── README.md ├── Observer Design Pattern │ ├── Observer Design Pattern - Coding Simplified │ │ └── com.codingsimplified.observer │ │ │ └── ObserverPatternTest.java │ └── README.md ├── Prototype Design Pattern │ ├── Prototype Design Pattern - Coding Simplified │ │ └── com.codingsimplified.prototype │ │ │ └── VehiclePrototypeExample.java │ └── README.md ├── Proxy Design Pattern │ ├── Proxy Design Pattern - Coding Simplified │ │ └── com.codingsimplified.proxy │ │ │ └── DBExecutorProxyExample.java │ ├── Proxy Design Pattern - javaTpoint │ │ └── com.javaTpoint.proxy │ │ │ └── README.md │ └── README.md ├── README.md └── Singleton Design Pattern │ ├── README.md │ ├── Singleton Design Pattern - Coding Simplified │ └── com.codingsimplified.singleton │ │ ├── SingletonEagerInitiaization.java │ │ ├── SingletonLazyInitialization.java │ │ ├── SingletonSynchronizedBlock.java │ │ └── SingletonSynchronizedMethod.java │ └── Singleton Design Pattern - GeeksForGeeks │ └── com.geeksforgeeks.singleton │ └── Singleton.java ├── README.md ├── coffee.pdf └── ecoop93-patterns.pdf /Derek Banas/Observer Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | https://www.javatpoint.com/observer-pattern 2 | 3 | An Observer Pattern says that "just define a one-to-one dependency so that when one object changes state, all its dependents are notified and updated automatically". 4 | 5 | The Memento pattern is also known as Dependents or Publish-Subscribe. 6 | 7 | Benefits: 8 | It describes the coupling between the objects and the observer. 9 | It provides the support for broadcast-type communication. 10 | Usage: 11 | When the change of a state in one object must be reflected in another object without keeping the objects tight coupled. 12 | When the framework we writes and needs to be enhanced in future with new observers with minimal chamges. 13 | 14 | 15 | ========================================================================================================================================= 16 | 17 | https://www.youtube.com/watch?v=wiQdrH2YpT4&list=PLF206E906175C7E07&index=4 18 | 19 | http://www.newthinktank.com/2012/08/observer-design-pattern-tutorial/ 20 | 21 | The Observer pattern is a software design pattern in which an object, called the subject (Publisher), maintains a list of its dependents, 22 | called observers (Subscribers), and notifies them automatically of any state changes, usually by calling one of their methods. 23 | -------------------------------------------------------------------------------- /Derek Banas/Observer Design Pattern/com.derekbanas.observer/GrabStocks.java: -------------------------------------------------------------------------------- 1 | package com.derekbanas.observer; 2 | 3 | public class GrabStocks { 4 | public static void main(String args[]) { 5 | StockGrabber stockGrabber = new StockGrabber(); 6 | 7 | StockObserver observer1 = new StockObserver(stockGrabber); 8 | 9 | stockGrabber.setIbmprice(1.0); 10 | stockGrabber.setAaplprice(2.0); 11 | stockGrabber.setGoogprice(3.0); 12 | 13 | StockObserver observer2 = new StockObserver(stockGrabber); 14 | 15 | stockGrabber.setIbmprice(1.01); 16 | stockGrabber.setAaplprice(2.02); 17 | stockGrabber.setGoogprice(3.03); 18 | 19 | stockGrabber.unregister(observer2); 20 | stockGrabber.setIbmprice(1.012); 21 | stockGrabber.setAaplprice(2.023); 22 | stockGrabber.setGoogprice(3.034); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /Derek Banas/Observer Design Pattern/com.derekbanas.observer/Observer.java: -------------------------------------------------------------------------------- 1 | package com.derekbanas.observer; 2 | 3 | public interface Observer { 4 | public void update(double ibmprice, double aaplprice, double googprice); 5 | } 6 | -------------------------------------------------------------------------------- /Derek Banas/Observer Design Pattern/com.derekbanas.observer/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Derek Banas/Observer Design Pattern/com.derekbanas.observer/StockGrabber.java: -------------------------------------------------------------------------------- 1 | package com.derekbanas.observer; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class StockGrabber implements Subject{ 6 | 7 | private ArrayList observers; 8 | private double ibmprice; 9 | private double aaplprice; 10 | private double googprice; 11 | 12 | public StockGrabber() { 13 | observers = new ArrayList(); 14 | } 15 | 16 | @Override 17 | public void register(Observer o) { 18 | observers.add(o); 19 | } 20 | 21 | @Override 22 | public void unregister(Observer o) { 23 | int index = observers.indexOf(o); 24 | System.out.println("observer deleted: " + index+1); 25 | observers.remove(index); 26 | } 27 | 28 | @Override 29 | public void notifyObservers() { 30 | for(Observer observer: observers) { 31 | observer.update(ibmprice, aaplprice, googprice); 32 | } 33 | } 34 | 35 | public void setIbmprice(double IBMprice) { 36 | ibmprice = IBMprice; 37 | notifyObservers(); 38 | } 39 | 40 | public void setAaplprice(double AAPLprice) { 41 | aaplprice = AAPLprice; 42 | notifyObservers(); 43 | } 44 | 45 | public void setGoogprice(double GOOGprice) { 46 | googprice = GOOGprice; 47 | notifyObservers(); 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /Derek Banas/Observer Design Pattern/com.derekbanas.observer/StockObserver.java: -------------------------------------------------------------------------------- 1 | package com.derekbanas.observer; 2 | 3 | public class StockObserver implements Observer{ 4 | private static int ObserverIDTracker=0; 5 | private int ObserverID; 6 | 7 | private double ibmprice; 8 | private double aaplprice; 9 | private double googprice; 10 | 11 | private Subject stockGrabber; 12 | 13 | public StockObserver(Subject stockGrabber){ 14 | this.stockGrabber = stockGrabber; 15 | this.ObserverID = ++ObserverIDTracker; 16 | System.out.println("New observer: "+ this.ObserverID); 17 | stockGrabber.register(this); 18 | } 19 | 20 | @Override 21 | public void update(double ibmprice, double aaplprice, double googprice) { 22 | this.ibmprice = ibmprice; 23 | this.aaplprice = aaplprice; 24 | this.googprice = googprice; 25 | printThePrices(); 26 | } 27 | private void printThePrices() { 28 | System.out.println(ObserverID+" \nIBM: "+ibmprice+" \nAAPL: "+aaplprice+" \nGOOG: "+googprice); 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /Derek Banas/Observer Design Pattern/com.derekbanas.observer/Subject.java: -------------------------------------------------------------------------------- 1 | package com.derekbanas.observer; 2 | 3 | public interface Subject { 4 | void register(Observer o); 5 | void unregister(Observer o); 6 | void notifyObservers(); 7 | } 8 | -------------------------------------------------------------------------------- /Derek Banas/Strategy Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | http://www.newthinktank.com/2012/08/strategy-design-pattern-tutorial/ 2 | 3 | https://www.youtube.com/watch?v=-NCgRD9-C6o&list=PLF206E906175C7E07&index=3 4 | 5 | Here is my Strategy design patterns tutorial. You use this pattern if you need to dynamically change an algorithm used by an object at run time. Don’t worry, just watch the video and you’ll get it. 6 | 7 | The pattern also allows you to eliminate code duplication. It separates behavior from super and subclasses. It is a super design pattern and is often the first one taught. 8 | 9 | ==================================================================================================================== 10 | 11 | https://www.javatpoint.com/strategy-pattern 12 | 13 | A Strategy Pattern says that "defines a family of functionality, encapsulate each one, and make them interchangeable". 14 | 15 | The Strategy Pattern is also known as Policy. 16 | 17 | Benefits: 18 | It provides a substitute to subclassing. 19 | It defines each behavior within its own class, eliminating the need for conditional statements. 20 | It makes it easier to extend and incorporate new behavior without changing the application. 21 | Usage: 22 | When the multiple classes differ only in their behaviors.e.g. Servlet API. 23 | It is used when you need different variations of an algorithm. 24 | -------------------------------------------------------------------------------- /Derek Banas/Strategy Design Pattern/com.derekbanas.strategy/Animal.java: -------------------------------------------------------------------------------- 1 | 2 | package com.derekbanas.strategy; 3 | 4 | public class Animal { 5 | 6 | private String sound; 7 | private String name; 8 | private Flys flyingType; 9 | public void setSound(String s) { 10 | 11 | } 12 | 13 | public String getName() { 14 | return name; 15 | } 16 | 17 | public void setName(String name) { 18 | this.name = name; 19 | } 20 | 21 | public String getSound() { 22 | return sound; 23 | } 24 | 25 | public void setFlyingType(Flys newFlyingType) { 26 | flyingType = newFlyingType; 27 | } 28 | 29 | public String tryToFly() { 30 | return flyingType.fly(); 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /Derek Banas/Strategy Design Pattern/com.derekbanas.strategy/AnimalPlay.java: -------------------------------------------------------------------------------- 1 | 2 | package com.derekbanas.strategy; 3 | 4 | public class AnimalPlay { 5 | public static void main(String args[]) { 6 | Animal dog = new Dog(); 7 | System.out.println(dog.tryToFly()); 8 | Animal bird = new Bird(); 9 | System.out.println(bird.tryToFly()); 10 | dog.setFlyingType(new ItFlys()); 11 | System.out.println(dog.tryToFly()); 12 | } 13 | } 14 | -------------------------------------------------------------------------------- /Derek Banas/Strategy Design Pattern/com.derekbanas.strategy/Bird.java: -------------------------------------------------------------------------------- 1 | 2 | package com.derekbanas.strategy; 3 | 4 | public class Bird extends Animal{ 5 | 6 | public Bird() { 7 | setSound("Chirp"); 8 | setFlyingType(new ItFlys()); 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /Derek Banas/Strategy Design Pattern/com.derekbanas.strategy/CantFly.java: -------------------------------------------------------------------------------- 1 | 2 | package com.derekbanas.strategy; 3 | 4 | public class CantFly implements Flys{ 5 | public String fly() { 6 | return "Can't fly"; 7 | } 8 | } 9 | -------------------------------------------------------------------------------- /Derek Banas/Strategy Design Pattern/com.derekbanas.strategy/Dog.java: -------------------------------------------------------------------------------- 1 | package com.derekbanas.strategy; 2 | 3 | public class Dog extends Animal{ 4 | 5 | public Dog() { 6 | setSound("Bark"); 7 | setFlyingType(new CantFly()); 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Derek Banas/Strategy Design Pattern/com.derekbanas.strategy/Flys.java: -------------------------------------------------------------------------------- 1 | package com.derekbanas.strategy; 2 | 3 | public interface Flys { 4 | public String fly(); 5 | } 6 | -------------------------------------------------------------------------------- /Derek Banas/Strategy Design Pattern/com.derekbanas.strategy/ItFlys.java: -------------------------------------------------------------------------------- 1 | package com.derekbanas.strategy; 2 | 3 | public class ItFlys implements Flys{ 4 | public String fly() { 5 | return "flying high"; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Others/Adapter Design Pattern/Adapter Design Pattern - Coding Simplified/com.codingsimplified.adapter/WebDriverExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Structural 3 | * 4 | * Adaptor means adapting like how charging adaptor helps us with charger in different countries. 5 | * 6 | * Allows existing class to be used with others(Adaptee) without modifying their source code 7 | * WenDriver executor Adapter 8 | * 9 | * Interface: Webdriver 10 | * Interface Implementation : ChromeDriver, WebDriverAdapter 11 | * Adapter: WebDriverAdapter 12 | * Adaptee: ieDriver 13 | * Client: Adapter 14 | */ 15 | 16 | package com.codingsimplified.adapter; 17 | 18 | interface Driver { 19 | public void getElement(); 20 | public void selectElement(); 21 | } 22 | 23 | class ChromeDriver implements Driver { 24 | 25 | @Override 26 | public void getElement() { 27 | System.out.println("chrome driver get element"); 28 | 29 | } 30 | 31 | @Override 32 | public void selectElement() { 33 | System.out.println("chrome driver select element"); 34 | } 35 | 36 | } 37 | 38 | class IEDriver { 39 | public void findElement() { 40 | System.out.println("IE driver find element"); 41 | } 42 | 43 | public void clickElement() { 44 | System.out.println("IE driver click element"); 45 | } 46 | } 47 | 48 | class WebDriver implements Driver { 49 | IEDriver ieDriver; 50 | 51 | public WebDriver(IEDriver ieDriver) { 52 | this.ieDriver = ieDriver; 53 | } 54 | 55 | @Override 56 | public void getElement() { 57 | ieDriver.findElement(); 58 | } 59 | 60 | @Override 61 | public void selectElement() { 62 | ieDriver.clickElement(); 63 | } 64 | } 65 | 66 | public class WebDriverExample { 67 | public static void main(String args[]) { 68 | ChromeDriver chromeDriver = new ChromeDriver(); 69 | chromeDriver.getElement(); 70 | chromeDriver.selectElement(); 71 | 72 | IEDriver ieDriver = new IEDriver(); 73 | ieDriver.findElement(); 74 | ieDriver.clickElement(); 75 | 76 | Driver driver = new WebDriver(ieDriver); 77 | driver.getElement(); 78 | driver.selectElement(); 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /Others/Adapter Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Bridge Design Pattern/Bridge Design Pattern - Coding Simplified/com.codingsimplified.bridge/Client.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Structural 3 | * 4 | * Used when we've hiearchies in both interfaces as well as implementationd & we want 5 | * to hide implementatio from client 6 | * 7 | * it decouple abstraction from implementation 8 | */ 9 | 10 | package com.codingsimplified.bridge; 11 | 12 | abstract class TV { 13 | Remote remote; 14 | 15 | public TV(Remote remote) { 16 | this.remote = remote; 17 | } 18 | 19 | public void on() { 20 | } 21 | 22 | public void off() { 23 | } 24 | } 25 | 26 | class Sony extends TV { 27 | 28 | Remote remote; 29 | 30 | public Sony(Remote remote) { 31 | super(remote); 32 | this.remote = remote; 33 | } 34 | 35 | @Override 36 | public void on() { 37 | System.out.println("Sony TV ON"); 38 | } 39 | 40 | @Override 41 | public void off() { 42 | System.out.println("Sony TV OFF"); 43 | } 44 | } 45 | 46 | class Philips extends TV { 47 | 48 | Remote remote; 49 | 50 | public Philips(Remote remote) { 51 | super(remote); 52 | this.remote = remote; 53 | } 54 | 55 | @Override 56 | public void on() { 57 | System.out.println("Philips TV ON"); 58 | } 59 | 60 | @Override 61 | public void off() { 62 | System.out.println("Philips TV OFF"); 63 | } 64 | } 65 | 66 | interface Remote { 67 | public void on(); 68 | public void off(); 69 | } 70 | 71 | class OldRemote implements Remote { 72 | @Override 73 | public void on() { 74 | System.out.println("Old remote ON"); 75 | } 76 | 77 | @Override 78 | public void off() { 79 | System.out.println("Old remote OFF"); 80 | } 81 | } 82 | 83 | class NewRemote implements Remote { 84 | @Override 85 | public void on() { 86 | System.out.println("New remote ON"); 87 | } 88 | 89 | @Override 90 | public void off() { 91 | System.out.println("New remote ON"); 92 | } 93 | } 94 | 95 | public class Client { 96 | public static void main(String args[]) { 97 | TV sonyWithOld = new Sony(new OldRemote()); 98 | sonyWithOld.on(); 99 | sonyWithOld.off(); 100 | 101 | TV sonyWithNew = new Sony(new NewRemote()); 102 | sonyWithNew.on(); 103 | sonyWithNew.off(); 104 | 105 | TV philipsWithOld = new Philips(new OldRemote()); 106 | philipsWithOld.on(); 107 | philipsWithOld.off(); 108 | 109 | TV philipsWithNew = new Philips(new NewRemote()); 110 | philipsWithNew.on(); 111 | philipsWithNew.off(); 112 | } 113 | } 114 | -------------------------------------------------------------------------------- /Others/Bridge Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Builder Design Pattern/Builder Design Pattern - Coding Simplified/com.codingsimplified.builder/VehicleBuilderExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | *Creational Design Pattern 3 | * 4 | * Too many parameters in constructor, its difficult to maintain the order 5 | * 6 | * -- with this, no need to maintain the order 7 | * no need of passing all parameters, only required ones(generally otherwise we send null for optional parameters) 8 | */ 9 | 10 | package com.codingsimplified.builder; 11 | 12 | class Vehicle { 13 | // mandatory params 14 | private String engine; 15 | private int wheels; 16 | 17 | // optional params 18 | private int airbags; 19 | 20 | private Vehicle(VehicleBuilder builder) { 21 | this.engine = builder.engine; 22 | this.wheels = builder.wheels; 23 | this.airbags = builder.airbags; 24 | } 25 | 26 | public String getEngine() { 27 | return engine; 28 | } 29 | 30 | public int getWheels() { 31 | return wheels; 32 | } 33 | 34 | public int getAirbags() { 35 | return airbags; 36 | } 37 | 38 | public static class VehicleBuilder { 39 | // mandatory params 40 | private String engine; 41 | private int wheels; 42 | 43 | // optional params 44 | private int airbags; 45 | 46 | public VehicleBuilder(String engine, int wheels) { 47 | this.engine = engine; 48 | this.wheels = wheels; 49 | } 50 | 51 | public VehicleBuilder setAirbags(int airbags) { 52 | this.airbags = airbags; 53 | return this; 54 | } 55 | 56 | public Vehicle build() { 57 | return new Vehicle(this); 58 | } 59 | } 60 | } 61 | 62 | class VehicleBuilderExample { 63 | public static void main(String args[]) { 64 | Vehicle Car = new Vehicle.VehicleBuilder("1500cc", 4).setAirbags(4).build(); 65 | Vehicle Bike = new Vehicle.VehicleBuilder("1200cc", 2).build(); 66 | 67 | System.out.println(Car.getEngine()); 68 | System.out.println(Car.getWheels()); 69 | System.out.println(Car.getAirbags()); 70 | 71 | System.out.println(Bike.getEngine()); 72 | System.out.println(Bike.getWheels()); 73 | System.out.println(Bike.getAirbags()); 74 | } 75 | } 76 | -------------------------------------------------------------------------------- /Others/Builder Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Composite Design Pattern/Composite Design Pattern - Coding Simplified/CompositeExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Structural design pattern 3 | * 4 | * result from more than one class. individual obj -> leaf, composition f object - > composite 5 | * 6 | * four components : component(Account), leaf(Saving and Deposit), composite(CompositeAccount), client(Client class) 7 | * 8 | * Individual class will work as well as composite 9 | */ 10 | 11 | //https://www.youtube.com/watch?v=AIyTWtOqrfs&list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx&index=7 12 | 13 | package com.codingsimplified.composite; 14 | 15 | import java.util.ArrayList; 16 | 17 | abstract class Account { 18 | public abstract float getBalance(); 19 | } 20 | 21 | class DepositAccount extends Account { 22 | private String accountNo; 23 | private float accountBalance; 24 | 25 | public DepositAccount(String accountNo, float accountBalance) { 26 | super(); 27 | this.accountNo = accountNo; 28 | this.accountBalance = accountBalance; 29 | } 30 | 31 | @Override 32 | public float getBalance() { 33 | return accountBalance; 34 | } 35 | 36 | } 37 | 38 | class SavingAccount extends Account { 39 | private String accountNo; 40 | private float accountBalance; 41 | 42 | public SavingAccount(String accountNo, float accountBalance) { 43 | super(); 44 | this.accountNo = accountNo; 45 | this.accountBalance = accountBalance; 46 | } 47 | 48 | @Override 49 | public float getBalance() { 50 | return accountBalance; 51 | } 52 | } 53 | 54 | class CompositeAccount extends Account { 55 | private float totalBalance; 56 | private ArrayList accountList = new ArrayList(); 57 | 58 | @Override 59 | public float getBalance() { 60 | totalBalance = 0; 61 | for (Account account : accountList) { 62 | totalBalance += account.getBalance(); 63 | } 64 | return totalBalance; 65 | } 66 | 67 | public void addAccount(Account account) { 68 | accountList.add(account); 69 | } 70 | 71 | public void removeAccount(Account account) { 72 | accountList.remove(account); 73 | } 74 | } 75 | 76 | public class CompositeExample { 77 | public static void main(String args[]) { 78 | CompositeAccount component = new CompositeAccount(); 79 | 80 | component.addAccount(new DepositAccount("DA001", 100)); 81 | component.addAccount(new SavingAccount("DA002", 200)); 82 | component.addAccount(new SavingAccount("SA001", 300)); 83 | 84 | System.out.println(component.getBalance()); 85 | } 86 | } 87 | -------------------------------------------------------------------------------- /Others/Composite Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Decorator Design Pattern/Decorator Design Pattern - Coding Simplified/com.codingsimplified.decorator/DressDecoratorExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Structural 3 | * 4 | * Modify functionality at runtime 5 | */ 6 | 7 | package com.codingsimplified.decorator; 8 | 9 | interface Dress { 10 | public void assemble(); 11 | } 12 | 13 | class BasicDress implements Dress { 14 | public void assemble() { 15 | 16 | } 17 | } 18 | 19 | class DressDecorator implements Dress { 20 | protected Dress dress; 21 | 22 | public DressDecorator(Dress dress) { 23 | this.dress = dress; 24 | } 25 | 26 | public void assemble() { 27 | this.dress.assemble(); 28 | } 29 | } 30 | 31 | class FancyDress extends DressDecorator { 32 | public FancyDress(Dress c) { 33 | super(c); 34 | } 35 | 36 | public void assemble() { 37 | super.assemble(); 38 | System.out.println("Adding fancy dress"); 39 | } 40 | } 41 | 42 | class SportyDress extends DressDecorator { 43 | public SportyDress(Dress c) { 44 | super(c); 45 | } 46 | 47 | public void assemble() { 48 | super.assemble(); 49 | System.out.println("Adding sporty dress"); 50 | } 51 | } 52 | 53 | class CasualDress extends DressDecorator { 54 | public CasualDress(Dress c) { 55 | super(c); 56 | } 57 | 58 | public void assemble() { 59 | super.assemble(); 60 | System.out.println("Adding casual dress"); 61 | } 62 | } 63 | 64 | public class DressDecoratorExample { 65 | public static void main(String args[]) { 66 | Dress sportydress = new SportyDress(new BasicDress()); 67 | sportydress.assemble(); 68 | System.out.println(); 69 | 70 | Dress fancydress = new FancyDress(new BasicDress()); 71 | fancydress.assemble(); 72 | System.out.println(); 73 | 74 | Dress casualdress = new CasualDress(new BasicDress()); 75 | casualdress.assemble(); 76 | System.out.println(); 77 | 78 | Dress sportyfancydress = new SportyDress(new FancyDress(new BasicDress())); 79 | sportyfancydress.assemble(); 80 | System.out.println(); 81 | 82 | Dress sportyCasualdress = new SportyDress(new CasualDress(new BasicDress())); 83 | sportyCasualdress.assemble(); 84 | System.out.println(); 85 | 86 | Dress casualFancydress = new CasualDress(new FancyDress(new BasicDress())); 87 | casualFancydress.assemble(); 88 | System.out.println(); 89 | 90 | } 91 | } 92 | -------------------------------------------------------------------------------- /Others/Decorator Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Facade Design Pattern/Facade Design Pattern - Coding Simplified/com.codingsimplified.facade/WebExplorerHelperFacade.java: -------------------------------------------------------------------------------- 1 | package com.codingsimplified.facade; 2 | 3 | import java.sql.Driver; 4 | 5 | class ChromeDriver { 6 | public static Driver getChromeDriver() { 7 | return null; 8 | } 9 | 10 | public static void generateHTMLReport(String test, Driver driver) { 11 | System.out.println("Generting HTML Reports with chrome driver"); 12 | } 13 | 14 | public static void generateJunitReport(String test, Driver driver) { 15 | System.out.println("Generting Junit Reports with chrome driver"); 16 | } 17 | } 18 | 19 | class FirefoxDriver { 20 | public static Driver getFireFoxDriver() { 21 | return null; 22 | } 23 | 24 | public static void generateHTMLReport(String test, Driver driver) { 25 | System.out.println("Generting HTML Reports with firefox driver"); 26 | } 27 | 28 | public static void generateJunitReport(String test, Driver driver) { 29 | System.out.println("Generting Junit Reports with firefox driver"); 30 | } 31 | } 32 | 33 | class WebExplorerHelper { 34 | public static void generateReports(String explorer, String test, String report) { 35 | Driver driver = null; 36 | switch (explorer) { 37 | case "chrome": 38 | driver = ChromeDriver.getChromeDriver(); 39 | switch (report) { 40 | case "HTML": 41 | ChromeDriver.generateHTMLReport(test, driver); 42 | break; 43 | case "Junit": 44 | ChromeDriver.generateJunitReport(test, driver); 45 | break; 46 | } 47 | break; 48 | case "firefox": 49 | driver = FirefoxDriver.getFireFoxDriver(); 50 | switch (report) { 51 | case "HTML": 52 | FirefoxDriver.generateHTMLReport(test, driver); 53 | break; 54 | case "Junit": 55 | FirefoxDriver.generateJunitReport(test, driver); 56 | break; 57 | } 58 | break; 59 | } 60 | } 61 | } 62 | 63 | public class WebExplorerHelperFacade { 64 | public static void main(String args[]) { 65 | String test = "facade design pattern"; 66 | WebExplorerHelper.generateReports("firefox", test, "HTML"); 67 | WebExplorerHelper.generateReports("firefox", test, "Junit"); 68 | WebExplorerHelper.generateReports("chrome", test, "HTML"); 69 | WebExplorerHelper.generateReports("chrome", test, "Junit"); 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /Others/Facade Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/Factory Design Pattern - Coding Simplified/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/Factory Design Pattern - Coding Simplified/com.codingsimplified.factory/FactoryPatternExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * *Creational Design Pattern 3 | * 4 | * Responsible for returning instance of all subClasses. 5 | * provide abstraction between client class and implementation.. 6 | * for vehicle as parent and car and bike as subclass. if we create direct object of subclasses and use car c= new Car(), Bike b = new Bike(). 7 | * If the name of Car class changes, we have to update everywhere in client class, whereas with factory patter, we can get away by just updating it in factory class. 8 | * 9 | */ 10 | 11 | //https://www.youtube.com/watch?v=jcGSowIzmzM&list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx&index=2 12 | 13 | package com.codingsimplified.factory; 14 | 15 | abstract class Vehicle { 16 | abstract int getWheel(); 17 | } 18 | 19 | class Car extends Vehicle { 20 | int wheel; 21 | 22 | Car(int wheel) { 23 | this.wheel = wheel; 24 | } 25 | 26 | @Override 27 | int getWheel() { 28 | // TODO Auto-generated method stub 29 | return this.wheel; 30 | } 31 | } 32 | 33 | class Bike extends Vehicle { 34 | int wheel; 35 | 36 | Bike(int wheel) { 37 | this.wheel = wheel; 38 | } 39 | 40 | @Override 41 | int getWheel() { 42 | // TODO Auto-generated method stub 43 | return this.wheel; 44 | } 45 | } 46 | 47 | class VehicleFactory { 48 | public static Vehicle getVehicle(String vehicle, int wheel) { 49 | if (vehicle.equals("Car")) { 50 | return new Car(wheel); 51 | } else if (vehicle.equals("Bike")) { 52 | return new Bike(wheel); 53 | } 54 | return null; 55 | } 56 | } 57 | 58 | public class FactoryPatternExample { 59 | public static void main(String args[]) { 60 | Vehicle v = VehicleFactory.getVehicle("Car", 4); 61 | System.out.println("number of wheels: " + v.getWheel()); 62 | Vehicle v2 = VehicleFactory.getVehicle("Bike", 2); 63 | System.out.println("number of wheels: " + v2.getWheel()); 64 | } 65 | } 66 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/Factory Design Pattern - javaTpoint/README.md: -------------------------------------------------------------------------------- 1 | https://www.javatpoint.com/factory-method-design-pattern 2 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/Factory Design Pattern - javaTpoint/com.javaTpoint.factory/CommercialPlan.java: -------------------------------------------------------------------------------- 1 | package com.javaTpoint.factory; 2 | 3 | public class CommercialPlan extends Plan { 4 | 5 | @Override 6 | void setRate() { 7 | rate = 8.0; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/Factory Design Pattern - javaTpoint/com.javaTpoint.factory/DomesticPlan.java: -------------------------------------------------------------------------------- 1 | package com.javaTpoint.factory; 2 | 3 | public class DomesticPlan extends Plan { 4 | 5 | @Override 6 | void setRate() { 7 | rate = 5.5; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/Factory Design Pattern - javaTpoint/com.javaTpoint.factory/GenerateBill.java: -------------------------------------------------------------------------------- 1 | package com.javaTpoint.factory; 2 | 3 | import java.util.Scanner; 4 | 5 | public class GenerateBill { 6 | public static void main(String args[]) { 7 | GetPlanFactory planFactoryObject = new GetPlanFactory(); 8 | Scanner sc = new Scanner(System.in); 9 | String planType = sc.next(); 10 | int units = sc.nextInt(); 11 | Plan plan = planFactoryObject.getPlan(planType); 12 | System.out.println("Plan: " + planType); 13 | plan.setRate(); 14 | System.out.println("Bill: " + plan.calculateBill(units)); 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/Factory Design Pattern - javaTpoint/com.javaTpoint.factory/GetPlanFactory.java: -------------------------------------------------------------------------------- 1 | package com.javaTpoint.factory; 2 | 3 | public class GetPlanFactory { 4 | Plan getPlan(String planType) { 5 | if (planType.equals("CommercialPlan")) { 6 | return new CommercialPlan(); 7 | } else if (planType.equals("DomesticPlan")) { 8 | return new DomesticPlan(); 9 | } else if (planType.equals("institutionalPlan")) { 10 | return new InstitutionalPlan(); 11 | } 12 | return null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/Factory Design Pattern - javaTpoint/com.javaTpoint.factory/InstitutionalPlan.java: -------------------------------------------------------------------------------- 1 | package com.javaTpoint.factory; 2 | 3 | public class InstitutionalPlan extends Plan { 4 | 5 | @Override 6 | void setRate() { 7 | rate = 10.5; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/Factory Design Pattern - javaTpoint/com.javaTpoint.factory/Plan.java: -------------------------------------------------------------------------------- 1 | package com.javaTpoint.factory; 2 | 3 | public abstract class Plan { 4 | double rate; 5 | 6 | abstract void setRate(); 7 | 8 | double calculateBill(int units) { 9 | return units * rate; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /Others/Factory Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Flyweight Design Pattern/Flyweight Design Pattern - Coding Simplified/com.codingsimplified.flyweight/EngineeringFlyweight.java: -------------------------------------------------------------------------------- 1 | 2 | /* 3 | * Structural 4 | * used when creation of more than 10^5 objects is needed, to reduce creation of object 5 | * Create map and store object 6 | * 7 | * Interface: contain common methods, Employee 8 | * Object: Individual class: Developer, Tester 9 | *Intrinsic Properties: Which are same for an object (bug -> dev: fixing bug, tester: finding bug) 10 | *Extrinsic Properties: which are different for a object (skills-> both dev and tester have different skills 11 | *We use factory to use return object: employeefactory 12 | *client:client class 13 | * 14 | */ 15 | 16 | package com.codingsimplified.flyweight; 17 | 18 | import java.util.HashMap; 19 | import java.util.Random; 20 | 21 | interface Employee { 22 | public void assignskills(String skill); 23 | 24 | public void task(); 25 | } 26 | 27 | class Developer implements Employee { 28 | 29 | public final String job; 30 | public String skill; 31 | 32 | public Developer() { 33 | this.job = "Fix the bug"; 34 | } 35 | 36 | public void assignskills(String skill) { 37 | this.skill = skill; 38 | } 39 | 40 | @Override 41 | public void task() { 42 | System.out.println("Developer with skill: " + this.skill + " does the job: " + this.job); 43 | } 44 | } 45 | 46 | class Tester implements Employee { 47 | public final String job; 48 | public String skill; 49 | 50 | public Tester() { 51 | this.job = "find the bug"; 52 | } 53 | 54 | @Override 55 | public void assignskills(String skill) { 56 | this.skill = skill; 57 | } 58 | 59 | @Override 60 | public void task() { 61 | System.out.println("Tester with skill: " + this.skill + " does the job: " + this.job); 62 | } 63 | 64 | } 65 | 66 | class EmployeeFactory { 67 | static HashMap m = new HashMap<>(); 68 | 69 | public static Employee getEmployee(String type) { 70 | Employee p = null; 71 | if (m.get(type) != null) { 72 | p = m.get(type); 73 | } else { 74 | switch (type) { 75 | case "Developer": 76 | p = new Developer(); 77 | System.out.println("Developer created"); 78 | break; 79 | case "Tester": 80 | p = new Tester(); 81 | System.out.println("Tester created"); 82 | break; 83 | default: 84 | System.out.println("No such employee"); 85 | break; 86 | } 87 | m.put(type, p); 88 | } 89 | return p; 90 | } 91 | } 92 | 93 | public class EngineeringFlyweight { 94 | private static String employeeType[] = { "Developer", "Tester" }; 95 | private static String skills[] = { "Java", "C++", "Python" }; 96 | 97 | public static void main(String args[]) { 98 | for (int i = 0; i < 10; i++) { 99 | Employee e = EmployeeFactory.getEmployee(getRandEmployee()); 100 | e.assignskills(getRandSkill()); 101 | e.task(); 102 | } 103 | } 104 | 105 | public static String getRandEmployee() { 106 | Random r = new Random(); 107 | return employeeType[r.nextInt(employeeType.length)]; 108 | } 109 | 110 | public static String getRandSkill() { 111 | Random r = new Random(); 112 | return skills[r.nextInt(skills.length)]; 113 | 114 | } 115 | } 116 | -------------------------------------------------------------------------------- /Others/Flyweight Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Observer Design Pattern/Observer Design Pattern - Coding Simplified/com.codingsimplified.observer/ObserverPatternTest.java: -------------------------------------------------------------------------------- 1 | package com.codingsimplified.observer; 2 | 3 | import java.util.ArrayList; 4 | 5 | interface Subject { 6 | void register(Observer observer); 7 | void unregister(Observer observer); 8 | void notifyObservers(); 9 | } 10 | 11 | class DeliveryData implements Subject { 12 | ArrayList observerList; 13 | String location; 14 | 15 | public DeliveryData() { 16 | this.observerList = new ArrayList(); 17 | } 18 | 19 | @Override 20 | public void register(Observer observer) { 21 | observerList.add(observer); 22 | } 23 | 24 | @Override 25 | public void unregister(Observer observer) { 26 | observerList.remove(observer); 27 | } 28 | 29 | @Override 30 | public void notifyObservers() { 31 | for (Observer observer : observerList) { 32 | observer.update(location); 33 | } 34 | } 35 | 36 | public void locationChanged() { 37 | this.location = getLoation(); 38 | notifyObservers(); 39 | } 40 | 41 | private String getLoation() { 42 | return "Y_Place"; 43 | } 44 | } 45 | 46 | interface Observer { 47 | void update(String location); 48 | } 49 | 50 | class Seller implements Observer { 51 | 52 | public String location; 53 | 54 | @Override 55 | public void update(String location) { 56 | this.location = location; 57 | showLocation(location); 58 | } 59 | 60 | private void showLocation(String location2) { 61 | System.out.println("Seller at location: " + location); 62 | } 63 | } 64 | 65 | class User implements Observer { 66 | 67 | public String location; 68 | 69 | @Override 70 | public void update(String location) { 71 | this.location = location; 72 | showLocation(location); 73 | } 74 | 75 | private void showLocation(String location2) { 76 | System.out.println("User at location: " + location); 77 | } 78 | } 79 | 80 | class WareHouse implements Observer { 81 | public String location; 82 | 83 | @Override 84 | public void update(String location) { 85 | this.location = location; 86 | showLocation(location); 87 | } 88 | 89 | private void showLocation(String location2) { 90 | System.out.println("WareHouse at location: " + location); 91 | } 92 | } 93 | 94 | public class ObserverPatternTest { 95 | public static void main(String args[]) { 96 | DeliveryData deliveryObj = new DeliveryData(); 97 | Observer sellerObserverObj = new Seller(); 98 | Observer userObserverObj = new User(); 99 | Observer wareHouseObserverObj = new WareHouse(); 100 | 101 | deliveryObj.register(sellerObserverObj); 102 | deliveryObj.register(userObserverObj); 103 | deliveryObj.register(wareHouseObserverObj); 104 | 105 | deliveryObj.locationChanged(); 106 | 107 | deliveryObj.unregister(userObserverObj); 108 | deliveryObj.locationChanged(); 109 | } 110 | } 111 | -------------------------------------------------------------------------------- /Others/Observer Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Prototype Design Pattern/Prototype Design Pattern - Coding Simplified/com.codingsimplified.prototype/VehiclePrototypeExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Creational Design Pattern 3 | * 4 | * Avoid multiple creation of same object 5 | */ 6 | 7 | //https://www.youtube.com/watch?v=MYe5NSmFU_c&list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx&index=4 8 | 9 | package code.codingsimplified.prototype; 10 | 11 | import java.util.*; 12 | 13 | class Vehicle implements Cloneable { 14 | private List vehicleList; 15 | 16 | public Vehicle() { 17 | this.vehicleList = new ArrayList(); 18 | } 19 | 20 | public Vehicle(List list) { 21 | this.vehicleList = list; 22 | } 23 | 24 | public List getList() { 25 | return this.vehicleList; 26 | } 27 | 28 | public void insertData() { 29 | vehicleList.add("Honda amaze"); 30 | vehicleList.add("Honda city"); 31 | vehicleList.add("Audi A4"); 32 | vehicleList.add("Renault Dustor"); 33 | vehicleList.add("BMU"); 34 | } 35 | 36 | public void printVehicles() { 37 | for (String s : this.vehicleList) { 38 | System.out.print(s + ", "); 39 | } 40 | System.out.println(); 41 | } 42 | 43 | @Override 44 | public Object clone() throws CloneNotSupportedException { 45 | List tempList = new ArrayList(); 46 | for (String s : vehicleList) { 47 | tempList.add(s); 48 | } 49 | return new Vehicle(tempList); 50 | } 51 | } 52 | 53 | public class VehiclePrototypeExample { 54 | public static void main(String args[]) throws CloneNotSupportedException { 55 | Vehicle a = new Vehicle(); 56 | a.insertData(); 57 | a.printVehicles(); 58 | 59 | Vehicle b = (Vehicle) a.clone(); 60 | b.printVehicles(); 61 | 62 | a.getList().remove("BMU"); 63 | a.printVehicles(); 64 | 65 | b.getList().add("Skoda"); 66 | b.printVehicles(); 67 | } 68 | } 69 | -------------------------------------------------------------------------------- /Others/Prototype Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Proxy Design Pattern/Proxy Design Pattern - Coding Simplified/com.codingsimplified.proxy/DBExecutorProxyExample.java: -------------------------------------------------------------------------------- 1 | /* 2 | * Improves structure of the java code 3 | * work with interfaces, gives u a good interface 4 | */ 5 | 6 | //https://www.youtube.com/watch?v=AB0gaAg9jwI&list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx&index=5 7 | 8 | package com.codingsimplified.proxy; 9 | 10 | interface DBExecutor { 11 | public void executeQuery(String query) throws Exception; 12 | } 13 | 14 | class DBxecutorImpl implements DBExecutor { 15 | 16 | @Override 17 | public void executeQuery(String query) throws Exception { 18 | System.out.println("Executing the query: " + query); 19 | } 20 | } 21 | 22 | class DBExecutorProxy implements DBExecutor { 23 | 24 | boolean isAdmin; 25 | DBxecutorImpl dbExecutor; 26 | 27 | public DBExecutorProxy(String user, String password) { 28 | if (user.equals("admin")) { 29 | isAdmin = true; 30 | } 31 | dbExecutor = new DBxecutorImpl(); 32 | } 33 | 34 | @Override 35 | public void executeQuery(String query) throws Exception { 36 | if (isAdmin) { 37 | dbExecutor.executeQuery(query); 38 | } else { 39 | if (query.equals("DELETE")) { 40 | throw new Exception("Non-admin users can't execute query: " + query); 41 | } else { 42 | dbExecutor.executeQuery(query); 43 | } 44 | } 45 | } 46 | 47 | } 48 | 49 | public class DBExecutorProxyExample { 50 | public static void main(String args[]) throws Exception { 51 | DBExecutorProxy dbProxy = new DBExecutorProxy("admin", "12345"); 52 | System.out.println("Admin: "); 53 | dbProxy.executeQuery("DELETE"); 54 | dbProxy.executeQuery("INSERT"); 55 | DBExecutorProxy dbProxy2 = new DBExecutorProxy("non-admin", "12345"); 56 | System.out.println("Non-admin: "); 57 | dbProxy2.executeQuery("INSERT"); 58 | dbProxy2.executeQuery("DELETE"); 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /Others/Proxy Design Pattern/Proxy Design Pattern - javaTpoint/com.javaTpoint.proxy/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Proxy Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/README.md: -------------------------------------------------------------------------------- 1 | 2 | -------------------------------------------------------------------------------- /Others/Singleton Design Pattern/README.md: -------------------------------------------------------------------------------- 1 | 2 | https://www.geeksforgeeks.org/singleton-class-java/ 3 | -------------------------------------------------------------------------------- /Others/Singleton Design Pattern/Singleton Design Pattern - Coding Simplified/com.codingsimplified.singleton/SingletonEagerInitiaization.java: -------------------------------------------------------------------------------- 1 | /* 2 | * 3 | *Creational Design Pattern 4 | * 5 | * 6 | *Only one object of class should exist, logging, cache session etc. are examples 7 | */ 8 | 9 | //https://www.youtube.com/watch?v=VGLjQuEQgkI&list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx&index=1 10 | 11 | package com.codingsimplified.singleton; 12 | 13 | class SingletonEager{ 14 | private static SingletonEager singeton_instance= new SingletonEager(); 15 | 16 | private SingletonEager() { 17 | 18 | } 19 | 20 | public static SingletonEager getInstance() { 21 | return singeton_instance; 22 | } 23 | } 24 | 25 | public class SingletonEagerInitiaization { 26 | public static void main(String args[]) { 27 | SingletonEager s = SingletonEager.getInstance(); 28 | System.out.println(s); 29 | 30 | SingletonEager s1 = SingletonEager.getInstance(); 31 | System.out.println(s1); 32 | } 33 | } 34 | 35 | 36 | -------------------------------------------------------------------------------- /Others/Singleton Design Pattern/Singleton Design Pattern - Coding Simplified/com.codingsimplified.singleton/SingletonLazyInitialization.java: -------------------------------------------------------------------------------- 1 | //https://www.youtube.com/watch?v=VGLjQuEQgkI&list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx&index=1 2 | 3 | package com.codingsimplified.singleton; 4 | 5 | class SingletonLazy{ 6 | private static SingletonLazy singeton_instance; 7 | 8 | private SingletonLazy() { 9 | 10 | } 11 | 12 | public static SingletonLazy getInstance() { 13 | if(singeton_instance == null) { 14 | singeton_instance = new SingletonLazy(); 15 | } 16 | return singeton_instance; 17 | } 18 | } 19 | 20 | public class SingletonLazyInitialization { 21 | public static void main(String args[]) { 22 | SingletonLazy s = SingletonLazy.getInstance(); 23 | System.out.println(s); 24 | 25 | SingletonLazy s1 = SingletonLazy.getInstance(); 26 | System.out.println(s1); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Others/Singleton Design Pattern/Singleton Design Pattern - Coding Simplified/com.codingsimplified.singleton/SingletonSynchronizedBlock.java: -------------------------------------------------------------------------------- 1 | //https://www.youtube.com/watch?v=VGLjQuEQgkI&list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx&index=1 2 | 3 | package com.codingsimplified.singleton; 4 | 5 | class SingletonSyncB{ 6 | private static SingletonSyncB singeton_instance; 7 | 8 | private SingletonSyncB() { 9 | } 10 | 11 | public static SingletonSyncB getInstance() { 12 | synchronized(SingletonSyncB.class){ 13 | if(singeton_instance == null) { 14 | singeton_instance = new SingletonSyncB(); 15 | } 16 | } 17 | return singeton_instance; 18 | } 19 | } 20 | 21 | public class SingletonSynchronizedBlock { 22 | public static void main(String args[]) { 23 | SingletonSyncB s = SingletonSyncB.getInstance(); 24 | System.out.println(s); 25 | SingletonSyncB s2 = SingletonSyncB.getInstance(); 26 | System.out.println(s); 27 | } 28 | } 29 | -------------------------------------------------------------------------------- /Others/Singleton Design Pattern/Singleton Design Pattern - Coding Simplified/com.codingsimplified.singleton/SingletonSynchronizedMethod.java: -------------------------------------------------------------------------------- 1 | //https://www.youtube.com/watch?v=VGLjQuEQgkI&list=PLt4nG7RVVk1h9lxOYSOGI9pcP3I5oblbx&index=1 2 | 3 | package com.codingsimplified.singleton; 4 | 5 | class SingletonSyncM{ 6 | private static SingletonSyncM singeton_instance; 7 | 8 | private SingletonSyncM() { 9 | 10 | } 11 | 12 | public static synchronized SingletonSyncM getInstance() { 13 | if(singeton_instance == null) { 14 | singeton_instance = new SingletonSyncM(); 15 | } 16 | return singeton_instance; 17 | } 18 | } 19 | 20 | public class SingletonSynchronizedMethod { 21 | public static void main(String args[]) { 22 | SingletonSyncM s = SingletonSyncM.getInstance(); 23 | System.out.println(s); 24 | SingletonSyncM s1 = SingletonSyncM.getInstance(); 25 | System.out.println(s1); 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /Others/Singleton Design Pattern/Singleton Design Pattern - GeeksForGeeks/com.geeksforgeeks.singleton/Singleton.java: -------------------------------------------------------------------------------- 1 | package com.geeksforgeeks.singleton; 2 | 3 | public class Singleton { 4 | private static Singleton singleton = null; 5 | public String str; 6 | 7 | private Singleton() { 8 | str = "You are dealing with a singleton object!"; 9 | } 10 | 11 | public static Singleton Singleton() { 12 | if (singleton == null) { 13 | singleton = new Singleton(); 14 | } 15 | return singleton; 16 | } 17 | 18 | public static void main(String args[]) { 19 | Singleton obj1 = Singleton(); 20 | System.out.println(obj1.str); 21 | obj1.str = "You are again dealing with a singleton object!"; 22 | Singleton obj2 = Singleton(); 23 | System.out.println(obj2.str); 24 | Singleton obj3 = Singleton(); 25 | obj2.str = (obj2.str).toUpperCase(); 26 | System.out.println("obj1 str: " + obj1.str); 27 | System.out.println("obj2 str: " + obj2.str); 28 | System.out.println("obj3 str: " + obj3.str); 29 | } 30 | } 31 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Design-Patterns -------------------------------------------------------------------------------- /coffee.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/archygupta/Design-Patterns/43422499170b9fbd2dde7ad9350e87e2c1bcc162/coffee.pdf -------------------------------------------------------------------------------- /ecoop93-patterns.pdf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/archygupta/Design-Patterns/43422499170b9fbd2dde7ad9350e87e2c1bcc162/ecoop93-patterns.pdf --------------------------------------------------------------------------------