SlideShare a Scribd company logo
Object Oriented Programming with Java
Unit No. 6: Design Patterns
Sanjivani Rural Education Society’s
Sanjivani College of Engineering, Kopargaon-423603
(An Autonomous Institute Affiliated to Savitribai Phule Pune University, Pune)
NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified
Department of Information Technology
(UG Course: NBA Accredited)
Dr. Y.S.Deshmukh
Assistant Professor
Object Oriented Programming with Java
What is Design Pattern?:
Christopher Alexander says, "Each 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" [AIS+77, page x}. Even though Alexander was talking
about patterns in buildings and towns, what he says is true about object-oriented
design patterns. Our solutions are expressed in terms of objects and interfaces instead
of walls and doors, but at the core of both kinds of patterns is a solution to a problem
in a context.
Object Oriented Programming with Java
In general, a pattern has four essential elements:
1. The pattern name is a handle we can use to describe a design problem, its solution,
and consequences in a word or two. Naming a pattern immediately increases our
design vocabulary. It lets us design at a higher level of abstraction. Having a
vocabulary for patterns lets us talk about them with our colleagues, in our
documentation, and even to ourselves. It makes it easier to think about designs
and to communicate them and their trade-offs to others. Finding good names has
been one of the hardest parts of developing our catalog.
2. The problem describes when to apply the pattern. It explains the problem and its
context. It might describe specific design problems such as how to represent
algorithms as objects. It might describe class or object structures that are
symptomatic of an inflexible design. Sometimes the problem will include a list of
conditions that must be met before it makes sense to apply the pattern.
Object Oriented Programming with Java
In general, a pattern has four essential elements:
3. The solution describes the elements that make up the design, their relationships,
responsibilities, and collaborations. The solution doesn't describe a particular concrete
design or implementation, because a pattern is like a template that can be applied in
many different situations. Instead, the pattern provides an abstract description of a
design problem and how a general arrangement of elements (classes and objects in our
case) solves it.
4. The consequences are the results and trade-offs of applying the pattern. Though
consequences are often unvoiced when we describe design decisions, they are critical
for evaluating design alternatives and for understanding the costs and benefits of
applying the pattern. The consequences for software often concern space and time
trade-offs. They may address language and implementation issues as well. Since reuse
is often a factor in object-oriented design, the consequences of a pattern include its
impact on a system's flexibility, extensibility, or portability. Listing these consequences
explicitly helps you understand and evaluate them.
Object Oriented Programming with Java
How to Select a Design Pattern?:
With more than 20 design patterns in the catalog to choose from, it might be hard to
find the one that addresses a particular design problem, especially if the catalog is new
and unfamiliar to you. Here are several different approaches to finding the design
pattern that's right for your problem:
• Consider how design patterns solve design problems. Section 1.6 discusses how
design patterns help you find appropriate objects, determine object granularity,
specify object interfaces, and several other ways in which design patterns solve
design problems. Referring to these discussions can help guide your search for the
right pattern.
• Scan Intent sections. Lists the Intent sections from all the patterns in the catalog.
Read through each pattern's intent to find one or more that sound relevant to your
problem. You can use the classification scheme to narrow your search.
Object Oriented Programming with Java
How to Select a Design Pattern?:
• Study how patterns interrelate. Figure shows relationships between design patterns
graphically. Studying these relationships can help direct you to the right pattern or
group of patterns.
• Study patterns of like purpose. The catalog has three chapters, one for creational
patterns, another for structural patterns, and a third for behavioral patterns. Each
chapter starts off with introductory comments on the patterns and concludes with a
section that compares and contrasts them. These sections give you insight into the
similarities and differences between patterns of like purpose.
• Examine a cause of redesign. Look at the causes of redesign. Then look at the
patterns that help you avoid the causes of redesign.
• Consider what should be variable in your design. This approach is the opposite of
focusing on the causes of redesign. Instead of considering what might force a
change to a design, consider what you want to be able to change without redesign.
The focus here is on encapsulating the concept that varies, a theme of many design
patterns.
Object Oriented Programming with Java
How to Use a Design Pattern?:
Once you've picked a design pattern, how do you use it? Here's a step-by-step
approach to applying a design pattern effectively:
1. Read the pattern once through for an overview. Pay particular attention to the
Applicability and Consequences sections to ensure the pattern is right for your
problem.
2. Go back and study the Structure, Participants, and Collaborations sections. Make
sure you understand the classes and objects in the pattern and how they relate to
one another.
3. Look at the Sample Code section to see a concrete example of the pattern in code.
Studying the code helps you learn how to implement the pattern.
4. Choose names for pattern participants that are meaningful in the application
context. The names for participants in design patterns are usually too abstract to
appear directly in an application. Nevertheless, it's useful to incorporate the
participant name into the name that appears in the application. That helps make
the pattern more explicit in the implementation. For example, if you use the
Strategy pattern for a text compositing algorithm, then you might have classes
SimpleLayoutStrategy or TeXLayoutStrategy.
Object Oriented Programming with Java
How to Use a Design Pattern?:
Once you've picked a design pattern, how do you use it? Here's a step-by-step
approach to applying a design pattern effectively:
5. Define the classes. Declare their interfaces, establish their inheritance relationships,
and define the instance variables that represent data and object references. Identify
existing classes in your application that the pattern will affect, and modify them
accordingly.
6. Define application-specific names for operations in the pattern. Here again, the
names generally depend on the application. Use the responsibilities and collaborations
associated with each operation as a guide. Also, be consistent in your naming
conventions. For example, you might use the "Create-" prefix consistently to denote a
factory method
7. Implement the operations to carry out the responsibilities and collaborations in the
pattern. The Implementation section offers hints to guide you in the implementation.
The examples in the Sample Code section can help as well.
Object Oriented Programming with Java
Purpose Design Pattern Aspect(s) That Can Vary
Creational Abstract Factory families of product objects
Builder how a composite object gets created
Factory Method subclass of object that is instantiated
Prototype class of object that is instantiated
Singleton the sole instance of a class
Structural Adapter
Bridge
Composite
Decorator
Facade
Flyweight
Proxy
interface to an object
implementation of an object
structure and composition of an object
responsibilities of an object without sub classing
interface to a subsystem
storage costs of objects
how an object is accessed; its location
Object Oriented Programming with Java
Purpose Design Pattern Aspect(s) That Can Vary
Behavioral Chain of
Responsibility
Command
Interpreter
Iterator
Mediator
Memento
Observer
State
Strategy
Template Method
Visitor
object that can fulfill a request
when and how a request is fulfilled
grammar and interpretation of a language
how an aggregate's elements are accessed, traversed
how and which objects interact with each other
what private information is stored outside an object, and when
number of objects that depend on another object; how the
dependent objects stay up to date
states of an object
an algorithm
steps of an algorithm
operations that can be applied to object(s) without changing
their class(es
Object Oriented Programming with Java
Object Oriented Programming with Java
SINGLETON Design Pattern:
Intent:
Ensure a class only has one instance, and provide a global point of access to it.
Motivation :
It's important for some classes to have exactly one instance. Although there can be many printers in a system,
there should be only one printer spooler. There should be only one file system and one window manager. A
digital filter will have one A/D converter. An accounting system will be dedicated to serving one company.
How do we ensure that a class has only one instance and that the instance is easily accessible? A global
variable makes an object accessible, but it doesn't keep you from instantiating multiple objects.
A better solution is to make the class it self responsible for keeping track of its sole instance.
The class can ensure that no other instance can be created (by intercepting requests to create new objects),
and it can provide a way to access the instance.
This is the Singleton pattern.
Applicability Use the Singleton pattern when
• there must be exactly one instance of a class, and it must be accessible to clients from a well-known access
point.
• when the sole instance should be extensible by sub classing, and clients should be able to use an extended
instance without modifying their code.
Object Oriented Programming with Java
ADAPTER Design Pattern:
Intent:
Convert the interface of a class into another interface clients expect. Adapter lets classes work together that
couldn't otherwise because of incompatible interfaces.
Motivation :
Sometimes a toolkit class that's designed for reuse isn't reusable only because its interface doesn't match the
domain-specific interface an application requires. Consider for example a drawing editor that lets users draw
and arrange graphical elements (lines, polygons, text, etc.) into pictures and diagrams. The drawing editor's
key abstraction is the graphical object, which has an editable shape and can draw itself. The interface for
graphical objects is defined by an abstract class called Shape. The editor defines a subclass of Shape for each
kind of graphical object: a Line Shape class for lines, a Polygon Shape class for polygons, and so forth.
Applicability Use the Adapter pattern when
Use the Adapter pattern when
• you want to use an existing class, and its interface does not match the one you need.
• you want to create a reusable class that cooperates with unrelated or unfore-seen classes, that is, classes
that don't necessarily have compatible interfaces.
• (object adapter only) you need to use several existing subclasses, but it's im-practical to adapt their
interface by sub classing every one. An object adapter can adapt the interface of its parent class.
Object Oriented Programming with Java
ITERATOR Design Pattern:
Intent:
Provide a way to access the elements of an aggregate object sequentially without exposing its underlying
representation
Motivation :
An aggregate object such as a list should give you a way to access its elements without exposing its internal
structure. Moreover, you might want to traverse the list in different ways, depending on what you want to
accomplish. But you probably don't want to bloat the List interface with operations for different traversals,
even if you could anticipate the ones you will need. You might also need to have more than one traversal
pending on the same list
Applicability Use the Iterator pattern when
Use the Iterator pattern
• to access an aggregate object's contents without exposing its internal representation.
• to support multiple traversals of aggregate objects.
• to provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic
iteration).
Object Oriented Programming with Java
Thank You

