SlideShare une entreprise Scribd logo
1  sur  20
COMP23420 week 5:
GRASP design principles and
       inheritance

John Sargeant (johns@cs.man.ac.uk)
GRASP principles - reminder

• High cohesion – each class should represent a single
  well-defined entity
• Low coupling – classes should not be overly
  dependent on each other.
• Pure fabrication – add extra classes, not in the
  domain, to improve cohesion and coupling
• Information expert – put operations in the classes
  which contain the information to carry them out (and
  vice versa).
GRASP: Protected variations
• Design so that if some thing changes, the rest of the
  code is not badly affected, or,,
• abstract the Thing Which Varies, so when the Thing
  Which Varies varies, it doesn’t trash the rest of the
  code.
• Example: the Java Calender and DateFormat classes
  protect against variations in locale by providing for
  different timezones, date/time formats etc.
• Problem: many things can vary, it’s hard to predict
  which ones will matter, and you have finite effort.
  (See Larman pp 427-434 for a discussion).
GRASP: Polymorphism
• Use inheritance to improve cohesion, reduce
  coupling, and protect against variation.
• NB: in the OO community polymorphism ==
  inheritance, in academic CS, inheritance is just one
  form of polymorphism.
• Inheritance used properly is very powerful – dynamic
  binding is better than if-statements, but:
• observation: inexperienced designers (and also
  some who should know better) overuse inheritance.
• Important question: when is it appropriate to use
  inheritance?
Inheritance rules
Only use inheritance if:

3. There is a clear is-a-kind of relationship between the
   superclass and its subclasses.
4. Each of the subclasses is different in some non-
   trivial way from the others and from the superclass
5. Every object of the superclass type belongs to
   exactly one subclass.

NB: Inheritance is not primarily about code reuse – that
  can be done in other ways.
Case study - reminder
You have been hired to develop an integrated
  information system for the Irwell online store. They
  originated as a book store but now sell other forms of
  media such as CDs and DVDs. They also intend to
  branch out into selling electronic devices such as
  laptops and PDAs although they don’t intend to
  become a general store.

Initially you will develop an inventory system: an
   inventory lists the products a company has in stock,
   along with information such as prices.
Representing products
Good design?
• All three conditions are met
• New kinds of product require new subclasses, ok
  provided that “they don’t intend to become a general
  store.”
• But what if this changes? Can’t keep changing the
  code each time a different product appears.
• Option 1: use inheritance to represent broad
  categories of products.
• Option 2: represent product categories as a single
  class.
Option 1




Allows us to change the category of a product (e.g. if its
   VAT classification changes) but still requires extra
   classes for extra categories.
Option 2




• The most flexible option, but may be hard to
  determine which properties go with which categories.
Classes vs. instances
• Sometimes it can be hard to figure out which things
  should be represented as separate classes, and
  which should be just instances of a more general
  class. (As above)
• Sometimes it’s easy:

public class Footballer
public class WayneRooney extends Footballer


Hopefully you’d never dream of doing this!
A natural example?
• We could represent players in a football management
  game like this…
Inheritance and roles

• What if a player plays multiple positions?
• We’ve broken rule 3 – more than one software object
  may represent the same physical object
• Results in redundancy and potential inconsistency of
  data – e.g. if a player changes club we need to
  change this information several times.
• Hard to change the set of roles (positions) once the
  data is in place.
Alternatives
Advantages
• Information about a footballer, e.g. club, is held in just
  one object
• Roles (positions) can change over time
• New positions can easily be added, and made more
  fine-grain (e.g. wing-back).
• Code using the Footballer class is decoupled from
  how position is represented – so easy to switch
  between the two alternatives.
• Generally better to start with the simple design, and
  introduce inheritance if needed later.
Indirect object creation
Creating objects:

new ConcreteClassName(actual parameters)


is the weak link in encapsulation. We have to provide
  an actual concrete class name, and actual
  parameters.

Often best to create objects indirectly, e.g. we use
  getGraphics() to get an instance of the (abstract)
  java.awt.Graphics class.
Factories
• A factory is a class whose sole purpose is to create
  objects of other classes
• Used when object creation is complex, or dependent
  on external factors (e.g. platform dependencies)
• Example: a FontFactory which gets the best
  approximation to a font with a particular description
  on a particular platform.
