SlideShare una empresa de Scribd logo
1 de 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?

Más contenido relacionado

Similar a Week5 inhertiance

Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane1
 
Chapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptChapter 4_Introduction to Patterns.ppt
Chapter 4_Introduction to Patterns.pptRushikeshChikane2
 
Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)Fundamentals of OOP (Object Oriented Programming)
Fundamentals of OOP (Object Oriented Programming)MD Sulaiman
 
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 PrinciplesDr. Syed Hassan Amin
 
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 InheritanceBlue Elephant Consulting
 
SKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSKILLWISE - OOPS CONCEPT
SKILLWISE - OOPS CONCEPTSkillwise Group
 
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
 
UNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxUNIT IV DESIGN PATTERNS.pptx
UNIT IV DESIGN PATTERNS.pptxanguraju1
 
Design Patterns .Net
Design Patterns .NetDesign Patterns .Net
Design Patterns .NetHariom Shah
 
Design patterns
Design patternsDesign patterns
Design patternsAlok Guha
 
Reading Notes : the practice of programming
Reading Notes : the practice of programmingReading Notes : the practice of programming
Reading Notes : the practice of programmingJuggernaut Liu
 
Intro to oop.pptx
Intro to oop.pptxIntro to oop.pptx
Intro to oop.pptxUmerUmer25
 

Similar a 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
 

Último

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
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...Miguel Araújo
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
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 2024The Digital Insurer
 
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 Processorsdebabhi2
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slidevu2urc
 
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?Antenna Manufacturer Coco
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationSafe Software
 
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 organizationRadu Cotescu
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUK Journal
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
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 Servicegiselly40
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 

Último (20)

EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
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...
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 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
 
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
 
Histor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slideHistor y of HAM Radio presentation slide
Histor y of HAM Radio presentation slide
 
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?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
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
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 

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?

Notas del editor

  1. Title slide