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-13Lines changed: 13 additions & 13 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -4,20 +4,20 @@ Design Patterns are very popular among software developers. A design pattern is
4
4
common software problem.
5
5
6
6
Some of benefits of using design patterns are :
7
-
* Design patterns are already defined and provides industry standard approach to solve reccuring problem,
8
-
so it saves time if we senibly use the design pattern .
9
-
* Using design pattern promotes reusability that leads to more robust and hightly maintenable code.
10
-
* 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.
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
11
12
12
13
13
__What is a Design Pattern ?__
14
14
> A software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design --- Wikipedia
15
15
16
-
__Java Design Patterns__ are divived into tree parts : *Creational*, *Structural* and *Behavioral*.
16
+
__Java Design Patterns__ are divided into tree parts : *Creational*, *Structural* and *Behavioral*.
17
17
18
18
## CREATIONAL DESIGN PATTERNS
19
19
20
-
Creational design pattens provide solution to instatiate an object in the best possible way for specific situations.
20
+
Creational design pattens provide solution to instantiate an object in the best possible way for specific situations.
21
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
22
There are five creational design patterns that we will discuss on :
23
23
@@ -28,11 +28,11 @@ __Java Design Patterns__ are divived into tree parts : *Creational*, *Structural
28
28
* Prototype Pattern
29
29
30
30
### Pattern Singleton
31
-
31
+
32
32
Pattern Singleton: > One Class, one Instance.
33
33
34
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__.
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
36
37
37
To implement Singleton pattern, there are really many approaches but all of them have following common concepts:
38
38
@@ -63,9 +63,9 @@ We'll implement the thread safe one here. Classes are in the package `com.single
63
63
```
64
64
### Pattern Factory
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
+
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
67
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.
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
69
see `com.factory`.
70
70
* sub-classes: Let’s say we have two sub-classes PC and Server with implementation in `com.factory`
71
71
@@ -95,12 +95,12 @@ Now let's write the test class.
95
95
```
96
96
This pattern provides some advantages such as :
97
97
98
-
* It provides approach to code for the interface rathan than the implementation.
98
+
* It provides approach to code for the interface rather than the implementation.
99
99
* It removes the instantiation of the actual implementation classes from client code, making it more robust.
100
100
* It provides abstraction between implementation and client classes through inheritance.
101
101
102
102
As examples of its implementation in JDK we have :
103
-
103
+
104
104
**java.util.Calendar*, *ResourceBundle()* and *NumberFormat getInstance()*;
105
105
**valueOf()* method in wrapper classes like Boolean , Integer etc.
106
106
@@ -150,7 +150,7 @@ There are really various implementations of this pattern in JDK : java.lang.Stri
150
150
151
151
### Pattern Prototype
152
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.
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.
0 commit comments