SlideShare a Scribd company logo
1 of 20
Approaching Patterns
How to Apply
Problem Statement
• Cat Simulation
• Walk, run, purr, jump, wag tail
Using OOPS
• Using traditional OOP we take common features of all Cats and put them in a base class. Since the
requirement mentioned simulation of different Cats we then create different Cat classes (Siamese,
Persian)
• Persian Class will implement its own Display function and so too Siamese.
Change is always a Constant
• The requirements change after a client review
meeting and now they want to add
functionality that will simulate eating
attribute. So all Cats need to eat.
What do we do
• Using power of inheritance, I just have to add
a method called Eat() to the base class. Right ?
• What if tomorrow requirement changes to say that
they want to add a feature to simulate toy Cats. The
client wants to make their simulation kids friendly.
• We know there is a potential problem that a Toy Cat
can possibly Purr, Walk, (battery operated) but can it
eat?
• So again we can use the power of inheritance
and override the functions that we don’t want
to use and ask it to not implement anything.
Problems in above design
• Maintenance nightmare
• Readability
• Intuitive
Sharpen What you learnt
• Which of the following are disadvantages of
using inheritance to provide Cat type
behavior? (Choose all that apply.)
A. Code is duplicated across subclasses.
B. Runtime behavior changes are difficult.
C. We can’t make Cats dance.
D. Hard to gain knowledge of all Cats behaviors.
E. Cats can’t Purr and run at the same time.
F. Changes can unintentionally affect other Cats
Interface – How would it help
• Take what varies and “encapsulate”
• Separate the “parts that change from those
that stay the same”
• Create two sets of Interfaces (totally apart
from Cat), one for IEat and one for IPurr.
• Each set of concrete classes will hold all the
implementations of their respective behavior.
• This is what our new Cats class looks like.
• What we have effectively done is that a Cat
will now delegate its Purring and Eating
behaviour, instead of using Purring methods
defined in the Cat class (or subclasses).
public Class Cats
{
public IEat eat;
public IPurr meow;
.
public void ExecuteEat()
{
eat.Eat();
}
public void ExecutePurr()
{
meow.Purr();
}
}
Public Class Persian : Cats, IEat, IPurr
{
public Persian()
{
eat = new CanEat();
meow = new Whimper();
}
}
Public Class ToyCat : Cats, IEat, IPurr
{
public ToyCat()
{
eat = new CantEat();
}
}
Sample Client code
Public Static Void Main(String[] args….)
{
Cats c = new Persian();
c.ExecutePurr();
c.ExecuteEat();
}
Flexibility and Code Maintenance
• There is still room for flexibility
• We are doing a poor job of initializing the
instance variables in a flexible way
• Polymorphism : The PurrBehavior instance
variable is an interface type. Dynamically
assign a different PurrBehavior
implementation class at runtime or even from
the client without having to alter code.
• Let us make changes below to the Cat class.
We are introducing two methods
– SetPurrBehaviour(PurrBehaviour b)
– SetEatBehaviour(EatBehaviour e)
• From the client code now we can dynamically
add a new behaviour and without modifying
lots of code add extra functionality.
public Class Cats
{
public IEat eat;
public IPurr meow;
.
public void SetPurrBehaviour(IPurr b)
{
meow = b;
}
public void SetEatBehaviour(IEat e)
{
eat = e;
}
}
public class Hiss : IPurr
{
public void Purr()
{
// HISS HISS
}
}
Client Code looks like
• Static void main()
• {
– Cat p = new Persian();
– P. SetPurrBehaviour(new Hiss ());
– P.ExecutePurr();
• }
Welcome to the Strategy Pattern
• Patterns should be applied depending on the
need of the situation.
• Apply basic OOPS concepts first then refactor.
• Separate what changes
• Always code to an Interface
• Open for extension and closed for
modification
• Favor composition over inheritance.

More Related Content

Viewers also liked

Sollicitatiecafé aankondiging 15 december 2015
Sollicitatiecafé aankondiging 15 december 2015Sollicitatiecafé aankondiging 15 december 2015
Sollicitatiecafé aankondiging 15 december 2015Sandra van Megen
 
A Rapid and Innovative Method for Prediction of Spin Multiplicity and Spin St...
A Rapid and Innovative Method for Prediction of Spin Multiplicity and Spin St...A Rapid and Innovative Method for Prediction of Spin Multiplicity and Spin St...
A Rapid and Innovative Method for Prediction of Spin Multiplicity and Spin St...iosrjce
 