More Related Content

PDF
Module 2 design patterns-2
Ankit Dubey
 
DOCX
Unit iii design patterns 9
kiruthikamurugesan2628
 
PPT
Chapter 4_Introduction to Patterns.ppt
RushikeshChikane1
 
PPT
Chapter 4_Introduction to Patterns.ppt
RushikeshChikane2
 
PPT
Design Patterns.ppt
TanishaKochak
 
PDF
Why Design Patterns Are Important In Software Engineering
Protelo, Inc.
 
PDF
130607 yann-gael gueheneuc - ptidej tool suite
Ptidej Team
 
PPTX
ap assignmnet presentation.pptx
AwanAdhikari
 
Module 2 design patterns-2
Ankit Dubey
 
Unit iii design patterns 9
kiruthikamurugesan2628
 
Chapter 4_Introduction to Patterns.ppt
RushikeshChikane1
 
Chapter 4_Introduction to Patterns.ppt
RushikeshChikane2
 
Design Patterns.ppt
TanishaKochak
 
Why Design Patterns Are Important In Software Engineering
Protelo, Inc.
 
130607 yann-gael gueheneuc - ptidej tool suite
Ptidej Team
 
ap assignmnet presentation.pptx
AwanAdhikari
 

Similar to Unit No 6 Design Patterns.pptx (20)

