diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md new file mode 100644 index 0000000..fdc12b1 --- /dev/null +++ b/CONTRIBUTING.md @@ -0,0 +1 @@ +# How to contribute ? diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..2da335b --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2017 Sonkeng Maldini + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md index 0b4d6dd..31adb81 100644 --- a/README.md +++ b/README.md @@ -4,20 +4,20 @@ Design Patterns are very popular among software developers. A design pattern is common software problem. Some of benefits of using design patterns are : - * Design patterns are already defined and provides industry standard approach to solve reccuring problem, - so it saves time if we senibly use the design pattern . - * Using design pattern promotes reusability that leads to more robust and hightly maintenable code. - * Since design patterns are already defined, it makes out code easy to understand and debug. It lead to faster developement and new members of team understand it easily. + * Design patterns are already defined and provides industry standard approach to solve recurring problem, + so it saves time if we use the design pattern . + * Using design pattern promotes re-usability that leads to more robust and highly maintainable code. + * 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. __What is a Design Pattern ?__ > A software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design --- Wikipedia -__Java Design Patterns__ are divived into tree parts : *Creational*, *Structural* and *Behavioral*. +__Java Design Patterns__ are divided into tree parts : __Creational__, __Structural__ and __Behavioral__. ## CREATIONAL DESIGN PATTERNS - Creational design pattens provide solution to instatiate an object in the best possible way for specific situations. + Creational design pattens provide solution to instantiate an object in the best possible way for specific situations. 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. There are five creational design patterns that we will discuss on : @@ -28,11 +28,11 @@ __Java Design Patterns__ are divived into tree parts : *Creational*, *Structural * Prototype Pattern ### Pattern Singleton - + Pattern Singleton: > One Class, one Instance. Singleton is one of the Gangs of Four Design patterns and comes in the Creational Design Pattern category. -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__. +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__. To implement Singleton pattern, there are really many approaches but all of them have following common concepts: @@ -63,9 +63,9 @@ We'll implement the thread safe one here. Classes are in the package `com.single ``` ### Pattern Factory -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. +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. -* Super Class : Super class in factory pattern can be an interface, abstract class or a normaljava class. For our example, we have super class as abstract class with overridden toString() method for testing purpose. +* 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. see `com.factory`. * sub-classes: Let’s say we have two sub-classes PC and Server with implementation in `com.factory` @@ -95,12 +95,12 @@ Now let's write the test class. ``` This pattern provides some advantages such as : - * It provides approach to code for the interface rathan than the implementation. + * It provides approach to code for the interface rather than the implementation. * It removes the instantiation of the actual implementation classes from client code, making it more robust. * It provides abstraction between implementation and client classes through inheritance. As examples of its implementation in JDK we have : - + * *java.util.Calendar*, *ResourceBundle()* and *NumberFormat getInstance()*; * *valueOf()* method in wrapper classes like Boolean , Integer etc. @@ -150,7 +150,7 @@ There are really various implementations of this pattern in JDK : java.lang.Stri ### Pattern Prototype -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. +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. ```java @@ -208,9 +208,123 @@ public class PrototypePatternTest { List list1 = usersNew1.getEmpList(); list1.remove("Pankaj"); - System.out.println("users List: "+users.getEmpList()); + System.out.println("users List: "+ users.getEmpList()); System.out.println("users New List: "+list); System.out.println("users New1 List: "+list1); } } ``` + +## STRUCTURAL DESIGN PATTERNS + + Structural Patterns provide different ways to create a class structure, for example using inheritance and composition + to create a large object from small objects. + +### Adapter Pattern + +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. +First of all we'll have two classes : Volt - to measure volts) and Socket : + +```java + +public class Volt { + private int volts; + public Volt(int v){ + this.volts=v; + } + public int getVolts() { + return volts; + } + public void setVolts(int volts) { + this.volts = volts; + } +} + + +public class Socket { + public Volt getVolt(){ + return new Volt(120); +} + +} +``` +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. + +```java +public interface SocketAdapter { +public Volt get120Volt(); +public Volt get12Volt(); +public Volt get3Volt(); +} + +``` + +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 ! + + +```java +public class SocketClassAdapterImpl extends Socket implements + SocketAdapter{ + @Override + public Volt get120Volt() { + return getVolt(); + } + @Override + public Volt get12Volt() { + Volt v= getVolt(); + return convertVolt(v,10); + } + @Override + public Volt get3Volt() { + Volt v= getVolt(); + return convertVolt(v,40); + } + private Volt convertVolt(Volt v, int i) { + return new Volt(v.getVolts()/i); + } +} +``` +Now let's see how all this work ! Here's a test Main function to illustrate. + + +```java + +public class AdapterPatternTest { + + public static void main(String[] args) { + testClassAdapter(); + + testAdapter(); + + //The results goes in your console :) +} + +private static void testAdapter() { + SocketAdapter sockAdapter = new SocketObjectAdapterImpl(); + + Volt v3 = getVolt(sockAdapter,3); + + Volt v12 = getVolt(sockAdapter,12); + + Volt v120 = getVolt(sockAdapter,120); + + System.out.println("v3 volts using Object Adapter="+v3.getVolts()); + System.out.println("v12 volts using Object Adapter="+v12.getVolts()); + System.out.println("v120 volts using Object Adapter="+v120.getVolts()); +} + +private static Volt getVolt(SocketAdapter sockAdapter, int i) { + switch (i){ + case 3: return sockAdapter.get3Volt(); + case 12: return sockAdapter.get12Volt(); + case 120: return sockAdapter.get120Volt(); + default: return sockAdapter.get120Volt(); + } +} +} +``` +This pattern has many usage in the JDK : + * __java.util.Arrays#asList() java.io.InputStreamReader(InputStream)__ (returns a Reader), + * __java.io.OutputStreamWriter(OutputStream)__ (returns a Writer). + +### Composite Pattern diff --git a/com/abstractFactory/ServerFactory.java b/com/abstractFactory/ServerFactory.java index ac2c071..461c34b 100644 --- a/com/abstractFactory/ServerFactory.java +++ b/com/abstractFactory/ServerFactory.java @@ -3,7 +3,7 @@ /** * Created by sdmg15 on 21/03/17. */ -public class ServerFactory { +public class ServerFactory implements AbstractFactory{ private String ram; diff --git a/com/builder/Computer.java b/com/builder/Computer.java index 0b7955b..b58aea4 100644 --- a/com/builder/Computer.java +++ b/com/builder/Computer.java @@ -35,11 +35,12 @@ public boolean isBluetoothEnabled(){ private Computer(ComputerBuilder builder){ - this.ram = com.builder; - this.hdd = com.builder; + this.ram = builder.ram; + this.hdd = builder.hdd; + + this.isGraphicsCardEnable= builder.isGraphicsCardEnable; + this.isBluetoothEnabled= builder.isBluetoothEnabled; - this.isGraphicsCardEnable = com.isGraphicsCardEnable; - this.isBluetoothEnabled = com.isBluetoothEnabled; }