Design Patterns
Design Patterns
Design Pattern
• A design pattern is just a convenient way of reusing object-oriented
(OO) code between projects and programmers. A common pattern in
early literature on programming frameworks is Model-View-
Controller (MVC) for Smalltalk [Krasner and Pope, 1988], which
divides the user interface problem into three parts.
Design Pattern Definitions
1. Design patterns are recurring solutions to design problems you see over and
over.
2. Design patterns constitute a set of rules describing how to accomplish certain
tasks in the realm of software development.
3. Design patterns focus more on reuse of recurring architectural design themes,
while frameworks focus on detailed design... and implementation
4. A pattern addresses a recurring design problem that arises in specific design
situations and presents a solution to it.
5. Patterns identify and specify abstractions the at are above the level of single
classes and instances, or of components.
6. Design patterns are not just about the design of objects; they are
also about interaction between objects. One possible view of
some of these patterns is to consider them as communication
patterns
Design Pattern Continue
• The design patterns divides into three types:
• Creational patterns: create objects for you, rather than your having
to instantiate objects directly. Your program gains more flexibility in
deciding which objects need to be created for a given case.
• Structural patterns: help you compose groups of objects into larger
structures, such as complex user interfaces and accounting data.
• Behavioral patterns: help you to define the communication between
objects in your system and how the flow is controlled in a complex
program.
Division Of Design pattern
• Controllers are the activities themselves which contain all the business
logic done in the application.
• Models are our entities which describe the components of our apps.
• The MVP pattern allows separate the presentation layer from the logic.
The View
The view, usually implemented by an Activity (it may be a Fragment, a View… depending on how the app
is structured), will contain a reference to the presenter.
The only thing that the view will do is calling a method from the presenter every time there is an
interface action (a button click for example).
The model
In an application with a good layered architecture, this model would only be the gateway to the domain
layer or business logic.
Difference B/w MVC & MVP
MVC MVP
UI Presentation Pattern focus on separation of view with Based on MVC (UI Presentation Pattern)
Model
Separation of responsibility between three components: Separation of responsibility between four components:
1.View - responsible for rendering UI 1.View - responsible for rendering UI
elements elements
2.Controller - responsible for responding to 2.View Interface - responsible for loose
view actions coupling between view and model
3.Presenter - responsible for view and model
3.Model - responsible for business behavior
interaction
and state management 4.Model - responsible for business behavior
and state management
Difference B/w MVC & MVP Continue
MVC MVP
Fairly loose coupling Comparatively high degree of loose coupling
Controller is behavior based and multiple views Usually one view has one presenter (1-1 mapping).
can share single controller Multiple presenters would be associated with a
complex view
Identifies which view to update Presenter will update its associated view
The Following Android Classes uses Design
Patterns
• The adapter pattern is adapting between classes and objects. Like any
adapter in the real world it is used to be an interface, a bridge
between two objects.
• The main use of this pattern is when a class that you need to use
doesn't meet the requirements of an interface.
Composite Design Pattern
• Composite pattern is used where we need to treat a group of objects
in similar way as a single object. Composite pattern composes objects
in term of a tree structure to represent part as well as whole
hierarchy
• This pattern creates a class contains group of its own objects. This
class provides ways to modify its group of same objects
Behavioral patterns
Observer Design Pattern
Observer pattern is one of the behavioral design pattern
Observer design pattern is useful when you are interested in the state
of an object and want to get notified whenever there is any change
In observer pattern, the object that watch on the state of another
object are called Observer and the object that is being watched is
called Subject