Overview :
Design patterns, speaking generically, are language-independent, (i.e. Java/C# etc.) strategies for solving common object-oriented design problems. We can summarize this to say that design patterns are provided to ensure Industry standard levels and to solve iteration issues. They also provide the best solution for developers to develop the industry-base application .
Some benefits of design patterns:
- Patterns are generally a close-at-hand way to represent ideas.
- Patterns do have binding to programming paradigm(Oops programming).
- Pattern is appropriate for given problem/architecture/task and therefore you can implement it in any language — let it be Java , C# or Python etc.
- You can easily learn how to avoid silly mistakes in your code application.
- Patterns help you to decide which solution is the best by providing you a set of good solutions to these common problems.
When should we use design patterns?
Design pattern should we use during the analysis and requirement phase of SDLC.
How many design patterns are available in java?
Java offers creational, structural, and behavioral design patterns.These patters again divide into different patterns.
Let me explain this in more detail:
Creational Design Patterns: Creational Design Patterns provide the way to create ab objects (instantiate an object) for a specific situation. That means the developer should think before he creates the object.
Types of Creational Design Patterns:
(a) Singleton Pattern: Only one instance of the class exists in the JVM. The Singleton pattern has different-2 approach like EagerLazyStatic block initialization, Enum, Reflection concept and Serialization Singleton etc. For example:
package com.trigentDesignpattern.singleton; public enum EnumTrigentSingleton { INSTANCE; public static void doTrigentContent(){ //do something } }
(b) Prototype Pattern: Many instance of the class exists in the JVM and we can say the prototype results in a cloned object which is different from the original object. It is very useful in dynamic binding(classes to instantiate are specified at run time). Its improve the performance of the application because its used cloning concept. For example:
public interface TrigentPrototype {Public abstract Object Clone();} public class TrigentClone implements TrigentPrototype { public Object Clone(){ Return super.Clone();}} public class Clone{ Public static void main(String args[]){ TrigentClone obj1 = new TrigentClone (); TrigentClone obj2 = (TrigentClone)obj1.Clone(); } }
(c) Abstract Factory Pattern: Abstract factory is normally used for things like dependency injection and strategy concept, when we want to be able to create a whole family of objects that need to be of “the same kind”, and have some common base classes. For example:
interface TrigentFactory { Plant makeTrigentPlant(); Picker makeTrigentPicker(); } public class TrigentAppleFactory implements TrigentFactory { Plant makeTrigentPlant() { return new Apple(); } } public class TrigentOrangeFactory implements TrigentFactory { Plant makeTrigentPlant() { return new Orange(); } }
(d) Factory Pattern:
Creates objects based on input criteria and without showing the instantiate logic to the client. The Factory Pattern is used when we have some generic processing in a class. For example:
interface TrigentFactory { public Apple makeApple() { // Code for creating an Apple here. } } abstract class TrigentFruitPicker { protected abstract Fruit makeFruit(); public void pickFruit() { private final Fruit f = makeFruit(); // The fruit we will work on.. }} class ApplePicker extends TrigentFruitPicker { @Override protected Fruit makeFruit() { return new makeApple(); } }
(e) Builder Pattern: Builder Pattern used for recurringiterate generic processing. Make and return one object into various ways (Parse a complex representation, create one of several targets). Its reduce the number of parameters in the MethodConstructor via the custom typesparameters object.