HCQS.CC | Replacing iPhone 6 Lightning Connector Assembly Guide
HCQS.CC | Replacing iPhone 6 Lightning Connector Assembly GuideHCQS.CC | Replacing iPhone 6 Lightning Connector Assembly Guide
HCQS.CC | Replacing iPhone 6 Lightning Connector Assembly GuideWholesale iPhone LCD iPhone Parts
 
El huerto ecologico
El huerto ecologicoEl huerto ecologico
El huerto ecologiconoeliandy
 
Generation and Management of Electrical and Electronic Wastes (E-waste) in Ab...
Generation and Management of Electrical and Electronic Wastes (E-waste) in Ab...Generation and Management of Electrical and Electronic Wastes (E-waste) in Ab...
Generation and Management of Electrical and Electronic Wastes (E-waste) in Ab...iosrjce
 
Experimental Determination of Compressibility Factors of Gases
Experimental Determination of Compressibility Factors of GasesExperimental Determination of Compressibility Factors of Gases
Experimental Determination of Compressibility Factors of Gasesiosrjce
 
112615_labmeeting
112615_labmeeting112615_labmeeting
112615_labmeetingHyesoo Yoo
 
Comparative Analysis of Physicochemical Parameters and Heavy Metals of Public...
Comparative Analysis of Physicochemical Parameters and Heavy Metals of Public...Comparative Analysis of Physicochemical Parameters and Heavy Metals of Public...
Comparative Analysis of Physicochemical Parameters and Heavy Metals of Public...iosrjce
 
Electrochemical Degradation of Methylen Blue Using Carbon Composite Electrode...
Electrochemical Degradation of Methylen Blue Using Carbon Composite Electrode...Electrochemical Degradation of Methylen Blue Using Carbon Composite Electrode...
Electrochemical Degradation of Methylen Blue Using Carbon Composite Electrode...iosrjce
 
Keynote #Society - Open Source, les pratiques et les tendances, par Gaël MUSQUET
Keynote #Society - Open Source, les pratiques et les tendances, par Gaël MUSQUETKeynote #Society - Open Source, les pratiques et les tendances, par Gaël MUSQUET
Keynote #Society - Open Source, les pratiques et les tendances, par Gaël MUSQUETParis Open Source Summit
 

Viewers also liked (11)

Sollicitatiecafé aankondiging 15 december 2015
Sollicitatiecafé aankondiging 15 december 2015Sollicitatiecafé aankondiging 15 december 2015
Sollicitatiecafé aankondiging 15 december 2015
 
A Rapid and Innovative Method for Prediction of Spin Multiplicity and Spin St...
A Rapid and Innovative Method for Prediction of Spin Multiplicity and Spin St...A Rapid and Innovative Method for Prediction of Spin Multiplicity and Spin St...
A Rapid and Innovative Method for Prediction of Spin Multiplicity and Spin St...
 
HCQS.CC | Replacing iPhone 6 Lightning Connector Assembly Guide
HCQS.CC | Replacing iPhone 6 Lightning Connector Assembly GuideHCQS.CC | Replacing iPhone 6 Lightning Connector Assembly Guide
HCQS.CC | Replacing iPhone 6 Lightning Connector Assembly Guide
 
El huerto ecologico
El huerto ecologicoEl huerto ecologico
El huerto ecologico
 
Multimedia
MultimediaMultimedia
Multimedia
 
Generation and Management of Electrical and Electronic Wastes (E-waste) in Ab...
Generation and Management of Electrical and Electronic Wastes (E-waste) in Ab...Generation and Management of Electrical and Electronic Wastes (E-waste) in Ab...
Generation and Management of Electrical and Electronic Wastes (E-waste) in Ab...
 
Experimental Determination of Compressibility Factors of Gases
Experimental Determination of Compressibility Factors of GasesExperimental Determination of Compressibility Factors of Gases
Experimental Determination of Compressibility Factors of Gases
 
112615_labmeeting
112615_labmeeting112615_labmeeting
112615_labmeeting
 
Comparative Analysis of Physicochemical Parameters and Heavy Metals of Public...
Comparative Analysis of Physicochemical Parameters and Heavy Metals of Public...Comparative Analysis of Physicochemical Parameters and Heavy Metals of Public...
Comparative Analysis of Physicochemical Parameters and Heavy Metals of Public...
 
