I want to learn Design patterns with real time example. So can any one suggest where I can start.
Note: Adding brief definition with real life and Java API examples.
How do you want to create objects?
Prototype : A fully initialized instance to be copied or cloned
Example : initial status of chess game
java.lang.Object#clone()
Builder - Separates the construction of a complex object from its representation so that the same construction process can create different representations
Example : Kitchen is a Factory, Chef is a Builder where waiter tell to chef "pizza with cheese, onion". Here chef exposed attributes but hidden implementation.
Singleton - A class of which only a single instance can exist
Example : President of a country
Factory Method - Creates an instance of several derived classes.
Example : In an organisation HR works as factory method. Here development team request type of resource need to HR. Based on request type, HR provide resource to Development team.
Abstract Factory - Creates an instance of several families of classes
Example : HP, Samsung and Dell laptops are uses Intel and AMD processor.
Factory Method vs Abstract Factory
This design patterns is all about Class and Object composition i.e. How do you want structure the software component.
Proxy - An object representing another object
Example : check book leaf, credit card, debit card are proxy for Money and a customer representative to order a product.
Composite - Gives an unified interface to a leaf and composite.
Example : File System in Operating Systems, Directories are composite and files are leaves. System call Open
is single interface for both composite and leaf.
Decorator - Gives additional feature to leaf, while giving unified interface.
Example : 1) Adding discounts on an order 2) gun is a deadly weapon on it's own. But you can apply certain "decorations" to make it more accurate, silent and devastating.
Facade - Simplifies multiple complex components with single interface
Example : Control Panel
Adapter - Provides different interface for an interface.
Example : Power Adapters
Flyweight - A fine-grained instance used for efficient sharing
Example : Dial tone
This design patterns is all about Class's objects communication i.e How do you want a behavior in software?
Chain of Responsibility - A way of passing a request between a chain of objects
Example : Loan or Leave approval process
Iterator - Sequentially access the elements of a collection
Example : Next/Previous buttons on TV
State - Alter an object's behavior when its state changes
Example : A fan wall control
Observer - A way of notifying change to a number of classes
Example : Bidding or auction
Visitor - Defines a new operation to a class without change
Example : Taxi
Template - Defer the exact steps of an algorithm to a subclass
Example : A blue print
Command - Encapsulate a command request as an object
Example : The "Guest Check" at a diner is an example of a Command pattern. The waiter or waitress takes an order or command from a customer and encapsulates that order by writing it on the check. The order is then queued for a short order cook. Note that the pad of "checks" used by each waiter is not dependent on the menu, and therefore they can support commands to cook many different items.
Memento - Capture and restore an object's internal state
Example : save the state in a game & Undo/Redo operation in Windows
Mediator - Defines simplified communication between classes
Example : Air Traffic Controller(ATC)
Strategy - A Strategy defines a set of algorithms that can be used interchangeably.
Example : Modes of transportation
Courtesy.
Design Patterns