From 94c3d33542a3f979cafed9384babb47636815f82 Mon Sep 17 00:00:00 2001 From: sdmg15 Date: Mon, 1 May 2017 23:08:44 +0100 Subject: [PATCH 01/11] typo fixed --- README.md | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/README.md b/README.md index 0b4d6dd..bbdb748 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 From f7e4609a8746b0cf5b4b00d8e7ab53454463b262 Mon Sep 17 00:00:00 2001 From: sdmg15 Date: Mon, 1 May 2017 23:13:36 +0100 Subject: [PATCH 02/11] Strucutural patterns --- README.md | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/README.md b/README.md index bbdb748..a3b0df6 100644 --- a/README.md +++ b/README.md @@ -214,3 +214,8 @@ public class PrototypePatternTest { } } ``` + +## 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. From e3901d54909a55044acdb84ff264bd480203ee7a Mon Sep 17 00:00:00 2001 From: sdmg15 Date: Wed, 3 May 2017 02:43:41 +0100 Subject: [PATCH 03/11] Implement Pattern Adapter --- README.md | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 111 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index a3b0df6..175d7bb 100644 --- a/README.md +++ b/README.md @@ -208,14 +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 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(); + } + + +} + +} +``` + +### Composite Pattern From b281eb5911a0d2362e9fb7016837e6e832ddbd9c Mon Sep 17 00:00:00 2001 From: sdmg15 Date: Wed, 3 May 2017 02:45:22 +0100 Subject: [PATCH 04/11] Usage of Adapter in jDK --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 175d7bb..bfdf216 100644 --- a/README.md +++ b/README.md @@ -326,5 +326,6 @@ private static Volt getVolt(SocketAdapter sockAdapter, int i) { } ``` +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 +### Composite Pattern From 761825b866fc1bc116427d512f9c9d0a39dc1a11 Mon Sep 17 00:00:00 2001 From: sdmg15 Date: Wed, 3 May 2017 02:51:25 +0100 Subject: [PATCH 05/11] update readme --- README.md | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index bfdf216..30dcdd8 100644 --- a/README.md +++ b/README.md @@ -259,7 +259,7 @@ 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 ! +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 @@ -320,12 +320,11 @@ private static Volt getVolt(SocketAdapter sockAdapter, int i) { 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). +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 From 835321f8e5f4ba694a65b73a05b30d5ebee4f6a0 Mon Sep 17 00:00:00 2001 From: Sonkeng Maldini Date: Wed, 10 May 2017 23:25:07 +0100 Subject: [PATCH 06/11] Create CONTRIBUTING.md --- CONTRIBUTING.md | 1 + 1 file changed, 1 insertion(+) create mode 100644 CONTRIBUTING.md 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 ? From 78e72470e40711ec279cc6bb19454247e37aa572 Mon Sep 17 00:00:00 2001 From: Sonkeng Maldini Date: Thu, 20 Jul 2017 10:26:06 +0100 Subject: [PATCH 07/11] Fixing error --- com/abstractFactory/ServerFactory.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; From 1dbd534876fa422009a706c8c4d87f7752895171 Mon Sep 17 00:00:00 2001 From: Sonkeng Maldini Date: Mon, 7 Aug 2017 21:06:20 +0100 Subject: [PATCH 08/11] Set theme jekyll-theme-tactile From 1ed8a9f9a35348e03afd766f565c03881e420c81 Mon Sep 17 00:00:00 2001 From: Sonkeng Maldini Date: Wed, 13 Sep 2017 13:04:59 +0100 Subject: [PATCH 09/11] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 30dcdd8..31adb81 100644 --- a/README.md +++ b/README.md @@ -13,7 +13,7 @@ Some of benefits of using design patterns are : __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 divided into tree parts : *Creational*, *Structural* and *Behavioral*. +__Java Design Patterns__ are divided into tree parts : __Creational__, __Structural__ and __Behavioral__. ## CREATIONAL DESIGN PATTERNS From 4e6e4277c6485f8c4193b8cf7fbaade29ecb3cee Mon Sep 17 00:00:00 2001 From: Sonkeng Maldini Date: Wed, 13 Sep 2017 17:15:15 +0100 Subject: [PATCH 10/11] Following OS guide --- LICENSE | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 LICENSE 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. From 2f9d6dbdc198e333c4b03902104980d46e2adfce Mon Sep 17 00:00:00 2001 From: swatantraj Date: Thu, 6 Sep 2018 20:20:10 +0530 Subject: [PATCH 11/11] Error while setting builder variables --- com/builder/Computer.java | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) 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; }