DOCX
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 
PDF
Style & Design Principles 02 - Design Patterns
Nick Pruehs
 
PDF
Design patterns through java
Aditya Bhuyan
 
PPTX
Design patterns
Akhilesh Gupta
 
PPTX
Design patterns
Kolade Ibrahim Arowolo
 
DOCX
Java Design Pattern Interview Questions
jbashask
 
PDF
(E book pdf) thinking in patterns with java
Raffaella D'angelo
 
DOCX
Design patterns
Vignesh Nethaji
 
PPTX
Software Patterns
bonej010
 
PPTX
Design pattern and their application
Hiệp Tiến
 
KEY
Practical OOP In Java
wiradikusuma
 
PPTX
PATTERNS01 - An Introduction to Design Patterns
Michael Heron
 
PDF
Computer Science application and Engineering
AbdulazizBUmar
 
PDF
software engineering Design Patterns.pdf
mulugetaberihun3
 
PPTX
Design Patterns
Sergii Stets
 
PPTX
L05 Design Patterns
Ólafur Andri Ragnarsson
 
PDF
Design Patterns - GOF
Fanus van Straten
 
DOCX
Design pattern application
gayatri thakur
 
PPTX
Design patterns software re engineering lect 10
HibaAmjadSiddiqui
 
PPS
11 ooad uml-14
Niit Care
 
