├── README.md └── src ├── App.java └── patterns ├── builder ├── HouseBuilder.java ├── HouseType.java ├── IBuilder.java ├── director │ └── HouseDirector.java └── product │ └── House.java ├── factorymethod ├── creators │ ├── Factory.java │ ├── ShipFactory.java │ └── TruckFactory.java └── products │ ├── ITransport.java │ ├── Ship.java │ └── Truck.java └── singleton └── Database.java /README.md: -------------------------------------------------------------------------------- 1 | # Java Design Patterns 2 | 3 | Creational design patterns implementation: `Singleton`, `Factory Method`, `Builder` written in Java. 4 | 5 | ## Getting Started 6 | 7 | No additional steps required. Just compile and run the project. 8 | 9 | ## Folder Structure 10 | 11 | The workspace structure is: 12 | 13 | ```bash 14 | ├── README.md 15 | └── src 16 | ├── App.java # entry point of an application 17 | └── patterns # the folder to maintain all patterns 18 | ├── builder # builder pattern implementation folder 19 | │   ├── HouseBuilder.java 20 | │   ├── HouseType.java 21 | │   ├── IBuilder.java 22 | │   ├── director 23 | │   │   └── HouseDirector.java 24 | │   └── product 25 | │   └── House.java 26 | ├── factorymethod # factory method pattern implementation folder 27 | │   ├── creators 28 | │   │   ├── Factory.java 29 | │   │   ├── ShipFactory.java 30 | │   │   └── TruckFactory.java 31 | │   └── products 32 | │   ├── ITransport.java 33 | │   ├── Ship.java 34 | │   └── Truck.java 35 | └── singleton # singleton pattern implementation folder 36 | └── Database.java 37 | ``` 38 | 39 | ## Author 40 | 41 | Tamerlan Satualdypov. Group: SE-2018 -------------------------------------------------------------------------------- /src/App.java: -------------------------------------------------------------------------------- 1 | import java.util.Scanner; 2 | 3 | import patterns.singleton.*; 4 | 5 | import patterns.factorymethod.creators.*; 6 | 7 | import patterns.builder.*; 8 | import patterns.builder.director.*; 9 | import patterns.builder.product.*; 10 | 11 | public class App { 12 | 13 | private static void testSingleton() { 14 | System.out.println("Getting user..."); 15 | System.out.println(Database.getInstance().getUser()); 16 | } 17 | 18 | private static void testFactory(Scanner sc) { 19 | Factory factory = null; 20 | int answer = -1; 21 | 22 | System.out.println("How to deliver?"); 23 | System.out.println("1. By truck\n2. By ship"); 24 | 25 | answer = sc.nextInt(); 26 | 27 | switch (answer) { 28 | case 1: factory = new TruckFactory(); break; 29 | case 2: factory = new ShipFactory(); break; 30 | } 31 | 32 | factory.deliver(); 33 | sc.close(); 34 | } 35 | 36 | private static void testBuilder(Scanner sc) { 37 | HouseBuilder builder = new HouseBuilder(); 38 | HouseDirector director = new HouseDirector(); 39 | 40 | int answer = -1; 41 | 42 | System.out.println("Choose house type:"); 43 | System.out.println("1. Default"); 44 | System.out.println("2. With garage"); 45 | System.out.println("3. With swimming pool"); 46 | System.out.println("4. With statues"); 47 | System.out.println("5. With garden"); 48 | 49 | answer = sc.nextInt(); 50 | 51 | switch (answer) { 52 | case 1: director.constructHouse(HouseType.DEFAULT, builder); break; 53 | case 2: director.constructHouse(HouseType.WITHGARAGE, builder); break; 54 | case 3: director.constructHouse(HouseType.WITHPOOL, builder); break; 55 | case 4: director.constructHouse(HouseType.WITHSTATUES, builder); break; 56 | case 5: director.constructHouse(HouseType.WITHGARAGE, builder); break; 57 | } 58 | 59 | House house = builder.getResult(); 60 | 61 | house.print(); 62 | sc.close(); 63 | } 64 | 65 | public static void main(String[] args) throws Exception { 66 | System.out.println("Which pattern to test?"); 67 | System.out.println("1. Singleton\n2. Factory Method\n3. Builder"); 68 | 69 | int answer = -1; 70 | 71 | Scanner sc = new Scanner(System.in); 72 | answer = sc.nextInt(); 73 | 74 | switch (answer) { 75 | case 1: testSingleton(); break; 76 | case 2: testFactory(sc); break; 77 | case 3: testBuilder(sc); break; 78 | } 79 | } 80 | } 81 | -------------------------------------------------------------------------------- /src/patterns/builder/HouseBuilder.java: -------------------------------------------------------------------------------- 1 | package patterns.builder; 2 | 3 | import patterns.builder.product.House; 4 | 5 | public class HouseBuilder implements IBuilder { 6 | 7 | private Boolean needsGarage = false; 8 | private Boolean needsPool = false; 9 | private Boolean needsStatues = false; 10 | private Boolean needsGarden = false; 11 | 12 | @Override 13 | public void buildGarage() { 14 | this.needsGarage = true; 15 | } 16 | 17 | @Override 18 | public void buildSwimmingPool() { 19 | this.needsPool = true; 20 | } 21 | 22 | @Override 23 | public void buildStatues() { 24 | this.needsStatues = true; 25 | } 26 | 27 | @Override 28 | public void buildGarden() { 29 | this.needsGarden = true; 30 | } 31 | 32 | public House getResult() { 33 | return new House(this.needsGarage, this.needsPool, this.needsStatues, this.needsGarden); 34 | } 35 | } 36 | -------------------------------------------------------------------------------- /src/patterns/builder/HouseType.java: -------------------------------------------------------------------------------- 1 | package patterns.builder; 2 | 3 | public enum HouseType { 4 | DEFAULT, 5 | WITHPOOL, 6 | WITHGARDEN, 7 | WITHSTATUES, 8 | WITHGARAGE 9 | } 10 | -------------------------------------------------------------------------------- /src/patterns/builder/IBuilder.java: -------------------------------------------------------------------------------- 1 | package patterns.builder; 2 | 3 | public interface IBuilder { 4 | void buildGarage(); 5 | void buildSwimmingPool(); 6 | void buildStatues(); 7 | void buildGarden(); 8 | } 9 | -------------------------------------------------------------------------------- /src/patterns/builder/director/HouseDirector.java: -------------------------------------------------------------------------------- 1 | package patterns.builder.director; 2 | 3 | import patterns.builder.*; 4 | 5 | public class HouseDirector { 6 | 7 | public void constructHouse(HouseType type, IBuilder builder) { 8 | switch (type) { 9 | case DEFAULT: break; 10 | case WITHGARAGE: builder.buildGarage(); break; 11 | case WITHPOOL: builder.buildSwimmingPool(); break; 12 | case WITHSTATUES: builder.buildStatues(); break; 13 | case WITHGARDEN: builder.buildGarden(); break; 14 | } 15 | } 16 | } 17 | -------------------------------------------------------------------------------- /src/patterns/builder/product/House.java: -------------------------------------------------------------------------------- 1 | package patterns.builder.product; 2 | 3 | import java.util.ArrayList; 4 | 5 | public class House { 6 | 7 | // Optional house components. 8 | private final String garage = "garage"; 9 | private final String swimmingPool = "swimming pool"; 10 | private final String statues = "statues"; 11 | private final String garden = "garden"; 12 | 13 | // Default house components. 14 | private final String walls = "walls"; 15 | private final String floor = "floor"; 16 | private final String roof = "roof"; 17 | 18 | private ArrayList components = new ArrayList(); 19 | 20 | public void print() { 21 | System.out.println("You built a house: "); 22 | 23 | for (String component : this.components) { 24 | System.out.println("– with " + component); 25 | } 26 | } 27 | 28 | public House(Boolean needsGarage, Boolean needsPool, 29 | Boolean needsStatues, Boolean needsGarden) { 30 | // Every house has walls, 31 | // floor and roof. 32 | this.components.add(this.walls); 33 | this.components.add(this.floor); 34 | this.components.add(this.roof); 35 | 36 | if (needsGarage) this.components.add(this.garage); 37 | if (needsPool) this.components.add(this.swimmingPool); 38 | if (needsStatues) this.components.add(this.statues); 39 | if (needsGarden) this.components.add(this.garden); 40 | } 41 | } 42 | -------------------------------------------------------------------------------- /src/patterns/factorymethod/creators/Factory.java: -------------------------------------------------------------------------------- 1 | package patterns.factorymethod.creators; 2 | 3 | import patterns.factorymethod.products.ITransport; 4 | 5 | public abstract class Factory { 6 | 7 | public void deliver() { 8 | ITransport transport = this.create(); 9 | transport.deliver(); 10 | } 11 | 12 | public abstract ITransport create(); 13 | 14 | } 15 | -------------------------------------------------------------------------------- /src/patterns/factorymethod/creators/ShipFactory.java: -------------------------------------------------------------------------------- 1 | package patterns.factorymethod.creators; 2 | 3 | import patterns.factorymethod.products.ITransport; 4 | import patterns.factorymethod.products.Ship; 5 | 6 | public class ShipFactory extends Factory { 7 | 8 | @Override 9 | public ITransport create() { 10 | return new Ship(); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/patterns/factorymethod/creators/TruckFactory.java: -------------------------------------------------------------------------------- 1 | package patterns.factorymethod.creators; 2 | 3 | import patterns.factorymethod.products.ITransport; 4 | import patterns.factorymethod.products.Truck; 5 | 6 | public class TruckFactory extends Factory { 7 | 8 | @Override 9 | public ITransport create() { 10 | return new Truck(); 11 | } 12 | 13 | } 14 | -------------------------------------------------------------------------------- /src/patterns/factorymethod/products/ITransport.java: -------------------------------------------------------------------------------- 1 | package patterns.factorymethod.products; 2 | 3 | public interface ITransport { 4 | void deliver(); 5 | } 6 | -------------------------------------------------------------------------------- /src/patterns/factorymethod/products/Ship.java: -------------------------------------------------------------------------------- 1 | package patterns.factorymethod.products; 2 | 3 | public class Ship implements ITransport { 4 | 5 | public void deliver() { 6 | System.out.println("Delivering by Ship!"); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/patterns/factorymethod/products/Truck.java: -------------------------------------------------------------------------------- 1 | package patterns.factorymethod.products; 2 | 3 | public class Truck implements ITransport { 4 | 5 | public void deliver() { 6 | System.out.println("Delivering by Truck!"); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /src/patterns/singleton/Database.java: -------------------------------------------------------------------------------- 1 | package patterns.singleton; 2 | 3 | /** 4 | * Singleton design pattern implementation. 5 | */ 6 | public class Database { 7 | 8 | private static Database instance; 9 | 10 | /** 11 | * Function creates instance if 12 | * the class was not instantiated, 13 | * acts as a entry point to a Database class. 14 | * 15 | * @return Instance of class. 16 | */ 17 | public static Database getInstance() { 18 | if (Database.instance == null) { 19 | Database.instance = new Database(); 20 | } 21 | 22 | return Database.instance; 23 | } 24 | 25 | /** 26 | * This function is written just 27 | * for testing purpose. 28 | * 29 | * @return "Tamerlan Satualdypov" string. 30 | */ 31 | public String getUser() { 32 | return "Tamerlan Satualdypov"; 33 | } 34 | 35 | private Database() { } 36 | } --------------------------------------------------------------------------------