├── CONTRIBUTING.md ├── LICENSE ├── README.md ├── _config.yml └── com ├── abstractFactory ├── AbstractFactory.java ├── Computer.java ├── ComputerFactory.java ├── PC.java ├── PCFactory.java ├── Server.java └── ServerFactory.java ├── builder └── Computer.java ├── factory ├── Computer.java ├── FactoryClass.java ├── PC.java └── Server.java ├── patternObsTest ├── Jobservers.java ├── Jsubject.java ├── Observer.java ├── Subject.java └── valLength.java └── singleton └── SingletonTradeSafe.java /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | # How to contribute ? 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2017 Sonkeng Maldini 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Java Design Patterns 2 | 3 | Design Patterns are very popular among software developers. A design pattern is a well-described solution to 4 | common software problem. 5 | 6 | Some of benefits of using design patterns are : 7 | * Design patterns are already defined and provides industry standard approach to solve recurring problem, 8 | so it saves time if we use the design pattern . 9 | * Using design pattern promotes re-usability that leads to more robust and highly maintainable code. 10 | * Since design patterns are already defined, it makes out code easy to understand and debug. It lead to faster development and new members of team understand it easily. 11 | 12 | 13 | __What is a Design Pattern ?__ 14 | > A software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design --- Wikipedia 15 | 16 | __Java Design Patterns__ are divided into tree parts : __Creational__, __Structural__ and __Behavioral__. 17 | 18 | ## CREATIONAL DESIGN PATTERNS 19 | 20 | Creational design pattens provide solution to instantiate an object in the best possible way for specific situations. 21 | The basic form of object creation could result in design problems or add unwanted complexity to the design. Creational design patterns solve this problem by __controlling the object creation__ by different ways. 22 | There are five creational design patterns that we will discuss on : 23 | 24 | * Singleton Pattern 25 | * Factory Pattern 26 | * Abstract Factory Pattern 27 | * Builder Pattern 28 | * Prototype Pattern 29 | 30 | ### Pattern Singleton 31 | 32 | Pattern Singleton: > One Class, one Instance. 33 | 34 | Singleton is one of the Gangs of Four Design patterns and comes in the Creational Design Pattern category. 35 | Singleton pattern restricts the instantiation of a class and ensures that only one instance of the class exists in the Java virtual machine. The singleton class must provide a global access point to get the instance of the class. Singleton pattern is used for logging, driver objects, caching and thread pool. Singleton design pattern is also used in other design patterns like __Abstract Factory__, __Builder__, __Prototype__, __Facade__ etc. Singleton design pattern is used in core Java classes also, for example __java.lang.Runtime__ , __java.awt.Desktop__. 36 | 37 | To implement Singleton pattern, there are really many approaches but all of them have following common concepts: 38 | 39 | * A private constructor to avoid instantiation of the class, 40 | * A private static variable from the same class that's the only instance of the class. 41 | * public static method that returns the instance of the class, this is the global access point for the outer world to 42 | get the instance of the class. 43 | 44 | We'll implement the thread safe one here. Classes are in the package `com.singleton`; 45 | 46 | ```java 47 | import com.singleton.SingletonThreadSafe; 48 | 49 | public class SingletonTest { 50 | public static void main(String[] args){ 51 | SingletonThreadSafe sing = SingletonThreadSafe.getInstance(); 52 | System.out.println(sing); 53 | 54 | // Now let's instanciate another class 55 | 56 | SingletonThreadSafe sing1 = SingletonThreadSafe.getInstance(); 57 | 58 | System.out.println(sing1); 59 | 60 | //Now check out your console.... What the hell, the two instances have the same reference :o 61 | } 62 | } 63 | ``` 64 | ### Pattern Factory 65 | 66 | Factory design pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern take out the responsibility of instantiation of a class from client program to the factory class. Let’s first learn how to implement factory pattern in Java and then we will learn its benefits and we will see its usage in JDK. 67 | 68 | * Super Class : Super class in factory pattern can be an interface, abstract class or a normal Java class. For our example, we have super class as abstract class with overridden toString() method for testing purpose. 69 | see `com.factory`. 70 | * sub-classes: Let’s say we have two sub-classes PC and Server with implementation in `com.factory` 71 | 72 | Here's what we have in image : 73 | [](http://zupimages.net/up/17/13/7w9z.png) 74 | 75 | Now let's write the test class. 76 | 77 | ```java 78 | import com.factory.FactoryClass ; //The factory class 79 | import com.factory.PC; //sub-class 80 | import com.factory.Server; //sub-class 81 | //PC and Server classes extend from Computer. 82 | 83 | public static void main(String[] args){ 84 | 85 | FactoryClass fc = new FactoryClass(); 86 | Computer comp1 = fc.getComputer("PC",16,499,4.3); 87 | System.out.println(comp1); 88 | 89 | 90 | Computer comp2 = fc.getComputer("Server",30,900,9); 91 | System.out.println(comp2); 92 | 93 | //Now you can see the output in your console. 94 | } 95 | ``` 96 | This pattern provides some advantages such as : 97 | 98 | * It provides approach to code for the interface rather than the implementation. 99 | * It removes the instantiation of the actual implementation classes from client code, making it more robust. 100 | * It provides abstraction between implementation and client classes through inheritance. 101 | 102 | As examples of its implementation in JDK we have : 103 | 104 | * *java.util.Calendar*, *ResourceBundle()* and *NumberFormat getInstance()*; 105 | * *valueOf()* method in wrapper classes like Boolean , Integer etc. 106 | 107 | ### Abstract Factory 108 | 109 | This is one of the Creational Pattern and almost similar to Factory Pattern except the fact that it's most like 110 | Factory of factories. If you're familiar with __factory design pattern in java__ , you'll notice that we have a single Factory class that returns the different sub-classes based on the input provided and the factory class uses if-else or switch statement to achieve this. Like our factory pattern post, we will use the same super class and sub-classes. 111 | Codes are available in `com.abstractFactory`. 112 | Here's the implementation of the test class: 113 | 114 | ```java 115 | 116 | public class abstractFactoryTest{ 117 | public static void main(String[] args){ 118 | 119 | Computer c = ComputerFactory.getComputer(new PCFactory("4G","400G","2.9Ghz")); 120 | /* ComputerFactory class contains static method getComputer(abstractFactory fac) */ 121 | System.out.print(c); 122 | 123 | } 124 | } 125 | ``` 126 | 127 | ### Pattern Builder 128 | 129 | Builder pattern is a creational design pattern as Factory Pattern and Abstract Factory Pattern. This pattern was introduced to solve some of the problems with Factory and Abstract Factory patterns when the Object contains a lot of attributes. This pattern deals with a static nested class and then copy all the arguments from the outer class to the Builder class. 130 | The sample code where we have a Computer class and ComputerBuilder to build it are available in the package `com.builder.Computer`. 131 | Here's a test program showing how to use Builder class to get object. 132 | 133 | 134 | ```java 135 | 136 | import com.builder.Computer; 137 | 138 | public class TestBuilderPattenr{ 139 | public static void main(String[] args){ 140 | 141 | Computer comp = new Computer.ComputerBuilder( 142 | "500 GB","2 GB").setBluetoothEnabled(true) 143 | .setGraphicsCardEnabled(true).build(); // -) 144 | ) 145 | } 146 | 147 | } 148 | ``` 149 | There are really various implementations of this pattern in JDK : java.lang.StringBuilder#append() (unsynchronized) java.lang.StringBuffer#append() (synchronized) . 150 | 151 | ### Pattern Prototype 152 | 153 | Prototype pattern is one of the Creational Design pattern, so it provides a mechanism of object creation. Prototype pattern is used when the Object creation is a costly affair and requires a lot of time and resources and you have a similar object already existing. So this pattern provides a mechanism to copy the original object to a new object and then modify it according to our needs. This pattern uses Java cloning to copy the object. 154 | 155 | ```java 156 | 157 | import java.util.ArrayList; 158 | import java.util.List; 159 | public class Users implements Cloneable{ 160 | private List empList; 161 | 162 | public Users(){ 163 | 164 | empList = new ArrayList<>(); 165 | } 166 | 167 | public Users(List list){ 168 | this.empList=list; 169 | } 170 | 171 | //read some data from the database. 172 | public void loadData(){ 173 | empList.add("japak"); 174 | empList.add("King"); 175 | empList.add("David"); 176 | empList.add("Romeo"); 177 | } 178 | 179 | public List getEmpList() { 180 | return empList; 181 | } 182 | 183 | @Override 184 | public Object clone() throws CloneNotSupportedException{ 185 | List temp = new ArrayList(); 186 | for(String s : this.getEmpList()){ 187 | temp.add(s); 188 | } 189 | return new Users(temp); 190 | } 191 | } 192 | 193 | //Notice that the clone method is overridden to provide a deep copy of the users list. 194 | ``` 195 | 196 | Here's the program that will show the benefit of the Prototype pattern usage. 197 | 198 | ```java 199 | public class PrototypePatternTest { 200 | public static void main(String[] args) throws CloneNotSupportedException { 201 | Users usr = new Users(); 202 | user.loadData(); 203 | //Use the clone method to get the User object 204 | Users useNew = (Users) user.clone(); 205 | Users useNew1 = (Users) user.clone(); 206 | List list = useNew.getEmpList(); 207 | list.add("John"); 208 | 209 | List list1 = usersNew1.getEmpList(); 210 | list1.remove("Pankaj"); 211 | System.out.println("users List: "+ users.getEmpList()); 212 | System.out.println("users New List: "+list); 213 | System.out.println("users New1 List: "+list1); 214 | } 215 | } 216 | ``` 217 | 218 | ## STRUCTURAL DESIGN PATTERNS 219 | 220 | Structural Patterns provide different ways to create a class structure, for example using inheritance and composition 221 | to create a large object from small objects. 222 | 223 | ### Adapter Pattern 224 | 225 | This pattern is used in such a way that two unrelated interfaces can work together. The object that joins these unrelated interfaces is called Adapter. As a real life example, we can think of a mobile charger as an adapter because mobile battery needs 3 Volts to charge but the normal socket produces either 120V (US) or 240V (India). So the mobile charger works as an adapter between mobile charging socket and the wall socket. 226 | First of all we'll have two classes : Volt - to measure volts) and Socket : 227 | 228 | ```java 229 | 230 | public class Volt { 231 | private int volts; 232 | public Volt(int v){ 233 | this.volts=v; 234 | } 235 | public int getVolts() { 236 | return volts; 237 | } 238 | public void setVolts(int volts) { 239 | this.volts = volts; 240 | } 241 | } 242 | 243 | 244 | public class Socket { 245 | public Volt getVolt(){ 246 | return new Volt(120); 247 | } 248 | 249 | } 250 | ``` 251 | Now we want to build an adapter that can produce 3 volts, 12 volts and default 120 volts. So first of all we will create an adapter interface with these methods. 252 | 253 | ```java 254 | public interface SocketAdapter { 255 | public Volt get120Volt(); 256 | public Volt get12Volt(); 257 | public Volt get3Volt(); 258 | } 259 | 260 | ``` 261 | 262 | while implementing this pattern, there are two approaches : one that deals with inheritance and another one that deals with Composition. Note that they are almost the same thus, here we'll deal with one with inheritance. Let's implement out adapter class ! 263 | 264 | 265 | ```java 266 | public class SocketClassAdapterImpl extends Socket implements 267 | SocketAdapter{ 268 | @Override 269 | public Volt get120Volt() { 270 | return getVolt(); 271 | } 272 | @Override 273 | public Volt get12Volt() { 274 | Volt v= getVolt(); 275 | return convertVolt(v,10); 276 | } 277 | @Override 278 | public Volt get3Volt() { 279 | Volt v= getVolt(); 280 | return convertVolt(v,40); 281 | } 282 | private Volt convertVolt(Volt v, int i) { 283 | return new Volt(v.getVolts()/i); 284 | } 285 | } 286 | ``` 287 | Now let's see how all this work ! Here's a test Main function to illustrate. 288 | 289 | 290 | ```java 291 | 292 | public class AdapterPatternTest { 293 | 294 | public static void main(String[] args) { 295 | testClassAdapter(); 296 | 297 | testAdapter(); 298 | 299 | //The results goes in your console :) 300 | } 301 | 302 | private static void testAdapter() { 303 | SocketAdapter sockAdapter = new SocketObjectAdapterImpl(); 304 | 305 | Volt v3 = getVolt(sockAdapter,3); 306 | 307 | Volt v12 = getVolt(sockAdapter,12); 308 | 309 | Volt v120 = getVolt(sockAdapter,120); 310 | 311 | System.out.println("v3 volts using Object Adapter="+v3.getVolts()); 312 | System.out.println("v12 volts using Object Adapter="+v12.getVolts()); 313 | System.out.println("v120 volts using Object Adapter="+v120.getVolts()); 314 | } 315 | 316 | private static Volt getVolt(SocketAdapter sockAdapter, int i) { 317 | switch (i){ 318 | case 3: return sockAdapter.get3Volt(); 319 | case 12: return sockAdapter.get12Volt(); 320 | case 120: return sockAdapter.get120Volt(); 321 | default: return sockAdapter.get120Volt(); 322 | } 323 | } 324 | } 325 | ``` 326 | This pattern has many usage in the JDK : 327 | * __java.util.Arrays#asList() java.io.InputStreamReader(InputStream)__ (returns a Reader), 328 | * __java.io.OutputStreamWriter(OutputStream)__ (returns a Writer). 329 | 330 | ### Composite Pattern 331 | -------------------------------------------------------------------------------- /_config.yml: -------------------------------------------------------------------------------- 1 | theme: jekyll-theme-tactile -------------------------------------------------------------------------------- /com/abstractFactory/AbstractFactory.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 21/03/17. 5 | */ 6 | public interface AbstractFactory { 7 | 8 | Computer createComputer(); 9 | } 10 | -------------------------------------------------------------------------------- /com/abstractFactory/Computer.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 18/03/17. 5 | */ 6 | public abstract class Computer { 7 | 8 | 9 | public abstract String getRam(); 10 | public abstract String getCpu(); 11 | public abstract String getHdd(); 12 | 13 | @Override 14 | 15 | public String toString(){ 16 | return "Config :: RAM = " + this.getRam() + " CPU = " + this.getCpu() + " HDD = " + this.getHdd(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /com/abstractFactory/ComputerFactory.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 21/03/17. 5 | */ 6 | public class ComputerFactory { 7 | 8 | public static Computer getComputer(AbstractFactory fact){ 9 | return fact.createComputer(); 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /com/abstractFactory/PC.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 18/03/17. 5 | */ 6 | public class PC extends Computer { 7 | 8 | private String ram; 9 | private String cpu; 10 | private String hdd; 11 | 12 | 13 | public PC(String ram,String hdd, String cpu){ 14 | this.ram = ram; 15 | this.hdd= hdd; 16 | this.cpu = cpu; 17 | } 18 | public String getRam(){ 19 | return this.ram; 20 | } 21 | public String getCpu(){ 22 | return this.cpu; 23 | } 24 | public String getHdd(){return this.hdd;} 25 | 26 | 27 | 28 | 29 | } 30 | -------------------------------------------------------------------------------- /com/abstractFactory/PCFactory.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 21/03/17. 5 | */ 6 | public class PCFactory implements AbstractFactory { 7 | 8 | private String ram; 9 | private String hdd; 10 | 11 | private String cpu; 12 | 13 | 14 | public PCFactory(String r, String h, String c){ 15 | this.ram = r; 16 | this.hdd = h; 17 | this.cpu = c; 18 | } 19 | 20 | public Computer createComputer(){ 21 | return new PC(ram,hdd,cpu); 22 | } 23 | } 24 | -------------------------------------------------------------------------------- /com/abstractFactory/Server.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 18/03/17. 5 | */ 6 | public class Server extends Computer{ 7 | private String ram; 8 | private String cpu; 9 | private String hdd; 10 | 11 | 12 | public Server(String ram,String hdd, String cpu){ 13 | this.ram = ram; 14 | this.hdd= hdd; 15 | this.cpu = cpu; 16 | } 17 | public String getRam(){ 18 | return this.ram; 19 | } 20 | public String getCpu(){ 21 | return this.cpu; 22 | } 23 | 24 | public String getHdd(){ 25 | return this.hdd; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /com/abstractFactory/ServerFactory.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 21/03/17. 5 | */ 6 | public class ServerFactory implements AbstractFactory{ 7 | 8 | 9 | private String ram; 10 | private String hdd; 11 | 12 | private String cpu; 13 | 14 | 15 | public ServerFactory(String r, String h, String c){ 16 | this.ram = r; 17 | this.hdd = h; 18 | this.cpu = c; 19 | } 20 | 21 | public Computer createComputer(){ 22 | return new Server(ram,hdd,cpu); 23 | } 24 | } 25 | -------------------------------------------------------------------------------- /com/builder/Computer.java: -------------------------------------------------------------------------------- 1 | //Important the computer class 2 | 3 | import com.builder.Computer; 4 | 5 | 6 | public class Computer{ 7 | 8 | //required params. 9 | private String ram; 10 | 11 | private String hdd; 12 | 13 | //optionals params 14 | private boolean isGraphicsCardEnable; 15 | private boolean isBluetoothEnabled; 16 | 17 | public String getRam(){ 18 | return this.ram; 19 | } 20 | 21 | public String getHdd(){ 22 | return this.hdd; 23 | } 24 | 25 | 26 | public boolean isGraphicsCardEnable(){ 27 | return isGraphicsCardEnable; 28 | } 29 | 30 | public boolean isBluetoothEnabled(){ 31 | return isBluetoothEnabled; 32 | } 33 | 34 | // Note the private visibility of the constructor, and the builder variable type. 35 | 36 | private Computer(ComputerBuilder builder){ 37 | 38 | this.ram = builder.ram; 39 | this.hdd = builder.hdd; 40 | 41 | this.isGraphicsCardEnable= builder.isGraphicsCardEnable; 42 | this.isBluetoothEnabled= builder.isBluetoothEnabled; 43 | 44 | } 45 | 46 | 47 | //the builder inner class 48 | 49 | public static class ComputerBuilder{ 50 | //required params. 51 | private String ram; 52 | 53 | private String hdd; 54 | 55 | //optionals params 56 | private boolean isGraphicsCardEnable; 57 | private boolean isBluetoothEnabled; 58 | 59 | 60 | public ComputerBuilder(String hdd, String ram){ 61 | this.ram = ram; 62 | this.hdd = hdd; 63 | } 64 | 65 | public ComputerBuilder setGraphicsCardEnabled(boolean val){ 66 | this.isGraphicsCardEnable = val; 67 | return this; 68 | } 69 | 70 | public ComputerBuilder setBluetoothEnabled(boolean val){ 71 | this.isBluetoothEnabled = val; 72 | 73 | return this; 74 | } 75 | 76 | // The build function. 77 | public Computer build(){ 78 | return new Computer(this); 79 | } 80 | } 81 | 82 | } 83 | -------------------------------------------------------------------------------- /com/factory/Computer.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 18/03/17. 5 | */ 6 | public abstract class Computer { 7 | 8 | 9 | public abstract String getRam(); 10 | public abstract String getCpu(); 11 | public abstract String getHdd(); 12 | 13 | @Override 14 | 15 | public String toString(){ 16 | return "Config :: RAM = " + this.getRam() + " CPU = " + this.getCpu() + " HDD = " + this.getHdd(); 17 | } 18 | 19 | } 20 | -------------------------------------------------------------------------------- /com/factory/FactoryClass.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 18/03/17. 5 | */ 6 | public class FactoryClass { 7 | 8 | public static Computer getComputer(String c, String ram, String hdd, String dd){ 9 | if("PC".equalsIgnoreCase(c)) return new PC(ram,hdd,dd); 10 | else if("Server".equalsIgnoreCase(c)) return new Server(ram,hdd,dd); 11 | 12 | return null; 13 | } 14 | } 15 | -------------------------------------------------------------------------------- /com/factory/PC.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 18/03/17. 5 | */ 6 | public class PC extends Computer { 7 | 8 | private int ram; 9 | private double cpu; 10 | private int hdd; 11 | 12 | 13 | public PC(int ram,int hdd, double cpu){ 14 | this.ram = ram; 15 | this.hdd= hdd; 16 | this.cpu = cpu; 17 | } 18 | public String getRam(){ 19 | return this.ram; 20 | } 21 | public String getCpu(){ 22 | return this.cpu; 23 | } 24 | 25 | public String getHdd(){ 26 | return this.hdd; 27 | } 28 | 29 | 30 | 31 | 32 | } 33 | -------------------------------------------------------------------------------- /com/factory/Server.java: -------------------------------------------------------------------------------- 1 | package com.factory; 2 | 3 | /** 4 | * Created by sdmg15 on 18/03/17. 5 | */ 6 | public class Server extends Computer{ 7 | private int ram; 8 | private double cpu; 9 | private int hdd; 10 | 11 | 12 | public Server(int ram,int hdd,double cpu){ 13 | this.ram = ram; 14 | this.hdd= hdd; 15 | this.cpu = cpu; 16 | } 17 | public String getRam(){ 18 | return this.ram; 19 | } 20 | public String getCpu(){ 21 | return this.cpu; 22 | } 23 | 24 | public String getHdd(){ 25 | return this.hdd; 26 | } 27 | } 28 | -------------------------------------------------------------------------------- /com/patternObsTest/Jobservers.java: -------------------------------------------------------------------------------- 1 | package com.patternObsTest; 2 | 3 | /** 4 | * Created by sdmg15 on 08/03/17. 5 | */ 6 | public interface Jobservers { 7 | 8 | String update(Jsubject subj); 9 | 10 | 11 | } 12 | -------------------------------------------------------------------------------- /com/patternObsTest/Jsubject.java: -------------------------------------------------------------------------------- 1 | package com.patternObsTest; 2 | 3 | /** 4 | * Created by sdmg15 on 08/03/17. 5 | */ 6 | public interface Jsubject { 7 | 8 | void attach(Jobservers[] obs); 9 | 10 | void detach(int obs); 11 | 12 | void noty(); 13 | } 14 | -------------------------------------------------------------------------------- /com/patternObsTest/Observer.java: -------------------------------------------------------------------------------- 1 | package com.patternObsTest; 2 | 3 | /** 4 | * Created by sdmg15 on 08/03/17. 5 | */ 6 | public class Observer implements Jobservers { 7 | 8 | public String update(Jsubject j){ 9 | return "Someone wants to set the value of the attribue : length."; 10 | } 11 | } 12 | -------------------------------------------------------------------------------- /com/patternObsTest/Subject.java: -------------------------------------------------------------------------------- 1 | /** 2 | * Created by sdmg15 on 08/03/17. 3 | */ 4 | package com.patternObsTest; 5 | 6 | public class Subject implements Jsubject { 7 | 8 | public Jobservers[] tab; 9 | 10 | private int length; 11 | 12 | public void setLength(valLEngth val){ 13 | this.length = val.getVal(); 14 | this.noty(); 15 | } 16 | 17 | public int getLength(){ return this.length;} 18 | 19 | @Override 20 | public void attach(Jobservers[] obs){ 21 | 22 | this.tab = obs; 23 | } 24 | 25 | public void detach(int i){ 26 | this.tab[i]= new Observer(); 27 | } 28 | 29 | public void noty(){ 30 | 31 | for(int i = 0; i < this.tab.length; i++){ 32 | System.out.println(this.tab[i].update(this)); 33 | } 34 | 35 | } 36 | } 37 | -------------------------------------------------------------------------------- /com/patternObsTest/valLength.java: -------------------------------------------------------------------------------- 1 | package com.patternObsTest; 2 | 3 | /** 4 | * Created by sdmg15 on 17/03/17. 5 | */ 6 | public enum valLength { 7 | 8 | Val1(24), 9 | Val2(23), 10 | val3(45); 11 | public int val; 12 | 13 | valLength(int val){ 14 | this.val = val; 15 | } 16 | 17 | public int getVal(){ 18 | return this.val; 19 | } 20 | 21 | @Override 22 | public String toString() { 23 | return "I'm an Enum !"; 24 | } 25 | } 26 | -------------------------------------------------------------------------------- /com/singleton/SingletonTradeSafe.java: -------------------------------------------------------------------------------- 1 | package com.singleton; 2 | 3 | /** 4 | * Created by sdmg15 on 18/03/17. 5 | */ 6 | public class SingletonTradeSafe { 7 | 8 | private static SingletonTradeSafe instance; 9 | 10 | private SingletonTradeSafe(){} 11 | 12 | public static synchronized SingletonTradeSafe getInstance(){ 13 | if(instance == null){ 14 | instance = new SingletonTradeSafe(); 15 | } 16 | return instance; 17 | } 18 | } 19 | --------------------------------------------------------------------------------