Patterns (contd)Software Development ProcessDesign patte.docx
danhaley45372
 
Style & Design Principles 02 - Design Patterns
Nick Pruehs
 
Design patterns through java
Aditya Bhuyan
 
Design patterns
Akhilesh Gupta
 
Design patterns
Kolade Ibrahim Arowolo
 
Java Design Pattern Interview Questions
jbashask
 
(E book pdf) thinking in patterns with java
Raffaella D'angelo
 
Design patterns
Vignesh Nethaji
 
Software Patterns
bonej010
 
Design pattern and their application
Hiệp Tiến
 
Practical OOP In Java
wiradikusuma
 
PATTERNS01 - An Introduction to Design Patterns
Michael Heron
 
Computer Science application and Engineering
AbdulazizBUmar
 
software engineering Design Patterns.pdf
mulugetaberihun3
 
Design Patterns
Sergii Stets
 
L05 Design Patterns
Ólafur Andri Ragnarsson
 
Design Patterns - GOF
Fanus van Straten
 
Design pattern application
gayatri thakur
 
Design patterns software re engineering lect 10
HibaAmjadSiddiqui
 
11 ooad uml-14
Niit Care
 
Ad

More from DrYogeshDeshmukh1 (14)

PPTX
Unit No. 1 Introduction to Java.pptx
DrYogeshDeshmukh1
 
PPTX
Unit No 5 Files and Database Connectivity.pptx
DrYogeshDeshmukh1
 
PPTX
Unit No 4 Exception Handling and Multithreading.pptx
DrYogeshDeshmukh1
 
PPTX
Unit No 3 Inheritance annd Polymorphism.pptx
DrYogeshDeshmukh1
 
PPTX
Unit No 2 Objects and Classes.pptx
DrYogeshDeshmukh1
 
PPTX
Unit 6 Uncertainty.pptx
DrYogeshDeshmukh1
 
PPTX
Unit 5 Introduction to Planning and ANN.pptx
DrYogeshDeshmukh1
 
PPTX
Unit 4 Knowledge Representation.pptx
DrYogeshDeshmukh1
 
PPTX
Unit 3 Informed Search Strategies.pptx
DrYogeshDeshmukh1
 
PPTX
DAA Unit 1 Part 1.pptx
DrYogeshDeshmukh1
 
PPTX
AI Overview.pptx
DrYogeshDeshmukh1
 
PPTX
Unit 2 Uninformed Search Strategies.pptx
DrYogeshDeshmukh1
 
PPTX
Unit 1 Fundamentals of Artificial Intelligence-Part II.pptx
DrYogeshDeshmukh1
 
PPTX
Unit 1 Fundamentals of Artificial Intelligence-Part I.pptx
DrYogeshDeshmukh1
 
Unit No. 1 Introduction to Java.pptx
DrYogeshDeshmukh1
 
Unit No 5 Files and Database Connectivity.pptx
DrYogeshDeshmukh1
 
Unit No 4 Exception Handling and Multithreading.pptx
DrYogeshDeshmukh1
 
Unit No 3 Inheritance annd Polymorphism.pptx
DrYogeshDeshmukh1
 
Unit No 2 Objects and Classes.pptx
DrYogeshDeshmukh1
 
Unit 6 Uncertainty.pptx
DrYogeshDeshmukh1
 
Unit 5 Introduction to Planning and ANN.pptx
DrYogeshDeshmukh1
 
Unit 4 Knowledge Representation.pptx
DrYogeshDeshmukh1
 
Unit 3 Informed Search Strategies.pptx
DrYogeshDeshmukh1
 
DAA Unit 1 Part 1.pptx
DrYogeshDeshmukh1
 
AI Overview.pptx
DrYogeshDeshmukh1
 
Unit 2 Uninformed Search Strategies.pptx
DrYogeshDeshmukh1
 
Unit 1 Fundamentals of Artificial Intelligence-Part II.pptx
DrYogeshDeshmukh1
 
