SlideShare une entreprise Scribd logo
1  sur  23
Object Design
Michael Heron
Introduction
• In this lecture we are going to round off our discussion about
object orientation with some points about object design.
• Designing good object oriented programs is not simple.
• Object orientation is not a particular focus of the module.
• Important however you recognise it when you see it.
Class Design
• Several important principles when designing classes.
• Classes should have one responsibility only.
• This leads to high cohesion.
• Classes should have the minimal number of connections to other
classes.
• Not zero connections, that doesn’t work.
• Classes should expose only those parts of their functionality that
are part of the interface.
Visibility
• Variables should always be private.
• There is never a good reason for them not to be.
• Other methods should be:
• Private if they are entirely internal functions.
• Public if they are to be used by anyone.
• Protected if you can’t get away with making functions private.
• Protected is a special level of visibility dealing with inheritance.
Object Orientation
• Object oriented programs are not inherently scalable.
• If you are not careful, they become difficult to manipulate beyond
about 10/15 classes.
• Important to design object oriented programs correctly.
• Need a way to measure the quality of an OO design.
• Two metrics used for this.
Cohesion
• Cohesion is the degree to which a class adheres to a single
responsibility.
• Very important for good class design.
• Good object oriented programs have highly cohesive classes.
• They do one thing, and one thing only.
• If necessary, classes should be refactored to ensure this.
• Modify the internals of the class, breaking it out into multiple
classes where appropriate.
Coupling
• Coupling defines the degree to which classes are connected
together.
• Lower coupling is best.
• Trade off exists here – lowering coupling involves lowering the
cohesion of classes.
• Increasing cohesion means increasing the coupling of classes.
• Must decide on a balance for each individual program.
Object Orientation and
Inheritance
• One of the most powerful tools available in object orientation
is that of inheritance.
• It allows us to bundle functionality together and share it between
multiple objects.
• Whenever you have objects that have similar
roles/responsibilities, consider making use of inheritance.
• Many benefits come from this.
Inheritance
• Object oriented programs that make use of inheritance are, in
general:
• Easier to debug
• Easier to maintain
• Easier to reuse
• Quicker to write
• Use inheritance to express ‘is-a’ relationships in your code.
• Even if there is no immediate need, it’s worth considering for the
sake of ‘future proofing’.
Composition
• It’s quite common for objects to make use of other objects.
• In fact, it’s a big part of object orientation.
• This is called composition.
• It’s sometimes difficult to express when things should be an
inheritance and when it should be expressed as composition.
• Composition models a has-a relationship.
Deciding On Objects
• Working out which objects you want to incorporate into a
program can be a complicated affair.
• There’s no hard and fast rule about what an object actually is.
• Worthwhile to make use of some kind of heuristic system.
• Such as ‘natural language analysis’.
Natural Language Analysis
• Take a requirements brief.
• Such as an assessment exercise.
• Identify all the nouns
• These are your candidate classes.
• Identify all the verbs
• These imply methods
• Identify all the adjectives
• These suggest the existence of attributes.
• Make special note of relationships expressed in the text.
• These model inheritance and composition relationships.
Natural Language Analysis
• The idea is not to derive an exact model of the program.
• Only to give some inspiration.
• Much thought is needed to extract useful information.
• Written language, particularly if it scans well, is not very precise.
• Written language contains a lot of repetition and synonyms.
• The information you extract is candidate information only.
Designing an Object
• Designing an object works much in the same way as incremental
development.
• Start small, with a minimal representation.
• Add the methods that let you act upon that representation.
• Expand on this functionality as needed.
• Object orientation introduces new complications when it comes to
writing programs.
• Especially important to start small.
Designing an Object
• The architecture of an object oriented program is made up of
several things.
• The classes that exist in the program.
• The interface those classes present to others.
• The communication between objects
• Modeled as method calls
• Important to think of the context of objects.
• What are they supposed to do
• What objects should they be calling upon?
Example Process
• Given a scenario:
• The People Who Hate People bank are looking for someone to write
a software system for them. The system should permit the modeling
of several accounts and the owners of those accounts, allowing them
to deposit, withdraw, and view their balances. Accounts have
numbers that identify them, and owners have their name and
address stored in the system. The system should provide an interface
that allows users to interact with the application.
• How do we arrive at the classes?
Example Process
• First we use natural language analysis to identify all of the
different parts of the system.
• This gives us our candidate list.
• Then we analyse this list to derive the list of classes we are
going to write.
• This won’t be all of them.
• Some will be synonyms.
• Some will be better handled as attributes of another class.
• Some will be irrelevant.
Example Process
• Having gotten a list of viable candidates, consider their
relationship.
• A UML diagram is a good way to do this.
• Consider what data they must represent.
• Consider what interface they must present.
• Keep it simple to begin with.
• It’s easy to expand later as needed.
• It’s not so easy to scale back later.
Example Process
• Having gotten the plan down, create skeletons of the classes.
• A header and a CPP file for each class.
• Create the definition, don’t worry too much about the
implementation to begin with.
• Design them as if they were black box systems.
• Compile early and often.
• Don’t wait until they do something interesting before you
compile them.
Placeholder Methods
• An important part of software design is abstraction.
• Not worrying about the details until you have sorted out the big picture.
• One good way in which this is done is the writing of placeholder
methods.
• You don’t need to actually make methods do something.
• You just have to have them there in the right structure.
• Make abundant use of these in your code.
• Even if all they do is print to the screen ‘This method was called’.
Compile Early, Compile Often
• Object orientation in particular opens up a new family of
problems with compiling code.
• Mostly in terms of what are known as linker errors.
• These are errors in what you have told C++ the class should be
(in your .h file) as opposed to what it actually is (in your .cpp
file).
• Compiling early will indentify these problems before they are
too time-consuming to fix.
Compile Early, Compile Often
• Focus on getting only the simplest core functionality in place.
• Represent data
• Provide accessors for that data
• Once you have that simple representation, build on it.
• Add in methods that allow you to manipulate the data
representation appropriately.
• Compile and test after every new piece of functionality.
Summary
• Object design is tricky
• As is working out what objects you want to represent in a
program.
• There are several guidelines that can help.
• But it’s mostly going to come from practise.
• Object orientation introduces new complexities with coding.
• It’s important to manage these with incremental development.