Electrochemical Degradation of Methylen Blue Using Carbon Composite Electrode...
Electrochemical Degradation of Methylen Blue Using Carbon Composite Electrode...Electrochemical Degradation of Methylen Blue Using Carbon Composite Electrode...
Electrochemical Degradation of Methylen Blue Using Carbon Composite Electrode...
 
Keynote #Society - Open Source, les pratiques et les tendances, par Gaël MUSQUET
Keynote #Society - Open Source, les pratiques et les tendances, par Gaël MUSQUETKeynote #Society - Open Source, les pratiques et les tendances, par Gaël MUSQUET
Keynote #Society - Open Source, les pratiques et les tendances, par Gaël MUSQUET
 

Similar to Approaching Patterns How to Apply

cs1311lecture22wdl (1).ppt
cs1311lecture22wdl (1).pptcs1311lecture22wdl (1).ppt
cs1311lecture22wdl (1).pptSANDIPPRADHANBWU
 
cs1311lecture22wdl.ppt
cs1311lecture22wdl.pptcs1311lecture22wdl.ppt
cs1311lecture22wdl.pptammu241754
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slidesluqman bawany
 
Screen transitions with ease
Screen transitions with easeScreen transitions with ease
Screen transitions with easeOlivier Tassinari
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1Sherihan Anver
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptxKrutikaWankhade1
 
Pattern matching presentation
Pattern matching presentationPattern matching presentation
Pattern matching presentationMichael Dimmitt
 
Working Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in PracticeWorking Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in PracticeAmar Shah
 
Effectively Using UI Automation
Effectively Using UI AutomationEffectively Using UI Automation
Effectively Using UI AutomationAlexander Repty
 
Scalamen and OT
Scalamen and OTScalamen and OT
Scalamen and OTgetch123
 
Introduction to Python programming 1.pptx
Introduction to Python programming 1.pptxIntroduction to Python programming 1.pptx
Introduction to Python programming 1.pptxJoshuaAnnan5
 
Puppet Camp Tokyo 2014: Why we stopped using Puppet Agent Daemon
Puppet Camp Tokyo 2014: Why we stopped using Puppet Agent Daemon Puppet Camp Tokyo 2014: Why we stopped using Puppet Agent Daemon
Puppet Camp Tokyo 2014: Why we stopped using Puppet Agent Daemon Puppet
 

Similar to Approaching Patterns How to Apply (20)

cs1311lecture22wdl (1).ppt
cs1311lecture22wdl (1).pptcs1311lecture22wdl (1).ppt
cs1311lecture22wdl (1).ppt
 
Pplymorphism
PplymorphismPplymorphism
Pplymorphism
 
cs1311lecture22wdl.ppt
cs1311lecture22wdl.pptcs1311lecture22wdl.ppt
cs1311lecture22wdl.ppt
 
Introducing E-Cell 3.2
Introducing E-Cell 3.2Introducing E-Cell 3.2
Introducing E-Cell 3.2
 
Introduction to c first week slides
Introduction to c first week slidesIntroduction to c first week slides
Introduction to c first week slides
 
Screen transitions with ease
Screen transitions with easeScreen transitions with ease
Screen transitions with ease
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
Chapter8
Chapter8Chapter8
Chapter8
 
Advanced Java Testing
Advanced Java TestingAdvanced Java Testing
Advanced Java Testing
 
Java interview questions 1
Java interview questions 1Java interview questions 1
Java interview questions 1
 
Super Keyword in Java.pptx
Super Keyword in Java.pptxSuper Keyword in Java.pptx
Super Keyword in Java.pptx
 
Pattern matching presentation
Pattern matching presentationPattern matching presentation
Pattern matching presentation
 
Working Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in PracticeWorking Effectively with Legacy Code: Lessons in Practice
Working Effectively with Legacy Code: Lessons in Practice
 
Di & dagger
Di & daggerDi & dagger
Di & dagger
 
java.pptx
java.pptxjava.pptx
java.pptx
 
Effectively Using UI Automation
Effectively Using UI AutomationEffectively Using UI Automation
Effectively Using UI Automation
 
Scalamen and OT
Scalamen and OTScalamen and OT
Scalamen and OT
 
Introduction to Python programming 1.pptx
Introduction to Python programming 1.pptxIntroduction to Python programming 1.pptx
Introduction to Python programming 1.pptx
 
Puppet Camp Tokyo 2014: Why we stopped using Puppet Agent Daemon
Puppet Camp Tokyo 2014: Why we stopped using Puppet Agent Daemon Puppet Camp Tokyo 2014: Why we stopped using Puppet Agent Daemon
Puppet Camp Tokyo 2014: Why we stopped using Puppet Agent Daemon
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 

