design patterns
DESCRIPTION
In software engineering, a design pattern is a general reusable solution to a commonly occurring problem within a given context in software design. A design pattern is not a finished design that can be transformed directly into source or machine code. It is a description or template for how to solve a problem that can be used in many different situations. Patterns are formalized best practices that the programmer can use to solve common problems when designing an application or system. Object-oriented design patterns typically show relationships and interactions between classes or objects, without specifying the final application classes or objects that are involved. Patterns that imply object-orientation or more generally mutable state, are not as applicable in functional programming languages. Design patterns reside in the domain of modules and interconnections. At a higher level there are architectural patterns that are larger in scope, usually describing an overall pattern followed by an entire system.[1] There are many types of design patterns, for instance Algorithm strategy patterns addressing concerns related to high-level strategies describing how to exploit application characteristics on a computing platform. Computational design patterns addressing concerns related to key computation identification. Execution patterns that address concerns related to supporting application execution, including strategies in executing streams of tasks and building blocks to support task synchronization. Implementation strategy patterns addressing concerns related to implementing source code to support program organization, and the common data structures specific to parallel programming. Structural design patterns addressing concerns related to high-level structures of applications being developed.TRANSCRIPT
Design PatternsPresented by Akhilesh Gupta, SCJP 1.6
DefinitionsA pattern is a recurring solution to
a standard problem, in a context.
“ A pattern describes a problem which occurs over and
over again in our environment, and then describes the
core of the solution to that problem, in such a way that
you can use this solution a million times over, without
ever doing it the same way twice.”
Patterns in engineering How do other engineers find and use patterns?
Mature engineering disciplines have handbooks describing successful solutions to known problems
Automobile designers don't design cars from scratch using the laws of physics
Instead, they reuse standard designs with successful track records, learning from experience
Should software engineers make use of patterns? Why?
Developing software from scratch is also expensive Patterns support reuse of software architecture and
design
Elements of Design Patterns Design patterns have 4 essential
elements: Pattern name: increases vocabulary of
designers Problem: intent, context, when to apply Solution: UML-like structure, abstract code Consequences: results and tradeoffs
Three Types of Patterns Creational patterns:
Deal with initializing and configuring classes and objects
Structural patterns: Deal with decoupling interface and implementation of
classes and objects Composition of classes or objects
Behavioral patterns: Deal with dynamic interactions among societies of
classes and objects How they distribute responsibility
Observer : object behavioral
Intentdefine a one-to-many dependency between objects so that when one object changes state, all dependents are notified & updated
Applicability◦ an abstraction has two aspects, one
dependent on the other◦ a change to one object requires changing
untold others
Singleton : object creational
Intent
ensure a class only ever has one instance & provide a
global point of access
Applicability
◦ when there must be exactly one instance of a class,
& it must be accessible from a well-known access
point
◦ when the sole instance should be extensible by
subclassing, & clients should be able to use an
extended instance without modifying their code
Factory Pattern :class creationalIntent
• The client needs a product, but instead of creating it directly using the
new operator, it asks the factory object for a new product, providing the
information about the type of object it needs.
• The factory instantiates a new concrete product and then returns to the
client the newly created product(casted to abstract product class).
• The client uses the products as abstract products without being aware
about their concrete implementation.
Applicability
when a class cannot anticipate the objects it must create
or a class wants its subclasses to specify the objects it
creates
Strategy : object behavioral
Intent
define a family of algorithms, encapsulate each one, &
make them interchangeable to let clients & algorithms
vary independently
Applicability
◦ when an object should be configurable with one of
many algorithms,
◦ and all algorithms can be encapsulated,
◦ and one interface covers all encapsulations
Adapter : Structural Pattern Intent
"Convert the interface of class into another interface clients
expect. Adapter lets class work together that couldn't
otherwise because of incompatible interfaces".
A very simple example is using phone charger. Suppose your mobile phone needs 9 Volts of supply to get charged but main supply is 220 V which is not what you require but it can be a source and you can have something that can get 9 V out of this 220 V and supply to your phone for charging.Now phone charger is acting as an adapter for you. So Basically it is making a relationship between two unrelated interfaces. This is how Adpater pattern works
Target defines domains-specific interface client uses.
Client collaborates with objects conforming to target interface.
Adaptee defines existing interface that needs adapting
Adapter adapts the interface of adaptee to target interface.