Unit 1 Fundamentals of Artificial Intelligence-Part I.pptx
DrYogeshDeshmukh1
 
Ad

Recently uploaded (20)

PDF
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
PPTX
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
PDF
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
PPTX
Tunnel Ventilation System in Kanpur Metro
220105053
 
PDF
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
PDF
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
PPTX
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
PPTX
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
PDF
Introduction to Data Science: data science process
ShivarkarSandip
 
PDF
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
PDF
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
PPTX
Inventory management chapter in automation and robotics.
atisht0104
 
PPTX
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
PDF
Software Testing Tools - names and explanation
shruti533256
 
PPTX
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
PDF
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
PDF
Zero carbon Building Design Guidelines V4
BassemOsman1
 
PDF
Zero Carbon Building Performance standard
BassemOsman1
 
PPTX
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
PDF
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 
2025 Laurence Sigler - Advancing Decision Support. Content Management Ecommer...
Francisco Javier Mora Serrano
 
database slide on modern techniques for optimizing database queries.pptx
aky52024
 
dse_final_merit_2025_26 gtgfffffcjjjuuyy
rushabhjain127
 
Tunnel Ventilation System in Kanpur Metro
220105053
 
settlement FOR FOUNDATION ENGINEERS.pdf
Endalkazene
 
Top 10 read articles In Managing Information Technology.pdf
IJMIT JOURNAL
 
FUNDAMENTALS OF ELECTRIC VEHICLES UNIT-1
MikkiliSuresh
 
22PCOAM21 Session 1 Data Management.pptx
Guru Nanak Technical Institutions
 
Introduction to Data Science: data science process
ShivarkarSandip
 
Unit I Part II.pdf : Security Fundamentals
Dr. Madhuri Jawale
 
Natural_Language_processing_Unit_I_notes.pdf
sanguleumeshit
 
Inventory management chapter in automation and robotics.
atisht0104
 
business incubation centre aaaaaaaaaaaaaa
hodeeesite4
 
Software Testing Tools - names and explanation
shruti533256
 
Module2 Data Base Design- ER and NF.pptx
gomathisankariv2
 
20ME702-Mechatronics-UNIT-1,UNIT-2,UNIT-3,UNIT-4,UNIT-5, 2025-2026
Mohanumar S
 
Zero carbon Building Design Guidelines V4
BassemOsman1
 
Zero Carbon Building Performance standard
BassemOsman1
 
MT Chapter 1.pptx- Magnetic particle testing
ABCAnyBodyCanRelax
 
2010_Book_EnvironmentalBioengineering (1).pdf
EmilianoRodriguezTll
 

