├── .gitattributes ├── AndroidDeveloper.class ├── AndroidDeveloper.java ├── Design.pptx ├── DeveloperClient.class ├── DeveloperClient.java ├── Employee.class ├── Employee.java ├── EmployeeFactory.class ├── EmployeeFactory.java ├── Example.class ├── Example.java ├── Jalebi.class ├── Jalebi.java ├── Samosa.class ├── Samosa.java ├── WebDeveloper.class ├── WebDeveloper.java ├── abc.ob ├── abstract_design_pattern ├── AndroidDevFactory.class ├── AndroidDevFactory.java ├── AndroidDeveloper.class ├── AndroidDeveloper.java ├── Client.class ├── Client.java ├── Employee.class ├── Employee.java ├── EmployeeAbstractFactory.class ├── EmployeeAbstractFactory.java ├── EmployeeFactory.class ├── EmployeeFactory.java ├── Manager.class ├── Manager.java ├── ManagerFactory.class ├── ManagerFactory.java ├── WebDevFactory.class ├── WebDevFactory.java ├── WebDeveloper.class └── WebDeveloper.java ├── adapter ├── AdapterCharger.class ├── AdapterCharger.java ├── AndroidCharger.class ├── AndroidCharger.java ├── AppleCharger.class ├── AppleCharger.java ├── ChargerXYZ.class ├── ChargerXYZ.java ├── Demo.class ├── Demo.java ├── DkCharger.class ├── DkCharger.java ├── Iphone13.class └── Iphone13.java ├── backgrounds(4).png ├── builder ├── Main.class ├── Main.java ├── User$UserBuilder.class ├── User.class └── User.java ├── images ├── abstract design factory.png ├── factorypattern.png └── observer_pattern.png ├── iterator ├── Main.class ├── Main.java ├── MyIterator.class ├── MyIterator.java ├── MyIteratorImpl.class ├── MyIteratorImpl.java ├── User.class ├── User.java ├── UserManagement.class └── UserManagement.java ├── note.md ├── observer ├── Demo.class ├── Demo.java ├── Observer.class ├── Observer.java ├── Subject.class ├── Subject.java ├── Subsriber.class ├── Subsriber.java ├── YoutubeChannel.class └── YoutubeChannel.java ├── prototype ├── Main.class ├── Main.java ├── NetworkConnection.class └── NetworkConnection.java └── readme.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /AndroidDeveloper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/AndroidDeveloper.class -------------------------------------------------------------------------------- /AndroidDeveloper.java: -------------------------------------------------------------------------------- 1 | public class AndroidDeveloper implements Employee { 2 | 3 | public int salary() { 4 | System.out.println("Getting android developer salary"); 5 | return 50000; 6 | } 7 | } 8 | -------------------------------------------------------------------------------- /Design.pptx: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/Design.pptx -------------------------------------------------------------------------------- /DeveloperClient.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/DeveloperClient.class -------------------------------------------------------------------------------- /DeveloperClient.java: -------------------------------------------------------------------------------- 1 | public class DeveloperClient { 2 | public static void main(String[] args) { 3 | 4 | Employee employee = EmployeeFactory.getEmployee("ANDROID DEVELOPER"); 5 | System.out.println(employee); 6 | int salary = employee.salary(); 7 | System.out.println("Salary : " + salary); 8 | 9 | Employee employee2 = EmployeeFactory.getEmployee(("WEB DEVELOPER")); 10 | System.out.println("Salary : " + employee2.salary()); 11 | } 12 | } 13 | -------------------------------------------------------------------------------- /Employee.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/Employee.class -------------------------------------------------------------------------------- /Employee.java: -------------------------------------------------------------------------------- 1 | interface Employee { 2 | 3 | int salary(); 4 | 5 | } -------------------------------------------------------------------------------- /EmployeeFactory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/EmployeeFactory.class -------------------------------------------------------------------------------- /EmployeeFactory.java: -------------------------------------------------------------------------------- 1 | public class EmployeeFactory { 2 | 3 | // get the employee 4 | public static Employee getEmployee(String empType) { 5 | 6 | if (empType.trim().equalsIgnoreCase("ANDROID DEVELOPER")) { 7 | return new AndroidDeveloper(); 8 | } else if (empType.trim().equalsIgnoreCase("WEB DEVELOPER")) { 9 | return new WebDeveloper(); 10 | } else { 11 | return null; 12 | } 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /Example.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/Example.class -------------------------------------------------------------------------------- /Example.java: -------------------------------------------------------------------------------- 1 | import java.io.FileInputStream; 2 | import java.io.FileOutputStream; 3 | import java.io.ObjectInputStream; 4 | import java.io.ObjectOutputStream; 5 | import java.lang.reflect.Constructor; 6 | 7 | public class Example { 8 | public static void main(String[] args) throws Exception, CloneNotSupportedException { 9 | 10 | // Samosa samosa1 = Samosa.getSamosa(); 11 | 12 | // System.out.println(samosa1.hashCode()); 13 | 14 | // Samosa samosa2 = Samosa.getSamosa(); 15 | 16 | // System.out.println(samosa2.hashCode()); 17 | 18 | // System.out.println(Jalebi.getJalebi().hashCode()); 19 | // System.out.println(Jalebi.getJalebi().hashCode()); 20 | 21 | /* 22 | * 1. Reflection API to break singleton pattern 23 | * solution: 1-> if object is there ==> throw exception from inside constructor 24 | * 2-> use enum 25 | * 26 | * 27 | * 2. Deserialization: 28 | * solution: implementing readResolve method 29 | * 30 | * 3. cloning 31 | * 32 | * 33 | * 34 | */ 35 | 36 | // Samosa s1 = Samosa.INSTANCE; 37 | // System.out.println(s1.hashCode()); 38 | // s1.test(); 39 | 40 | // Constructor constructor = Samosa.class.getDeclaredConstructor(); 41 | // constructor.setAccessible(true); 42 | // Samosa s2 = constructor.newInstance(); 43 | // System.out.println(s2.hashCode()); 44 | Samosa samosa = Samosa.getSamosa(); 45 | System.out.println(samosa.hashCode()); 46 | // ObjectOutputStream oos = new ObjectOutputStream(new 47 | // FileOutputStream("abc.ob")); 48 | // oos.writeObject(samosa); 49 | 50 | // System.out.println("serialization done.."); 51 | 52 | // ObjectInputStream ois = new ObjectInputStream(new FileInputStream("abc.ob")); 53 | // Samosa s2 = (Samosa) ois.readObject(); 54 | // System.out.println(s2.hashCode()); 55 | 56 | Samosa s2 = (Samosa) samosa.clone(); 57 | System.out.println(s2.hashCode()); 58 | } 59 | } 60 | -------------------------------------------------------------------------------- /Jalebi.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/Jalebi.class -------------------------------------------------------------------------------- /Jalebi.java: -------------------------------------------------------------------------------- 1 | public class Jalebi { 2 | 3 | // Eager way of creating singleton object 4 | private static Jalebi jalebi = new Jalebi(); 5 | 6 | public static Jalebi getJalebi() { 7 | return jalebi; 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /Samosa.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/Samosa.class -------------------------------------------------------------------------------- /Samosa.java: -------------------------------------------------------------------------------- 1 | import java.io.Serializable; 2 | 3 | public class Samosa implements Serializable, Cloneable { 4 | 5 | private static Samosa samosa; 6 | 7 | // constructor 8 | private Samosa() { 9 | // if (samosa != null) { 10 | // throw new RuntimeException("You are trying to break singleton pattern"); 11 | 12 | // } 13 | } 14 | 15 | // Lazy way of creating single object 16 | public static Samosa getSamosa() { 17 | // object of this class 18 | if (samosa == null) { 19 | synchronized (Samosa.class) { 20 | if (samosa == null) { 21 | samosa = new Samosa(); 22 | } 23 | } 24 | } 25 | return samosa; 26 | } 27 | 28 | public Object readResolve() { 29 | return samosa; 30 | } 31 | 32 | @Override 33 | public Object clone() throws CloneNotSupportedException { 34 | return samosa; 35 | } 36 | } 37 | 38 | /* 39 | * 40 | * 1. constructor private 41 | * 42 | * 2. object create with the help of method 43 | * 44 | * 3. create field to store object is private 45 | * 46 | */ -------------------------------------------------------------------------------- /WebDeveloper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/WebDeveloper.class -------------------------------------------------------------------------------- /WebDeveloper.java: -------------------------------------------------------------------------------- 1 | public class WebDeveloper implements Employee { 2 | 3 | public int salary() { 4 | System.out.println("Getting web developer salary"); 5 | return 40000; 6 | } 7 | 8 | } 9 | -------------------------------------------------------------------------------- /abc.ob: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abc.ob -------------------------------------------------------------------------------- /abstract_design_pattern/AndroidDevFactory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/AndroidDevFactory.class -------------------------------------------------------------------------------- /abstract_design_pattern/AndroidDevFactory.java: -------------------------------------------------------------------------------- 1 | public class AndroidDevFactory extends EmployeeAbstractFactory { 2 | 3 | @Override 4 | public Employee createEmployee() { 5 | 6 | return new AndroidDeveloper(); 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /abstract_design_pattern/AndroidDeveloper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/AndroidDeveloper.class -------------------------------------------------------------------------------- /abstract_design_pattern/AndroidDeveloper.java: -------------------------------------------------------------------------------- 1 | public class AndroidDeveloper implements Employee { 2 | 3 | public int salary() { 4 | return 5000; 5 | } 6 | 7 | public String name() { 8 | System.out.println("I am android developer"); 9 | return "ANDROID DEVELOPER"; 10 | } 11 | 12 | } 13 | -------------------------------------------------------------------------------- /abstract_design_pattern/Client.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/Client.class -------------------------------------------------------------------------------- /abstract_design_pattern/Client.java: -------------------------------------------------------------------------------- 1 | public class Client { 2 | 3 | public static void main(String[] args) { 4 | 5 | // i want to get android developer 6 | 7 | Employee e1 = EmployeeFactory.getEmployee(new AndroidDevFactory()); 8 | e1.name(); 9 | 10 | Employee e2 = EmployeeFactory.getEmployee(new WebDevFactory()); 11 | 12 | e2.name(); 13 | 14 | Employee e3 = EmployeeFactory.getEmployee(new ManagerFactory()); 15 | e3.name(); 16 | 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /abstract_design_pattern/Employee.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/Employee.class -------------------------------------------------------------------------------- /abstract_design_pattern/Employee.java: -------------------------------------------------------------------------------- 1 | interface Employee { 2 | 3 | int salary(); 4 | 5 | String name(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /abstract_design_pattern/EmployeeAbstractFactory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/EmployeeAbstractFactory.class -------------------------------------------------------------------------------- /abstract_design_pattern/EmployeeAbstractFactory.java: -------------------------------------------------------------------------------- 1 | abstract public class EmployeeAbstractFactory { 2 | 3 | public abstract Employee createEmployee(); 4 | 5 | } 6 | -------------------------------------------------------------------------------- /abstract_design_pattern/EmployeeFactory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/EmployeeFactory.class -------------------------------------------------------------------------------- /abstract_design_pattern/EmployeeFactory.java: -------------------------------------------------------------------------------- 1 | public class EmployeeFactory { 2 | 3 | // get employee 4 | 5 | public static Employee getEmployee(EmployeeAbstractFactory factory) { 6 | return factory.createEmployee(); 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /abstract_design_pattern/Manager.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/Manager.class -------------------------------------------------------------------------------- /abstract_design_pattern/Manager.java: -------------------------------------------------------------------------------- 1 | public class Manager implements Employee { 2 | 3 | @Override 4 | public int salary() { 5 | return 100000; 6 | } 7 | 8 | @Override 9 | public String name() { 10 | System.out.println("I am manager"); 11 | return "MANGER"; 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /abstract_design_pattern/ManagerFactory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/ManagerFactory.class -------------------------------------------------------------------------------- /abstract_design_pattern/ManagerFactory.java: -------------------------------------------------------------------------------- 1 | public class ManagerFactory extends EmployeeAbstractFactory { 2 | 3 | @Override 4 | public Employee createEmployee() { 5 | return new Manager(); 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /abstract_design_pattern/WebDevFactory.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/WebDevFactory.class -------------------------------------------------------------------------------- /abstract_design_pattern/WebDevFactory.java: -------------------------------------------------------------------------------- 1 | public class WebDevFactory extends EmployeeAbstractFactory { 2 | 3 | @Override 4 | public Employee createEmployee() { 5 | return new WebDeveloper(); 6 | 7 | } 8 | 9 | } 10 | -------------------------------------------------------------------------------- /abstract_design_pattern/WebDeveloper.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/abstract_design_pattern/WebDeveloper.class -------------------------------------------------------------------------------- /abstract_design_pattern/WebDeveloper.java: -------------------------------------------------------------------------------- 1 | public class WebDeveloper implements Employee { 2 | public int salary() { 3 | return 4000; 4 | } 5 | 6 | public String name() { 7 | System.out.println("I am web developer"); 8 | return "WEB DEVELOPER"; 9 | } 10 | 11 | } 12 | -------------------------------------------------------------------------------- /adapter/AdapterCharger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/adapter/AdapterCharger.class -------------------------------------------------------------------------------- /adapter/AdapterCharger.java: -------------------------------------------------------------------------------- 1 | public class AdapterCharger implements AppleCharger { 2 | 3 | private AndroidCharger charger; 4 | 5 | public AdapterCharger(AndroidCharger charger) { 6 | this.charger = charger; 7 | } 8 | 9 | @Override 10 | public void chargePhone() { 11 | charger.chargerAndroidPhone(); 12 | System.out.println("you phone is charging with adapter"); 13 | 14 | } 15 | 16 | } 17 | -------------------------------------------------------------------------------- /adapter/AndroidCharger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/adapter/AndroidCharger.class -------------------------------------------------------------------------------- /adapter/AndroidCharger.java: -------------------------------------------------------------------------------- 1 | interface AndroidCharger { 2 | 3 | void chargerAndroidPhone(); 4 | 5 | } 6 | -------------------------------------------------------------------------------- /adapter/AppleCharger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/adapter/AppleCharger.class -------------------------------------------------------------------------------- /adapter/AppleCharger.java: -------------------------------------------------------------------------------- 1 | interface AppleCharger { 2 | 3 | void chargePhone(); 4 | 5 | } 6 | -------------------------------------------------------------------------------- /adapter/ChargerXYZ.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/adapter/ChargerXYZ.class -------------------------------------------------------------------------------- /adapter/ChargerXYZ.java: -------------------------------------------------------------------------------- 1 | public class ChargerXYZ implements AppleCharger { 2 | 3 | @Override 4 | public void chargePhone() { 5 | 6 | System.out.println("your iphone is charging"); 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /adapter/Demo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/adapter/Demo.class -------------------------------------------------------------------------------- /adapter/Demo.java: -------------------------------------------------------------------------------- 1 | class Demo { 2 | public static void main(String[] args) { 3 | 4 | System.out.println("program started "); 5 | 6 | AppleCharger charger = new AdapterCharger(new DkCharger()); 7 | 8 | Iphone13 iphone13 = new Iphone13(charger); 9 | 10 | iphone13.chargeIphone(); 11 | 12 | } 13 | } -------------------------------------------------------------------------------- /adapter/DkCharger.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/adapter/DkCharger.class -------------------------------------------------------------------------------- /adapter/DkCharger.java: -------------------------------------------------------------------------------- 1 | public class DkCharger implements AndroidCharger { 2 | 3 | @Override 4 | public void chargerAndroidPhone() { 5 | 6 | System.out.println("Your android phone is charging"); 7 | 8 | } 9 | 10 | } 11 | -------------------------------------------------------------------------------- /adapter/Iphone13.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/adapter/Iphone13.class -------------------------------------------------------------------------------- /adapter/Iphone13.java: -------------------------------------------------------------------------------- 1 | public class Iphone13 { 2 | 3 | private AppleCharger appleCharger; 4 | 5 | public Iphone13(AppleCharger appleCharger) { 6 | this.appleCharger = appleCharger; 7 | } 8 | 9 | public void chargeIphone() { 10 | 11 | appleCharger.chargePhone(); 12 | 13 | } 14 | 15 | } 16 | -------------------------------------------------------------------------------- /backgrounds(4).png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/backgrounds(4).png -------------------------------------------------------------------------------- /builder/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/builder/Main.class -------------------------------------------------------------------------------- /builder/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) { 3 | 4 | User user = new User.UserBuilder() 5 | .setEmailId("durgesh@dev.in") 6 | .setUserId("USERID123") 7 | .setUserName("Durgesh") 8 | .build(); 9 | 10 | System.out.println(user); 11 | 12 | User user2 = User.UserBuilder.builder() 13 | .setUserId("USER1234") 14 | .setEmailId("Ankit@gmail.com") 15 | 16 | .build(); 17 | 18 | System.out.println(user2); 19 | 20 | 21 | } 22 | } 23 | -------------------------------------------------------------------------------- /builder/User$UserBuilder.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/builder/User$UserBuilder.class -------------------------------------------------------------------------------- /builder/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/builder/User.class -------------------------------------------------------------------------------- /builder/User.java: -------------------------------------------------------------------------------- 1 | class User { 2 | 3 | private final String userId; 4 | private final String userName; 5 | private final String emailId; 6 | 7 | private User(UserBuilder builder) { 8 | // intialize 9 | this.userId = builder.userId; 10 | this.userName = builder.userName; 11 | this.emailId = builder.emailId; 12 | 13 | } 14 | 15 | public String getUserId() { 16 | return userId; 17 | } 18 | 19 | public String getUserName() { 20 | return userName; 21 | } 22 | 23 | public String getEmailId() { 24 | return emailId; 25 | } 26 | 27 | // inner class to create obejct 28 | 29 | @Override 30 | public String toString() { 31 | 32 | return this.userName + " : " + this.emailId + " : " + this.userId; 33 | } 34 | 35 | static class UserBuilder { 36 | 37 | private String userId; 38 | private String userName; 39 | private String emailId; 40 | 41 | public UserBuilder() { 42 | 43 | } 44 | 45 | public static UserBuilder builder() { 46 | return new UserBuilder(); 47 | } 48 | 49 | public UserBuilder setUserId(String userId) { 50 | this.userId = userId; 51 | return this; 52 | } 53 | 54 | public UserBuilder setUserName(String userName) { 55 | this.userName = userName; 56 | return this; 57 | 58 | } 59 | 60 | public UserBuilder setEmailId(String emailId) { 61 | this.emailId = emailId; 62 | return this; 63 | } 64 | 65 | public User build() { 66 | User user = new User(this); 67 | return user; 68 | } 69 | 70 | } 71 | 72 | } -------------------------------------------------------------------------------- /images/abstract design factory.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/images/abstract design factory.png -------------------------------------------------------------------------------- /images/factorypattern.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/images/factorypattern.png -------------------------------------------------------------------------------- /images/observer_pattern.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/images/observer_pattern.png -------------------------------------------------------------------------------- /iterator/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/iterator/Main.class -------------------------------------------------------------------------------- /iterator/Main.java: -------------------------------------------------------------------------------- 1 | class Main { 2 | public static void main(String[] args) { 3 | 4 | UserManagement userManagement = new UserManagement(); 5 | 6 | userManagement.addUser(new User("durgesh", "14")); 7 | userManagement.addUser(new User("harsh", "15")); 8 | userManagement.addUser(new User("ankit", "16")); 9 | userManagement.addUser(new User("gautam", "74")); 10 | 11 | MyIterator myIterator = userManagement.getIterator(); 12 | while (myIterator.hasNext()) { 13 | User user = (User) myIterator.next(); 14 | System.out.println(user.getName()); 15 | } 16 | 17 | } 18 | } -------------------------------------------------------------------------------- /iterator/MyIterator.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/iterator/MyIterator.class -------------------------------------------------------------------------------- /iterator/MyIterator.java: -------------------------------------------------------------------------------- 1 | public interface MyIterator { 2 | 3 | boolean hasNext(); 4 | 5 | Object next(); 6 | 7 | } 8 | -------------------------------------------------------------------------------- /iterator/MyIteratorImpl.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/iterator/MyIteratorImpl.class -------------------------------------------------------------------------------- /iterator/MyIteratorImpl.java: -------------------------------------------------------------------------------- 1 | import java.util.List; 2 | 3 | public class MyIteratorImpl implements MyIterator { 4 | 5 | private List list; 6 | private int length; 7 | 8 | private int position = 0; 9 | 10 | public MyIteratorImpl(List list) { 11 | this.list = list; 12 | this.length = list.size(); 13 | } 14 | 15 | @Override 16 | public boolean hasNext() { 17 | if (position >= length) 18 | return false; 19 | else 20 | return true; 21 | 22 | } 23 | 24 | @Override 25 | public Object next() { 26 | User user = list.get(position); 27 | position += 1; 28 | return user; 29 | } 30 | 31 | } 32 | -------------------------------------------------------------------------------- /iterator/User.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/iterator/User.class -------------------------------------------------------------------------------- /iterator/User.java: -------------------------------------------------------------------------------- 1 | public class User { 2 | 3 | private String name; 4 | private String userId; 5 | 6 | public User(String name, String userId) { 7 | this.name = name; 8 | this.userId = userId; 9 | } 10 | 11 | public String getName() { 12 | return name; 13 | } 14 | 15 | public void setName(String name) { 16 | this.name = name; 17 | } 18 | 19 | public String getUserId() { 20 | return userId; 21 | } 22 | 23 | public void setUserId(String userId) { 24 | this.userId = userId; 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /iterator/UserManagement.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/iterator/UserManagement.class -------------------------------------------------------------------------------- /iterator/UserManagement.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | 3 | public class UserManagement { 4 | 5 | private ArrayList userList = new ArrayList<>(); 6 | 7 | public void addUser(User user) { 8 | userList.add(user); 9 | } 10 | 11 | public User getUser(int index) { 12 | return userList.get(index); 13 | } 14 | 15 | public MyIterator getIterator() { 16 | 17 | return new MyIteratorImpl(userList); 18 | } 19 | } 20 | -------------------------------------------------------------------------------- /note.md: -------------------------------------------------------------------------------- 1 | # Singleton Design Pattern 2 | 3 | The Singleton design pattern is a creational design pattern that restricts the instantiation of a class to a single object and provides global access to that instance throughout the application. This pattern ensures that only one instance of a class is created and provides a global point of access to it. 4 | 5 | ## Singleton Object 6 | 7 | Singleton object are the object which are instantiated only once for project (jvm). 8 | If we try to get the object then we get same object again and again. 9 | 10 | ### lets create singleton patter using java 11 | 12 | Lazy way of creating singleton object 13 | 14 | ```java 15 | class Example{ 16 | 17 | 18 | private static Example ob; 19 | 20 | public static Example getExample(){ 21 | if(ob==null){ 22 | ob=new Example(); 23 | } 24 | return ob; 25 | } 26 | 27 | 28 | } 29 | 30 | ``` 31 | 32 | ### calling the singleton object 33 | 34 | ```java 35 | class Main 36 | { 37 | public static void main(String args[]){ 38 | Example ob=Example.getExample() 39 | //using the object 40 | 41 | } 42 | 43 | } 44 | ``` 45 | 46 | Eager way of creating Singleton object 47 | 48 | ```java 49 | class Example{ 50 | 51 | 52 | private static Example ob=new Example(); 53 | 54 | public static Example getExample(){ 55 | return ob; 56 | } 57 | 58 | 59 | } 60 | 61 | ``` 62 | 63 | Accessing object 64 | 65 | ```java 66 | 67 | class Main 68 | { 69 | public static void main(String args[]){ 70 | Example ob=Example.getExample() 71 | //using the object 72 | 73 | } 74 | } 75 | } 76 | 77 | ``` 78 | 79 | > note: for multithreaded environment we use syncronized block for creating singleton object. 80 | 81 | ```java 82 | class Example{ 83 | 84 | 85 | private static Example ob; 86 | 87 | public static Example getExample(){ 88 | if(ob==null){ 89 | syncronized(Example.class){ 90 | if(ob==null) 91 | { 92 | ob=new Example(); 93 | } 94 | } 95 | } 96 | return ob; 97 | } 98 | 99 | 100 | } 101 | 102 | 103 | ``` 104 | 105 | ## Breaking Singleton Design Pattern 106 | 107 | ### There are three ways to break singleton design pattern . Lets talk about the these way and i am also going to tell you about the solution of these problems. 108 | 109 | ## 1. Using Reflection API 110 | 111 | With the help of relfection api we can call private constructor as well and create multiple object by calling private constructor. 112 | 113 | ### lets see how we can call private constructor 114 | 115 | ```java 116 | 117 | Constructor constructor=Example.class.getDeclaredConstructor() 118 | 119 | //changing the accessibility to true 120 | constructor.setAccessible(true) 121 | 122 | Example example=constructor.newInstance(); 123 | ``` 124 | 125 | ## solution 126 | 127 | ### we can do the soultion in two ways. 128 | 129 | 1. using ENUM 130 | 131 | ```java 132 | 133 | public enum Example{ 134 | INSTANCE 135 | } 136 | 137 | ``` 138 | 139 | 2. check the object in private constructor if the object exists then throw exception to terminate the execution. 140 | 141 | ```java 142 | 143 | 144 | private Exmaple(){ 145 | 146 | if(ob!=null) 147 | { 148 | throw new RuntimeExcepiton("you are trying to break singleton pattern") 149 | } 150 | } 151 | 152 | 153 | 154 | ``` 155 | 156 | ## 2. Using Deserialization 157 | 158 | when we serialze and deserialze the singleton object then singleton automatically got destroyed and provide us different object. 159 | 160 | ```java 161 | ObjectOutputStream oos = new ObjectOutputStream(new 162 | FileOutputStream("abc.ob")); 163 | oos.writeObject(ob); 164 | 165 | System.out.println("serialization done.."); 166 | 167 | ObjectInputStream ois = new ObjectInputStream(new FileInputStream("abc.ob")); 168 | Example s2 = (Example) ois.readObject(); 169 | System.out.println(s2.hashCode()); 170 | 171 | 172 | 173 | ``` 174 | 175 | ### solution: 176 | 177 | just implement readResolve() method 178 | 179 | ```java 180 | public Object readResolve() { 181 | return ob; 182 | } 183 | 184 | ``` 185 | 186 | ## 3. Using cloning 187 | 188 | when we clone then also we get different object. 189 | 190 | ## solution 191 | 192 | just override clone method and return the same instance. 193 | 194 | ```java 195 | 196 | @Override 197 | public Object clone() throws CloneNotSupportedException { 198 | return samosa; 199 | } 200 | 201 | ``` 202 | 203 | # Factory Design Pattern 204 | 205 | ### Factory Method Design Pattern 206 | 207 | When there is superclass and multiple subclasses and we want to get object of subclasses based on input and requirement. 208 | 209 | Then we create factory class which takes the responsibility of creating object of class based on input. 210 | 211 | ## Advantages of Factory Design Pattern 212 | 213 | 1. Focus on creating object for Interface rather than implementation. 214 | 215 | 2. Loose coupling, more robust code 216 | 217 | ![Alt text](images/factorypattern.png) 218 | 219 | # Abstract Factory Design Pattern 220 | 221 | Similar to Factory Pattern 222 | 223 | It provide the concept of Factory of Factories. 224 | 225 | ![Alt text](images/abstract%20design%20factory.png) 226 | 227 | # Builder Design Pattern 228 | 229 | while creating object when object contain may attributes 230 | there are many problem exists : 231 | 232 | 1. we have to pass many arguments to create object. 233 | 2. some parameters might be optional 234 | 3. factory class takes all responsibility for creating object . if the object is heavy then all complexity is the part of factory class. 235 | 236 | ### So in builder pattern be create object step by step and finally return final object with desired values of attributes. 237 | 238 | --- 239 | 240 | ## Prototype Design Pattern 241 | 242 | The concept is to copy an existing object rather than creating a new instance from scratch. because creating new object may be costly. 243 | 244 | This approach saves costly resources and time, especially when object creation is a heavy process. 245 | 246 | # Observer Design Pattern 247 | 248 | - It is behavioural Design pattern. 249 | - In this when subject changes the state all its dependent objects notified the changes. 250 | - one to many relation. 251 | 252 | ![Alt text](images/observer_pattern.png) 253 | 254 | ## Iterator Design Pattern 255 | 256 | The iterator pattern provides a way to access the elements of an object without exposing its underlying implementation. 257 | 258 | --- 259 | 260 | ## Adapter Design Pattern 261 | -------------------------------------------------------------------------------- /observer/Demo.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/observer/Demo.class -------------------------------------------------------------------------------- /observer/Demo.java: -------------------------------------------------------------------------------- 1 | import java.io.BufferedReader; 2 | import java.io.IOException; 3 | import java.io.InputStreamReader; 4 | 5 | public class Demo { 6 | public static void main(String[] args) throws NumberFormatException, IOException { 7 | 8 | Subject channel = new YoutubeChannel(); 9 | 10 | Observer aman = new Subsriber("Aman"); 11 | Observer raman = new Subsriber("Raman"); 12 | 13 | channel.subscribe(aman); 14 | channel.subscribe(raman); 15 | 16 | channel.newVideoUploaded("Learn Design Pattern"); 17 | channel.newVideoUploaded("New Angular Course"); 18 | 19 | BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); 20 | 21 | while (true) { 22 | 23 | System.out.println("Press 1 to upload Video"); 24 | System.out.println("Press 2 create new Subscriber"); 25 | System.out.println("Press 3 to exit"); 26 | 27 | int c = Integer.parseInt(br.readLine()); 28 | if (c == 1) { 29 | // new video upload code 30 | 31 | System.out.println("Enter video title "); 32 | String videoTitle = br.readLine(); 33 | channel.newVideoUploaded(videoTitle); 34 | 35 | } else if (c == 2) { 36 | /// create new subscribe 37 | System.out.println("Enter name of subscriber: "); 38 | String subsName = br.readLine(); 39 | Observer subsriber3 = new Subsriber(subsName); 40 | channel.subscribe(subsriber3); 41 | 42 | } else if (c == 3) { 43 | // exit 44 | System.out.println("Thankyou for using app"); 45 | break; 46 | } else { 47 | // exit wrong input 48 | System.out.println("Wrong input"); 49 | } 50 | 51 | } 52 | 53 | } 54 | } 55 | -------------------------------------------------------------------------------- /observer/Observer.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/observer/Observer.class -------------------------------------------------------------------------------- /observer/Observer.java: -------------------------------------------------------------------------------- 1 | public interface Observer { 2 | 3 | void notified(String title); 4 | 5 | } 6 | -------------------------------------------------------------------------------- /observer/Subject.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/observer/Subject.class -------------------------------------------------------------------------------- /observer/Subject.java: -------------------------------------------------------------------------------- 1 | interface Subject { 2 | 3 | void subscribe(Observer ob); 4 | 5 | void unsubscribe(Observer ob); 6 | 7 | void newVideoUploaded(String title); 8 | 9 | } -------------------------------------------------------------------------------- /observer/Subsriber.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/observer/Subsriber.class -------------------------------------------------------------------------------- /observer/Subsriber.java: -------------------------------------------------------------------------------- 1 | public class Subsriber implements Observer { 2 | 3 | String name; 4 | 5 | Subsriber(String name) { 6 | this.name = name; 7 | } 8 | 9 | @Override 10 | public void notified(String title) { 11 | System.out.println("Hello " + this.name + " new video upload : " + title); 12 | } 13 | 14 | } 15 | -------------------------------------------------------------------------------- /observer/YoutubeChannel.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/observer/YoutubeChannel.class -------------------------------------------------------------------------------- /observer/YoutubeChannel.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class YoutubeChannel implements Subject { 5 | 6 | List subcribers = new ArrayList<>(); 7 | 8 | @Override 9 | public void subscribe(Observer ob) { 10 | this.subcribers.add(ob); 11 | } 12 | 13 | @Override 14 | public void unsubscribe(Observer ob) { 15 | this.subcribers.remove(ob); 16 | } 17 | 18 | @Override 19 | public void newVideoUploaded(String title) { 20 | 21 | for (Observer ob : this.subcribers) { 22 | ob.notified(title); 23 | } 24 | 25 | } 26 | 27 | } 28 | -------------------------------------------------------------------------------- /prototype/Main.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/prototype/Main.class -------------------------------------------------------------------------------- /prototype/Main.java: -------------------------------------------------------------------------------- 1 | public class Main { 2 | public static void main(String[] args) throws InterruptedException { 3 | 4 | System.out.println("Creating object using prototype design"); 5 | 6 | NetworkConnection networkConnection = new NetworkConnection(); 7 | networkConnection.setIp("192.168.4.4"); 8 | networkConnection.loadVeryImportantData(); 9 | 10 | // we want new object of network connection 11 | 12 | try { 13 | NetworkConnection networkConnection2 = (NetworkConnection) networkConnection.clone(); 14 | NetworkConnection networkConnection3 = (NetworkConnection) networkConnection.clone(); 15 | 16 | System.out.println(networkConnection); 17 | 18 | networkConnection.getDomains().remove(0); 19 | 20 | System.out.println(networkConnection); 21 | System.out.println(networkConnection2); 22 | System.out.println(networkConnection3); 23 | 24 | } catch (CloneNotSupportedException e) { 25 | e.printStackTrace(); 26 | } 27 | 28 | } 29 | } 30 | -------------------------------------------------------------------------------- /prototype/NetworkConnection.class: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/LearnCodeWithDurgesh/Design-Pattern-Playlist/d891f8f81b04b94b66c5e0b5a4396db31b1928e7/prototype/NetworkConnection.class -------------------------------------------------------------------------------- /prototype/NetworkConnection.java: -------------------------------------------------------------------------------- 1 | import java.util.ArrayList; 2 | import java.util.List; 3 | 4 | public class NetworkConnection implements Cloneable { 5 | 6 | private String ip; 7 | private String importantData; 8 | private List domains = new ArrayList<>(); 9 | 10 | public String getIp() { 11 | return ip; 12 | } 13 | 14 | public void setIp(String ip) { 15 | this.ip = ip; 16 | } 17 | 18 | public String getImportantData() { 19 | return importantData; 20 | } 21 | 22 | public void setImportantData(String importantData) { 23 | this.importantData = importantData; 24 | } 25 | 26 | public void loadVeryImportantData() throws InterruptedException { 27 | this.importantData = "Very very important data"; 28 | 29 | domains.add("www.learncodewithdurgesh.com"); 30 | domains.add("www.substringtechnologies.com"); 31 | domains.add("www.lcwd.com"); 32 | domains.add("www.google.com"); 33 | 34 | Thread.sleep(5000); 35 | // it will take 5 minutes 36 | } 37 | 38 | @Override 39 | public String toString() { 40 | 41 | return this.ip + " : " + this.importantData + " : " + this.domains; 42 | } 43 | 44 | @Override 45 | protected Object clone() throws CloneNotSupportedException { 46 | // logic for cloning 47 | NetworkConnection networkConnection = new NetworkConnection(); 48 | networkConnection.setIp(this.getIp()); 49 | networkConnection.setImportantData(this.getImportantData()); 50 | for (String d : this.getDomains()) { 51 | networkConnection.getDomains().add(d); 52 | } 53 | 54 | return networkConnection; 55 | } 56 | 57 | public List getDomains() { 58 | return domains; 59 | } 60 | 61 | public void setDomains(List domains) { 62 | this.domains = domains; 63 | } 64 | 65 | } 66 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | > > Use this guide with videos uploaded on youtube to get maximum out of it. 2 | 3 | > > [Click here to watch full series](https://www.youtube.com/playlist?list=PL0zysOflRCek8kmc_jYl_6C7tpud7U2V_) 4 | 5 | # Singleton Design Pattern 6 | 7 | The Singleton design pattern is a creational design pattern that restricts the instantiation of a class to a single object and provides global access to that instance throughout the application. This pattern ensures that only one instance of a class is created and provides a global point of access to it. 8 | 9 | ## Singleton Object 10 | 11 | Singleton object are the object which are instantiated only once for project (jvm). 12 | If we try to get the object then we get same object again and again. 13 | 14 | ### lets create singleton patter using java 15 | 16 | Lazy way of creating singleton object 17 | 18 | ```java 19 | class Example{ 20 | 21 | 22 | private static Example ob; 23 | 24 | public static Example getExample(){ 25 | if(ob==null){ 26 | ob=new Example(); 27 | } 28 | return ob; 29 | } 30 | 31 | 32 | } 33 | 34 | ``` 35 | 36 | ### calling the singleton object 37 | 38 | ```java 39 | class Main 40 | { 41 | public static void main(String args[]){ 42 | Example ob=Example.getExample() 43 | //using the object 44 | 45 | } 46 | 47 | } 48 | ``` 49 | 50 | Eager way of creating Singleton object 51 | 52 | ```java 53 | class Example{ 54 | 55 | 56 | private static Example ob=new Example(); 57 | 58 | public static Example getExample(){ 59 | return ob; 60 | } 61 | 62 | 63 | } 64 | 65 | ``` 66 | 67 | Accessing object 68 | 69 | ```java 70 | 71 | class Main 72 | { 73 | public static void main(String args[]){ 74 | Example ob=Example.getExample() 75 | //using the object 76 | 77 | } 78 | } 79 | } 80 | 81 | ``` 82 | 83 | > note: for multithreaded environment we use syncronized block for creating singleton object. 84 | 85 | ```java 86 | class Example{ 87 | 88 | 89 | private static Example ob; 90 | 91 | public static Example getExample(){ 92 | if(ob==null){ 93 | syncronized(Example.class){ 94 | if(ob==null) 95 | { 96 | ob=new Example(); 97 | } 98 | } 99 | } 100 | return ob; 101 | } 102 | 103 | 104 | } 105 | 106 | 107 | ``` 108 | 109 | ## Breaking Singleton Design Pattern 110 | 111 | ### There are three ways to break singleton design pattern . Lets talk about the these way and i am also going to tell you about the solution of these problems. 112 | 113 | ## 1. Using Reflection API 114 | 115 | With the help of relfection api we can call private constructor as well and create multiple object by calling private constructor. 116 | 117 | ### lets see how we can call private constructor 118 | 119 | ```java 120 | 121 | Constructor constructor=Example.class.getDeclaredConstructor() 122 | 123 | //changing the accessibility to true 124 | constructor.setAccessible(true) 125 | 126 | Example example=constructor.newInstance(); 127 | ``` 128 | 129 | ## solution 130 | 131 | ### we can do the soultion in two ways. 132 | 133 | 1. using ENUM 134 | 135 | ```java 136 | 137 | public enum Example{ 138 | INSTANCE 139 | } 140 | 141 | ``` 142 | 143 | 2. check the object in private constructor if the object exists then throw exception to terminate the execution. 144 | 145 | ```java 146 | 147 | 148 | private Exmaple(){ 149 | 150 | if(ob!=null) 151 | { 152 | throw new RuntimeExcepiton("you are trying to break singleton pattern") 153 | } 154 | } 155 | 156 | 157 | 158 | ``` 159 | 160 | ## 2. Using Deserialization 161 | 162 | when we serialze and deserialze the singleton object then singleton automatically got destroyed and provide us different object. 163 | 164 | ```java 165 | ObjectOutputStream oos = new ObjectOutputStream(new 166 | FileOutputStream("abc.ob")); 167 | oos.writeObject(ob); 168 | 169 | System.out.println("serialization done.."); 170 | 171 | ObjectInputStream ois = new ObjectInputStream(new FileInputStream("abc.ob")); 172 | Example s2 = (Example) ois.readObject(); 173 | System.out.println(s2.hashCode()); 174 | 175 | 176 | 177 | ``` 178 | 179 | ### solution: 180 | 181 | just implement readResolve() method 182 | 183 | ```java 184 | public Object readResolve() { 185 | return ob; 186 | } 187 | 188 | ``` 189 | 190 | ## 3. Using cloning 191 | 192 | when we clone then also we get different object. 193 | 194 | ## solution 195 | 196 | just override clone method and return the same instance. 197 | 198 | ```java 199 | 200 | @Override 201 | public Object clone() throws CloneNotSupportedException { 202 | return samosa; 203 | } 204 | 205 | ``` 206 | 207 | # Factory Design Pattern 208 | 209 | ### Factory Method Design Pattern 210 | 211 | When there is superclass and multiple subclasses and we want to get object of subclasses based on input and requirement. 212 | 213 | Then we create factory class which takes the responsibility of creating object of class based on input. 214 | 215 | ## Advantages of Factory Design Pattern 216 | 217 | 1. Focus on creating object for Interface rather than implementation. 218 | 219 | 2. Loose coupling, more robust code 220 | 221 | ![Alt text](images/factorypattern.png) 222 | 223 | # Abstract Factory Design Pattern 224 | 225 | Similar to Factory Pattern 226 | 227 | It provide the concept of Factory of Factories. 228 | 229 | ![Alt text](images/abstract%20design%20factory.png) 230 | 231 | # Builder Design Pattern 232 | 233 | while creating object when object contain may attributes 234 | there are many problem exists : 235 | 236 | 1. we have to pass many arguments to create object. 237 | 2. some parameters might be optional 238 | 3. factory class takes all responsibility for creating object . if the object is heavy then all complexity is the part of factory class. 239 | 240 | ### So in builder pattern be create object step by step and finally return final object with desired values of attributes. 241 | 242 | --- 243 | 244 | ## Prototype Design Pattern 245 | 246 | The concept is to copy an existing object rather than creating a new instance from scratch. because creating new object may be costly. 247 | 248 | This approach saves costly resources and time, especially when object creation is a heavy process. 249 | 250 | # Observer Design Pattern 251 | 252 | - It is behavioural Design pattern. 253 | - In this when subject changes the state all its dependent objects notified the changes. 254 | - one to many relation. 255 | 256 | ![Alt text](images/observer_pattern.png) 257 | --------------------------------------------------------------------------------