Contenu connexe

Similaire à CPP16 - Object Design

Similaire à CPP16 - Object Design (20)

2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program2CPP05 - Modelling an Object Oriented Program
2CPP05 - Modelling an Object Oriented Program
 
CPP14 - Encapsulation
CPP14 - EncapsulationCPP14 - Encapsulation
CPP14 - Encapsulation
 
Software development fundamentals
Software development fundamentalsSoftware development fundamentals
Software development fundamentals
 
CPP19 - Revision
CPP19 - RevisionCPP19 - Revision
CPP19 - Revision
 
[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP[OOP - Lec 01] Introduction to OOP
[OOP - Lec 01] Introduction to OOP
 
2CPP19 - Summation
2CPP19 - Summation2CPP19 - Summation
2CPP19 - Summation
 
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
 
[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP[OOP - Lec 02] Why do we need OOP
[OOP - Lec 02] Why do we need OOP
 
OOM Unit I - III.pdf
OOM Unit I - III.pdfOOM Unit I - III.pdf
OOM Unit I - III.pdf
 
Clean code presentation
Clean code presentationClean code presentation
Clean code presentation
 
Agile Software Development
Agile Software DevelopmentAgile Software Development
Agile Software Development
 
CPP02 - The Structure of a Program
CPP02 - The Structure of a ProgramCPP02 - The Structure of a Program
CPP02 - The Structure of a Program
 
Designpattern
DesignpatternDesignpattern
Designpattern
 
Introduction
IntroductionIntroduction
Introduction
 
CEN6016-Chapter1.ppt
CEN6016-Chapter1.pptCEN6016-Chapter1.ppt
CEN6016-Chapter1.ppt
 
CEN6016-Chapter1.ppt
CEN6016-Chapter1.pptCEN6016-Chapter1.ppt
CEN6016-Chapter1.ppt
 
Problem solving
Problem solvingProblem solving
Problem solving
 
Chapter 1
Chapter 1Chapter 1
Chapter 1
 
Oop.pptx
Oop.pptxOop.pptx
Oop.pptx
 

Plus de Michael Heron

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMichael Heron
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconductMichael Heron
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkMichael Heron
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility SupportMichael Heron
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and AutershipMichael Heron
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interactionMichael Heron
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityMichael Heron
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - TexturesMichael Heron
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)Michael Heron
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)Michael Heron
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationMichael Heron
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsMichael Heron
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsMichael Heron
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationMichael Heron
 

Plus de Michael Heron (20)

Meeple centred design - Board Game Accessibility
Meeple centred design - Board Game AccessibilityMeeple centred design - Board Game Accessibility
Meeple centred design - Board Game Accessibility
 
Musings on misconduct
Musings on misconductMusings on misconduct
Musings on misconduct
 
Accessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS FrameworkAccessibility Support with the ACCESS Framework
Accessibility Support with the ACCESS Framework
 
ACCESS: A Technical Framework for Adaptive Accessibility Support
ACCESS:  A Technical Framework for Adaptive Accessibility SupportACCESS:  A Technical Framework for Adaptive Accessibility Support
ACCESS: A Technical Framework for Adaptive Accessibility Support
 
Authorship and Autership
Authorship and AutershipAuthorship and Autership
Authorship and Autership
 