Unit No 6 Design Patterns.pptx

  • 1. Object Oriented Programming with Java Unit No. 6: Design Patterns Sanjivani Rural Education Society’s Sanjivani College of Engineering, Kopargaon-423603 (An Autonomous Institute Affiliated to Savitribai Phule Pune University, Pune) NAAC ‘A’ Grade Accredited, ISO 9001:2015 Certified Department of Information Technology (UG Course: NBA Accredited) Dr. Y.S.Deshmukh Assistant Professor
  • 2. Object Oriented Programming with Java What is Design Pattern?: Christopher Alexander says, "Each 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" [AIS+77, page x}. Even though Alexander was talking about patterns in buildings and towns, what he says is true about object-oriented design patterns. Our solutions are expressed in terms of objects and interfaces instead of walls and doors, but at the core of both kinds of patterns is a solution to a problem in a context.
  • 3. Object Oriented Programming with Java In general, a pattern has four essential elements: 1. The pattern name is a handle we can use to describe a design problem, its solution, and consequences in a word or two. Naming a pattern immediately increases our design vocabulary. It lets us design at a higher level of abstraction. Having a vocabulary for patterns lets us talk about them with our colleagues, in our documentation, and even to ourselves. It makes it easier to think about designs and to communicate them and their trade-offs to others. Finding good names has been one of the hardest parts of developing our catalog. 2. The problem describes when to apply the pattern. It explains the problem and its context. It might describe specific design problems such as how to represent algorithms as objects. It might describe class or object structures that are symptomatic of an inflexible design. Sometimes the problem will include a list of conditions that must be met before it makes sense to apply the pattern.
  • 4. Object Oriented Programming with Java In general, a pattern has four essential elements: 3. The solution describes the elements that make up the design, their relationships, responsibilities, and collaborations. The solution doesn't describe a particular concrete design or implementation, because a pattern is like a template that can be applied in many different situations. Instead, the pattern provides an abstract description of a design problem and how a general arrangement of elements (classes and objects in our case) solves it. 4. The consequences are the results and trade-offs of applying the pattern. Though consequences are often unvoiced when we describe design decisions, they are critical for evaluating design alternatives and for understanding the costs and benefits of applying the pattern. The consequences for software often concern space and time trade-offs. They may address language and implementation issues as well. Since reuse is often a factor in object-oriented design, the consequences of a pattern include its impact on a system's flexibility, extensibility, or portability. Listing these consequences explicitly helps you understand and evaluate them.
  • 5. Object Oriented Programming with Java How to Select a Design Pattern?: With more than 20 design patterns in the catalog to choose from, it might be hard to find the one that addresses a particular design problem, especially if the catalog is new and unfamiliar to you. Here are several different approaches to finding the design pattern that's right for your problem: • Consider how design patterns solve design problems. Section 1.6 discusses how design patterns help you find appropriate objects, determine object granularity, specify object interfaces, and several other ways in which design patterns solve design problems. Referring to these discussions can help guide your search for the right pattern. • Scan Intent sections. Lists the Intent sections from all the patterns in the catalog. Read through each pattern's intent to find one or more that sound relevant to your problem. You can use the classification scheme to narrow your search.
  • 6. Object Oriented Programming with Java How to Select a Design Pattern?: • Study how patterns interrelate. Figure shows relationships between design patterns graphically. Studying these relationships can help direct you to the right pattern or group of patterns. • Study patterns of like purpose. The catalog has three chapters, one for creational patterns, another for structural patterns, and a third for behavioral patterns. Each chapter starts off with introductory comments on the patterns and concludes with a section that compares and contrasts them. These sections give you insight into the similarities and differences between patterns of like purpose. • Examine a cause of redesign. Look at the causes of redesign. Then look at the patterns that help you avoid the causes of redesign. • Consider what should be variable in your design. This approach is the opposite of focusing on the causes of redesign. Instead of considering what might force a change to a design, consider what you want to be able to change without redesign. The focus here is on encapsulating the concept that varies, a theme of many design patterns.
  • 7. Object Oriented Programming with Java How to Use a Design Pattern?: Once you've picked a design pattern, how do you use it? Here's a step-by-step approach to applying a design pattern effectively: 1. Read the pattern once through for an overview. Pay particular attention to the Applicability and Consequences sections to ensure the pattern is right for your problem. 2. Go back and study the Structure, Participants, and Collaborations sections. Make sure you understand the classes and objects in the pattern and how they relate to one another. 3. Look at the Sample Code section to see a concrete example of the pattern in code. Studying the code helps you learn how to implement the pattern. 4. Choose names for pattern participants that are meaningful in the application context. The names for participants in design patterns are usually too abstract to appear directly in an application. Nevertheless, it's useful to incorporate the participant name into the name that appears in the application. That helps make the pattern more explicit in the implementation. For example, if you use the Strategy pattern for a text compositing algorithm, then you might have classes SimpleLayoutStrategy or TeXLayoutStrategy.
  • 8. Object Oriented Programming with Java How to Use a Design Pattern?: Once you've picked a design pattern, how do you use it? Here's a step-by-step approach to applying a design pattern effectively: 5. Define the classes. Declare their interfaces, establish their inheritance relationships, and define the instance variables that represent data and object references. Identify existing classes in your application that the pattern will affect, and modify them accordingly. 6. Define application-specific names for operations in the pattern. Here again, the names generally depend on the application. Use the responsibilities and collaborations associated with each operation as a guide. Also, be consistent in your naming conventions. For example, you might use the "Create-" prefix consistently to denote a factory method 7. Implement the operations to carry out the responsibilities and collaborations in the pattern. The Implementation section offers hints to guide you in the implementation. The examples in the Sample Code section can help as well.
  • 9. Object Oriented Programming with Java Purpose Design Pattern Aspect(s) That Can Vary Creational Abstract Factory families of product objects Builder how a composite object gets created Factory Method subclass of object that is instantiated Prototype class of object that is instantiated Singleton the sole instance of a class Structural Adapter Bridge Composite Decorator Facade Flyweight Proxy interface to an object implementation of an object structure and composition of an object responsibilities of an object without sub classing interface to a subsystem storage costs of objects how an object is accessed; its location
  • 10. Object Oriented Programming with Java Purpose Design Pattern Aspect(s) That Can Vary Behavioral Chain of Responsibility Command Interpreter Iterator Mediator Memento Observer State Strategy Template Method Visitor object that can fulfill a request when and how a request is fulfilled grammar and interpretation of a language how an aggregate's elements are accessed, traversed how and which objects interact with each other what private information is stored outside an object, and when number of objects that depend on another object; how the dependent objects stay up to date states of an object an algorithm steps of an algorithm operations that can be applied to object(s) without changing their class(es
  • 12. Object Oriented Programming with Java SINGLETON Design Pattern: Intent: Ensure a class only has one instance, and provide a global point of access to it. Motivation : It's important for some classes to have exactly one instance. Although there can be many printers in a system, there should be only one printer spooler. There should be only one file system and one window manager. A digital filter will have one A/D converter. An accounting system will be dedicated to serving one company. How do we ensure that a class has only one instance and that the instance is easily accessible? A global variable makes an object accessible, but it doesn't keep you from instantiating multiple objects. A better solution is to make the class it self responsible for keeping track of its sole instance. The class can ensure that no other instance can be created (by intercepting requests to create new objects), and it can provide a way to access the instance. This is the Singleton pattern. Applicability Use the Singleton pattern when • there must be exactly one instance of a class, and it must be accessible to clients from a well-known access point. • when the sole instance should be extensible by sub classing, and clients should be able to use an extended instance without modifying their code.
  • 13. Object Oriented Programming with Java ADAPTER Design Pattern: Intent: Convert the interface of a class into another interface clients expect. Adapter lets classes work together that couldn't otherwise because of incompatible interfaces. Motivation : Sometimes a toolkit class that's designed for reuse isn't reusable only because its interface doesn't match the domain-specific interface an application requires. Consider for example a drawing editor that lets users draw and arrange graphical elements (lines, polygons, text, etc.) into pictures and diagrams. The drawing editor's key abstraction is the graphical object, which has an editable shape and can draw itself. The interface for graphical objects is defined by an abstract class called Shape. The editor defines a subclass of Shape for each kind of graphical object: a Line Shape class for lines, a Polygon Shape class for polygons, and so forth. Applicability Use the Adapter pattern when Use the Adapter pattern when • you want to use an existing class, and its interface does not match the one you need. • you want to create a reusable class that cooperates with unrelated or unfore-seen classes, that is, classes that don't necessarily have compatible interfaces. • (object adapter only) you need to use several existing subclasses, but it's im-practical to adapt their interface by sub classing every one. An object adapter can adapt the interface of its parent class.
  • 14. Object Oriented Programming with Java ITERATOR Design Pattern: Intent: Provide a way to access the elements of an aggregate object sequentially without exposing its underlying representation Motivation : An aggregate object such as a list should give you a way to access its elements without exposing its internal structure. Moreover, you might want to traverse the list in different ways, depending on what you want to accomplish. But you probably don't want to bloat the List interface with operations for different traversals, even if you could anticipate the ones you will need. You might also need to have more than one traversal pending on the same list Applicability Use the Iterator pattern when Use the Iterator pattern • to access an aggregate object's contents without exposing its internal representation. • to support multiple traversals of aggregate objects. • to provide a uniform interface for traversing different aggregate structures (that is, to support polymorphic iteration).
  • 15. Object Oriented Programming with Java Thank You