You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+13-15Lines changed: 13 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -28,8 +28,9 @@ __Java Design Patterns__ are divived into tree parts : *Creational*, *Structural
28
28
* Prototype Pattern
29
29
30
30
### Pattern Singleton
31
+
32
+
Pattern Singleton: > One Class, one Instance.
31
33
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.
34
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__.
35
36
@@ -64,14 +65,14 @@ We'll implement the thread safe one here. Classes are in the package `com.single
64
65
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.
66
67
67
-
* Super Class
68
-
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.
68
+
* 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.
69
69
see `com.factory`.
70
-
* sub-classes
71
-
Let’s say we have two sub-classes PC and Server with implementation in `com.factory`
72
-
Here's what we have in image :
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
73
[](http://zupimages.net/up/17/13/7w9z.png)
74
-
Now let's write the test class.
74
+
75
+
Now let's write the test class.
75
76
76
77
```java
77
78
importcom.factory.FactoryClass ; //The factory class
@@ -99,18 +100,16 @@ This pattern provides some advantages such as :
99
100
* It provides abstraction between implementation and client classes through inheritance.
100
101
101
102
As examples of its implementation in JDK we have :
102
-
* java.util.Calendar, ResourceBundle() and NumberFormat getInstance();
103
-
* valueOf() method in wrapper classes like Boolean , Integer etc.
103
+
104
+
**java.util.Calendar*, *ResourceBundle()* and *NumberFormat getInstance()*;
105
+
**valueOf()* method in wrapper classes like Boolean , Integer etc.
104
106
105
107
### Abstract Factory
106
108
107
109
This is one of the Creational Pattern and almost similar to Factory Pattern except the fact that it's most like
108
-
Factory of factories.
109
-
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.
110
-
Like our factory pattern post, we will use the same super class and sub-classes.
111
-
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.
112
111
Codes are available in `com.abstractFactory`.
113
-
Here's the implementation of the test class:
112
+
Here's the implementation of the test class:
114
113
115
114
```java
116
115
@@ -125,7 +124,6 @@ Factory of factories.
125
124
}
126
125
```
127
126
128
-
129
127
### Pattern Builder
130
128
131
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.
0 commit comments