Text parser based interaction
Text parser based interactionText parser based interaction
Text parser based interaction
 
SAD04 - Inheritance
SAD04 - InheritanceSAD04 - Inheritance
SAD04 - Inheritance
 
GRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and RadiosityGRPHICS08 - Raytracing and Radiosity
GRPHICS08 - Raytracing and Radiosity
 
GRPHICS07 - Textures
GRPHICS07 - TexturesGRPHICS07 - Textures
GRPHICS07 - Textures
 
GRPHICS06 - Shading
GRPHICS06 - ShadingGRPHICS06 - Shading
GRPHICS06 - Shading
 
GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)GRPHICS05 - Rendering (2)
GRPHICS05 - Rendering (2)
 
GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)GRPHICS04 - Rendering (1)
GRPHICS04 - Rendering (1)
 
GRPHICS03 - Graphical Representation
GRPHICS03 - Graphical RepresentationGRPHICS03 - Graphical Representation
GRPHICS03 - Graphical Representation
 
GRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D GraphicsGRPHICS02 - Creating 3D Graphics
GRPHICS02 - Creating 3D Graphics
 
GRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D GraphicsGRPHICS01 - Introduction to 3D Graphics
GRPHICS01 - Introduction to 3D Graphics
 
GRPHICS09 - Art Appreciation
GRPHICS09 - Art AppreciationGRPHICS09 - Art Appreciation
GRPHICS09 - Art Appreciation
 
2CPP18 - Modifiers
2CPP18 - Modifiers2CPP18 - Modifiers
2CPP18 - Modifiers
 
2CPP17 - File IO
2CPP17 - File IO2CPP17 - File IO
2CPP17 - File IO
 
2CPP16 - STL
2CPP16 - STL2CPP16 - STL
2CPP16 - STL
 
2CPP15 - Templates
2CPP15 - Templates2CPP15 - Templates
2CPP15 - Templates
 

Dernier

introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfVishalKumarJha10
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesVictorSzoltysek
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Steffen Staab
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrainmasabamasaba
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfonteinmasabamasaba
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareJim McKeeth
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplatePresentation.STUDIO
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...masabamasaba
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...SelfMade bd
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsJhone kinadey
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️Delhi Call girls
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisamasabamasaba
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...Shane Coughlan
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdfPearlKirahMaeRagusta1
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durbanmasabamasaba
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfayushiqss
 

Dernier (20)

Microsoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdfMicrosoft AI Transformation Partner Playbook.pdf
Microsoft AI Transformation Partner Playbook.pdf
 
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdfintroduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
introduction-to-automotive Andoid os-csimmonds-ndctechtown-2021.pdf
 
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM TechniquesAI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
AI Mastery 201: Elevating Your Workflow with Advanced LLM Techniques
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
%in Bahrain+277-882-255-28 abortion pills for sale in Bahrain
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Announcing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK SoftwareAnnouncing Codolex 2.0 from GDK Software
Announcing Codolex 2.0 from GDK Software
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
Crypto Cloud Review - How To Earn Up To $500 Per DAY Of Bitcoin 100% On AutoP...
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
OpenChain - The Ramifications of ISO/IEC 5230 and ISO/IEC 18974 for Legal Pro...
 
Define the academic and professional writing..pdf
Define the academic and professional writing..pdfDefine the academic and professional writing..pdf
Define the academic and professional writing..pdf
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdfThe Top App Development Trends Shaping the Industry in 2024-25 .pdf
The Top App Development Trends Shaping the Industry in 2024-25 .pdf
 