• The objects to be created can be specified in many
  ways, e.g. via a config file.
• Factories can implement caching and other object-
  management functions.
Factories and GRASP
• Factories are Pure Fabrications
• They are also information experts, they contain, or
  have access to, the information needed to create
  objects.
• They decouple object creation from the code that
  uses those objects…
• … thereby improving the cohesion of both.

Like any other technique, factories are not always
   applicable – often creating objects directly is fine.
Assorted design tips
• Do everything in exactly one place
• Keep classes and methods small.
• Start with the simplest design which can possibly
  work – you can always embellish it afterwards
• Assume that the requirements will change during
  (and quite possibly as a result of) design
• Refactor early and often
• Inspect and think about designs of experienced
  designers
• Learn about design patterns (see Larman and
  COMP33411)
Things to think about
• “Inheritance is not primarily about code reuse – that
  can be done in other ways.” What other ways?
• Think of half a dozen possible uses of inheritance,
  and go through them to decide whether they are good
  ones or not.
• In Java (unlike e.g. C++) you can only inherit
  implementation from one class. Can you think of an
  example where you would really want to extend more
  than one class?
• Apart from the FontFactory example given, what
  other examples can you think of where a Factory
  could be useful?

Contenu connexe

Similaire à Week5 inhertiance

Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
stanbridge
 
Design Patterns .Net
Design Patterns .NetDesign Patterns .Net
Design Patterns .Net
Hariom Shah
 
Design patterns
Design patternsDesign patterns
Design patterns
Alok Guha
 

Similaire à Week5 inhertiance (20)

Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.ppt
 
Code Refactoring
Code RefactoringCode Refactoring
Code Refactoring
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)
 
80410172053.pdf
80410172053.pdf80410172053.pdf
80410172053.pdf
 
Design_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.pptDesign_Patterns_Dr.CM.ppt
Design_Patterns_Dr.CM.ppt
 
Improving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design PrinciplesImproving Software Quality Using Object Oriented Design Principles
Improving Software Quality Using Object Oriented Design Principles
 
Week3 to-design
Week3 to-designWeek3 to-design
Week3 to-design
 
Design p atterns
Design p atternsDesign p atterns
Design p atterns
 
Introduction To Design Patterns Class 4 Composition vs Inheritance
 Introduction To Design Patterns Class 4 Composition vs Inheritance Introduction To Design Patterns Class 4 Composition vs Inheritance
Introduction To Design Patterns Class 4 Composition vs Inheritance
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPT
 
Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)Cs 1023 lec 8 design pattern (week 2)
Cs 1023 lec 8 design pattern (week 2)
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptx
 
Bad Smells in Code
Bad Smells in CodeBad Smells in Code
Bad Smells in Code
 
Design Patterns .Net
Design Patterns .NetDesign Patterns .Net
Design Patterns .Net
 
Object oriented programming
Object oriented programmingObject oriented programming
Object oriented programming
 
CoreData
CoreDataCoreData
CoreData
 
Design patterns
Design patternsDesign patterns
Design patterns
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programming
 
Intro to oop.pptx
Intro to oop.pptxIntro to oop.pptx
Intro to oop.pptx
 

Dernier

CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
giselly40
 

Dernier (20)

2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)Powerful Google developer tools for immediate impact! (2023-24 C)
Powerful Google developer tools for immediate impact! (2023-24 C)
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Evaluating the top large language models.pdf
Evaluating the top large language models.pdfEvaluating the top large language models.pdf
Evaluating the top large language models.pdf
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 