Approaching Patterns How to Apply

  • 2. Problem Statement • Cat Simulation • Walk, run, purr, jump, wag tail
  • 3. Using OOPS • Using traditional OOP we take common features of all Cats and put them in a base class. Since the requirement mentioned simulation of different Cats we then create different Cat classes (Siamese, Persian) • Persian Class will implement its own Display function and so too Siamese.
  • 4. Change is always a Constant • The requirements change after a client review meeting and now they want to add functionality that will simulate eating attribute. So all Cats need to eat.
  • 5. What do we do • Using power of inheritance, I just have to add a method called Eat() to the base class. Right ?
  • 6.
  • 7. • What if tomorrow requirement changes to say that they want to add a feature to simulate toy Cats. The client wants to make their simulation kids friendly. • We know there is a potential problem that a Toy Cat can possibly Purr, Walk, (battery operated) but can it eat?
  • 8. • So again we can use the power of inheritance and override the functions that we don’t want to use and ask it to not implement anything.
  • 9. Problems in above design • Maintenance nightmare • Readability • Intuitive
  • 10. Sharpen What you learnt • Which of the following are disadvantages of using inheritance to provide Cat type behavior? (Choose all that apply.) A. Code is duplicated across subclasses. B. Runtime behavior changes are difficult. C. We can’t make Cats dance. D. Hard to gain knowledge of all Cats behaviors. E. Cats can’t Purr and run at the same time. F. Changes can unintentionally affect other Cats
  • 11. Interface – How would it help • Take what varies and “encapsulate” • Separate the “parts that change from those that stay the same” • Create two sets of Interfaces (totally apart from Cat), one for IEat and one for IPurr. • Each set of concrete classes will hold all the implementations of their respective behavior.
  • 12.
  • 13. • This is what our new Cats class looks like. • What we have effectively done is that a Cat will now delegate its Purring and Eating behaviour, instead of using Purring methods defined in the Cat class (or subclasses).
  • 14. public Class Cats { public IEat eat; public IPurr meow; . public void ExecuteEat() { eat.Eat(); } public void ExecutePurr() { meow.Purr(); } } Public Class Persian : Cats, IEat, IPurr { public Persian() { eat = new CanEat(); meow = new Whimper(); } } Public Class ToyCat : Cats, IEat, IPurr { public ToyCat() { eat = new CantEat(); } }
  • 15. Sample Client code Public Static Void Main(String[] args….) { Cats c = new Persian(); c.ExecutePurr(); c.ExecuteEat(); }
  • 16. Flexibility and Code Maintenance • There is still room for flexibility • We are doing a poor job of initializing the instance variables in a flexible way • Polymorphism : The PurrBehavior instance variable is an interface type. Dynamically assign a different PurrBehavior implementation class at runtime or even from the client without having to alter code.
  • 17. • Let us make changes below to the Cat class. We are introducing two methods – SetPurrBehaviour(PurrBehaviour b) – SetEatBehaviour(EatBehaviour e) • From the client code now we can dynamically add a new behaviour and without modifying lots of code add extra functionality.
  • 18. public Class Cats { public IEat eat; public IPurr meow; . public void SetPurrBehaviour(IPurr b) { meow = b; } public void SetEatBehaviour(IEat e) { eat = e; } } public class Hiss : IPurr { public void Purr() { // HISS HISS } }
  • 19. Client Code looks like • Static void main() • { – Cat p = new Persian(); – P. SetPurrBehaviour(new Hiss ()); – P.ExecutePurr(); • }
  • 20. Welcome to the Strategy Pattern • Patterns should be applied depending on the need of the situation. • Apply basic OOPS concepts first then refactor. • Separate what changes • Always code to an Interface • Open for extension and closed for modification • Favor composition over inheritance.

Editor's Notes

  1. We are asked to develop a simulation kind of application that the client wants to simulate whatever a Cat does. That is the client wants to have features such as walk, run, purr, jump, wag tail.
  2. It is simple, just add a function called Eat() to the super class Cats and with the magic of inheritance all Cats will now Eat.
  3. As requirements keep changing the above design will turn out to be a maintenance nightmare. For every class or entity that we add we might end up having lots of functions that will not have any implementation.
  4. The result? Fewer unintended consequences from code changes and more flexibility in your systems For instance, we might have one class that implements Purring, another that implements Eating, and another that implements silence.