CPP16 - Object Design

  • 2. Introduction • In this lecture we are going to round off our discussion about object orientation with some points about object design. • Designing good object oriented programs is not simple. • Object orientation is not a particular focus of the module. • Important however you recognise it when you see it.
  • 3. Class Design • Several important principles when designing classes. • Classes should have one responsibility only. • This leads to high cohesion. • Classes should have the minimal number of connections to other classes. • Not zero connections, that doesn’t work. • Classes should expose only those parts of their functionality that are part of the interface.
  • 4. Visibility • Variables should always be private. • There is never a good reason for them not to be. • Other methods should be: • Private if they are entirely internal functions. • Public if they are to be used by anyone. • Protected if you can’t get away with making functions private. • Protected is a special level of visibility dealing with inheritance.
  • 5. Object Orientation • Object oriented programs are not inherently scalable. • If you are not careful, they become difficult to manipulate beyond about 10/15 classes. • Important to design object oriented programs correctly. • Need a way to measure the quality of an OO design. • Two metrics used for this.
  • 6. Cohesion • Cohesion is the degree to which a class adheres to a single responsibility. • Very important for good class design. • Good object oriented programs have highly cohesive classes. • They do one thing, and one thing only. • If necessary, classes should be refactored to ensure this. • Modify the internals of the class, breaking it out into multiple classes where appropriate.
  • 7. Coupling • Coupling defines the degree to which classes are connected together. • Lower coupling is best. • Trade off exists here – lowering coupling involves lowering the cohesion of classes. • Increasing cohesion means increasing the coupling of classes. • Must decide on a balance for each individual program.
  • 8. Object Orientation and Inheritance • One of the most powerful tools available in object orientation is that of inheritance. • It allows us to bundle functionality together and share it between multiple objects. • Whenever you have objects that have similar roles/responsibilities, consider making use of inheritance. • Many benefits come from this.
  • 9. Inheritance • Object oriented programs that make use of inheritance are, in general: • Easier to debug • Easier to maintain • Easier to reuse • Quicker to write • Use inheritance to express ‘is-a’ relationships in your code. • Even if there is no immediate need, it’s worth considering for the sake of ‘future proofing’.
  • 10. Composition • It’s quite common for objects to make use of other objects. • In fact, it’s a big part of object orientation. • This is called composition. • It’s sometimes difficult to express when things should be an inheritance and when it should be expressed as composition. • Composition models a has-a relationship.
  • 11. Deciding On Objects • Working out which objects you want to incorporate into a program can be a complicated affair. • There’s no hard and fast rule about what an object actually is. • Worthwhile to make use of some kind of heuristic system. • Such as ‘natural language analysis’.
  • 12. Natural Language Analysis • Take a requirements brief. • Such as an assessment exercise. • Identify all the nouns • These are your candidate classes. • Identify all the verbs • These imply methods • Identify all the adjectives • These suggest the existence of attributes. • Make special note of relationships expressed in the text. • These model inheritance and composition relationships.
  • 13. Natural Language Analysis • The idea is not to derive an exact model of the program. • Only to give some inspiration. • Much thought is needed to extract useful information. • Written language, particularly if it scans well, is not very precise. • Written language contains a lot of repetition and synonyms. • The information you extract is candidate information only.
  • 14. Designing an Object • Designing an object works much in the same way as incremental development. • Start small, with a minimal representation. • Add the methods that let you act upon that representation. • Expand on this functionality as needed. • Object orientation introduces new complications when it comes to writing programs. • Especially important to start small.
  • 15. Designing an Object • The architecture of an object oriented program is made up of several things. • The classes that exist in the program. • The interface those classes present to others. • The communication between objects • Modeled as method calls • Important to think of the context of objects. • What are they supposed to do • What objects should they be calling upon?
  • 16. Example Process • Given a scenario: • The People Who Hate People bank are looking for someone to write a software system for them. The system should permit the modeling of several accounts and the owners of those accounts, allowing them to deposit, withdraw, and view their balances. Accounts have numbers that identify them, and owners have their name and address stored in the system. The system should provide an interface that allows users to interact with the application. • How do we arrive at the classes?
  • 17. Example Process • First we use natural language analysis to identify all of the different parts of the system. • This gives us our candidate list. • Then we analyse this list to derive the list of classes we are going to write. • This won’t be all of them. • Some will be synonyms. • Some will be better handled as attributes of another class. • Some will be irrelevant.
  • 18. Example Process • Having gotten a list of viable candidates, consider their relationship. • A UML diagram is a good way to do this. • Consider what data they must represent. • Consider what interface they must present. • Keep it simple to begin with. • It’s easy to expand later as needed. • It’s not so easy to scale back later.
  • 19. Example Process • Having gotten the plan down, create skeletons of the classes. • A header and a CPP file for each class. • Create the definition, don’t worry too much about the implementation to begin with. • Design them as if they were black box systems. • Compile early and often. • Don’t wait until they do something interesting before you compile them.
  • 20. Placeholder Methods • An important part of software design is abstraction. • Not worrying about the details until you have sorted out the big picture. • One good way in which this is done is the writing of placeholder methods. • You don’t need to actually make methods do something. • You just have to have them there in the right structure. • Make abundant use of these in your code. • Even if all they do is print to the screen ‘This method was called’.
  • 21. Compile Early, Compile Often • Object orientation in particular opens up a new family of problems with compiling code. • Mostly in terms of what are known as linker errors. • These are errors in what you have told C++ the class should be (in your .h file) as opposed to what it actually is (in your .cpp file). • Compiling early will indentify these problems before they are too time-consuming to fix.
  • 22. Compile Early, Compile Often • Focus on getting only the simplest core functionality in place. • Represent data • Provide accessors for that data • Once you have that simple representation, build on it. • Add in methods that allow you to manipulate the data representation appropriately. • Compile and test after every new piece of functionality.
  • 23. Summary • Object design is tricky • As is working out what objects you want to represent in a program. • There are several guidelines that can help. • But it’s mostly going to come from practise. • Object orientation introduces new complexities with coding. • It’s important to manage these with incremental development.