Week5 inhertiance

  • 1. COMP23420 week 5: GRASP design principles and inheritance John Sargeant (johns@cs.man.ac.uk)
  • 2. GRASP principles - reminder • High cohesion – each class should represent a single well-defined entity • Low coupling – classes should not be overly dependent on each other. • Pure fabrication – add extra classes, not in the domain, to improve cohesion and coupling • Information expert – put operations in the classes which contain the information to carry them out (and vice versa).
  • 3. GRASP: Protected variations • Design so that if some thing changes, the rest of the code is not badly affected, or,, • abstract the Thing Which Varies, so when the Thing Which Varies varies, it doesn’t trash the rest of the code. • Example: the Java Calender and DateFormat classes protect against variations in locale by providing for different timezones, date/time formats etc. • Problem: many things can vary, it’s hard to predict which ones will matter, and you have finite effort. (See Larman pp 427-434 for a discussion).
  • 4. GRASP: Polymorphism • Use inheritance to improve cohesion, reduce coupling, and protect against variation. • NB: in the OO community polymorphism == inheritance, in academic CS, inheritance is just one form of polymorphism. • Inheritance used properly is very powerful – dynamic binding is better than if-statements, but: • observation: inexperienced designers (and also some who should know better) overuse inheritance. • Important question: when is it appropriate to use inheritance?
  • 5. Inheritance rules Only use inheritance if: 3. There is a clear is-a-kind of relationship between the superclass and its subclasses. 4. Each of the subclasses is different in some non- trivial way from the others and from the superclass 5. Every object of the superclass type belongs to exactly one subclass. NB: Inheritance is not primarily about code reuse – that can be done in other ways.
  • 6. Case study - reminder You have been hired to develop an integrated information system for the Irwell online store. They originated as a book store but now sell other forms of media such as CDs and DVDs. They also intend to branch out into selling electronic devices such as laptops and PDAs although they don’t intend to become a general store. Initially you will develop an inventory system: an inventory lists the products a company has in stock, along with information such as prices.
  • 8. Good design? • All three conditions are met • New kinds of product require new subclasses, ok provided that “they don’t intend to become a general store.” • But what if this changes? Can’t keep changing the code each time a different product appears. • Option 1: use inheritance to represent broad categories of products. • Option 2: represent product categories as a single class.
  • 9. Option 1 Allows us to change the category of a product (e.g. if its VAT classification changes) but still requires extra classes for extra categories.
  • 10. Option 2 • The most flexible option, but may be hard to determine which properties go with which categories.
  • 11. Classes vs. instances • Sometimes it can be hard to figure out which things should be represented as separate classes, and which should be just instances of a more general class. (As above) • Sometimes it’s easy: public class Footballer public class WayneRooney extends Footballer Hopefully you’d never dream of doing this!
  • 12. A natural example? • We could represent players in a football management game like this…
  • 13. Inheritance and roles • What if a player plays multiple positions? • We’ve broken rule 3 – more than one software object may represent the same physical object • Results in redundancy and potential inconsistency of data – e.g. if a player changes club we need to change this information several times. • Hard to change the set of roles (positions) once the data is in place.
  • 15. Advantages • Information about a footballer, e.g. club, is held in just one object • Roles (positions) can change over time • New positions can easily be added, and made more fine-grain (e.g. wing-back). • Code using the Footballer class is decoupled from how position is represented – so easy to switch between the two alternatives. • Generally better to start with the simple design, and introduce inheritance if needed later.
  • 16. Indirect object creation Creating objects: new ConcreteClassName(actual parameters) is the weak link in encapsulation. We have to provide an actual concrete class name, and actual parameters. Often best to create objects indirectly, e.g. we use getGraphics() to get an instance of the (abstract) java.awt.Graphics class.
  • 17. Factories • A factory is a class whose sole purpose is to create objects of other classes • Used when object creation is complex, or dependent on external factors (e.g. platform dependencies) • Example: a FontFactory which gets the best approximation to a font with a particular description on a particular platform. • The objects to be created can be specified in many ways, e.g. via a config file. • Factories can implement caching and other object- management functions.
  • 18. Factories and GRASP • Factories are Pure Fabrications • They are also information experts, they contain, or have access to, the information needed to create objects. • They decouple object creation from the code that uses those objects… • … thereby improving the cohesion of both. Like any other technique, factories are not always applicable – often creating objects directly is fine.
  • 19. Assorted design tips • Do everything in exactly one place • Keep classes and methods small. • Start with the simplest design which can possibly work – you can always embellish it afterwards • Assume that the requirements will change during (and quite possibly as a result of) design • Refactor early and often • Inspect and think about designs of experienced designers • Learn about design patterns (see Larman and COMP33411)
  • 20. Things to think about • “Inheritance is not primarily about code reuse – that can be done in other ways.” What other ways? • Think of half a dozen possible uses of inheritance, and go through them to decide whether they are good ones or not. • In Java (unlike e.g. C++) you can only inherit implementation from one class. Can you think of an example where you would really want to extend more than one class? • Apart from the FontFactory example given, what other examples can you think of where a Factory could be useful?

Notes de l'éditeur

  1. Title slide