|
1 | 1 | # Java Design Patterns
|
2 | 2 |
|
3 |
| -Here's is an implementation of some Design Patterns from scratch :p |
| 3 | +Design Patterns are very popular among software developers. A design pattern is a well-described solution to |
| 4 | +common software problem. |
4 | 5 |
|
5 |
| -__What is a Design Pattern ?__ |
6 |
| - > A software design pattern is a general reusable solution to a commonly occurring problem within a given context in software design --- Wikipedia |
7 |
| -
|
8 |
| -In Java, Design Patterns are divived into tree parts : *Creational*, *Structural* and *Behavioral*. |
9 |
| - |
10 |
| -# Pattern Observer |
11 |
| - Code source files are available in the package `com.patternObsTest`. |
12 |
| - The observer pattern is a software design pattern in which an object, called the subject, maintains a list of its dependents, called observers, and notifies them automatically of any state changes, usually by calling one of their methods. |
| 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. |
13 | 11 |
|
14 |
| - ```java |
15 | 12 |
|
16 |
| - // importing classes in the package |
17 |
| - import com.patternObsTest.*; |
| 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 |
18 | 15 |
|
19 |
| - public class test{ |
20 |
| - public static void main(String[] args[]){ |
| 16 | +__Java Design Patterns__ are divived into tree parts : *Creational*, *Structural* and *Behavioral*. |
21 | 17 |
|
22 |
| - Subject s = new Subject(); |
23 |
| - Observer o = new Observer(); |
24 |
| - Observer o2 = new Observer(); |
25 |
| - Observer[] arr = {o,o2}; |
| 18 | +# CREATIONAL DESIGN PATTERNS |
26 | 19 |
|
27 |
| - s.attach(arr); |
| 20 | + Creational design pattens provide solution to instatiate 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 : |
28 | 23 |
|
29 |
| - /*now we set the value of the attribute length contains in the Subject. |
30 |
| - NOTE: the param is an enum (valLEngth) |
31 |
| - */ |
| 24 | + 1 .Singleton Pattern |
| 25 | + 2. Factory Pattern |
| 26 | + 3. Abstract Factory Pattern |
| 27 | + 4. Builder Pattern |
| 28 | + 4. Prototype Pattern |
32 | 29 |
|
33 |
| - s.setLength(valLength.Val2); |
34 |
| - //Now look at your console to see the result :p |
| 30 | +### Pattern Singleton |
| 31 | +Pattern Singleton: > One Class, one Instance. |
| 32 | + Singleton is one of the Gangs of Four Design patterns and comes in the Creational Design Pattern category. |
| 33 | +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 | 34 |
|
36 |
| - } |
| 35 | +To implement Singleton pattern, there are really many approaches but all of them have following common concepts: |
| 36 | +* A private constructor to avoid instantiation of the class, |
| 37 | +* A private static variable from the same class that's the only instance of the class. |
| 38 | +* public static method that returns the instance of the class, this is the global access point for the outer world to |
| 39 | + get the instance of the class. |
37 | 40 |
|
38 |
| - } |
39 |
| - ``` |
| 41 | +We'll implement the thread safe one here. Classes are in the package `com.singleton`; |
40 | 42 |
|
41 |
| -# Pattern Singleton |
42 |
| -Pattern Singleton: One Class, one Instance. |
43 |
| - Singleton is one of the Gangs of Four Design patterns and comes in the Creational Design Pattern category. |
44 |
| -There are many implementations of this pattern, but we will implement the Thread Safe one. |
45 |
| -Classes are in the package `com.Singleton`; |
46 | 43 | ```java
|
47 | 44 | import com.singleton.SingletonThreadSafe;
|
48 | 45 |
|
|
0 commit comments