COMP 2710 Software Construction
Object-Oriented Development
Dr. Xiao Qin
Auburn University
http://www.eng.auburn.edu/~xqin
xqin@auburn.edu
Some slides are adapted from notes by Robert C. Martin
1-1
Object Oriented Programming
1-2
First Version
All designs start well
The program is an overnight success!
How could it be more simple, elegant, and maintainable?
ReadKeyboard
Copy
WritePRinter
void copy(void)
{
int ch;
while( (ch=ReadKeyboard()) != EOF)
WritePrinter(ch);
}
1-3
Second Version
• We sometimes want to read from paper
tape reader.
• We could put a parameter in the call, but
we have hundreds of users already!
• No big deal, this is just an exception… we
can make it work.
Oh, no! Nobody said the requirements might change!
Second Version Design
ReadKeyboard
Copy
WritePrinter
ReadTape
bool GtapeReader = false; // remember to clear
void copy(void)
{
int ch;
while( (ch=GtapeReader ? ReadTape() : ReadKeyboard()) != EOF)
WritePrinter(ch);
}
1-5
Third Version
How unexpected! Requirements changed again!
bool GtapeReader = false;
Bool GtapePunch = false;
// remember to clear
void copy(void)
{
int ch;
while( (ch=GtapeReader ? ReadTape() : ReadKeyboard()) != EOF)
GtapePunch ? WritePunch(ch) : WritePrinter(ch);
}
It seems that sometimes we need to write to a paper tape punch.
We’ve had this problem before, and just added a flag. Looks like it
should work again.
1-6
Example of a Good Design
Why it is a good design?
First and only version.
void Copy()
{
int c;
while( (c=getchar()) != EOF)
putchar(c);
}
But wait! Aren’t we supposed to be learning
OO design? This isn’t OO is it?
1-7
…is it?
• FILE is an abstraction
– It represents some kind of byte
stream
– It has many variations
• It has methods
– Read, Write, getchar, putchar, etc
– The methods are *dynamically*
bound
It is a small program based on abstractions!
FILE is a class, just implemented differently.
1-8
Rephrased in OO
Copy
Reader Writer
KeyboardReader PrinterWriter
ÂŤinterfaceÂť ÂŤinterfaceÂť
class Reader {
public:
char read();
};
class Writer {
public:
void write(char c);
}
class Copy
{
Copy(Reader r, Writer w)
{
itsReader = r;
itsWriter = w;
}
public:
void copy()
{
int c;
while( (c==itsReader.read()) != EOF )
itsWriter.write(c);
}
private:
Reader itsReader;
Writer itsWriter;
}; 1-9
Class Design Principles
• SRP: The Single Responsibility Principle
• OCP: The Open/Closed Principle
• LSP: The Liskov Substitution Principle
• ISP: The Interface Segregation Principle
• DIP: The Dependency Inversion Principle
Reference: Agile Software Development: Principles, Patterns, and Practices.
Robert C. Martin, Prentice Hall, 2002.
1-10
The Single Responsibility Principle
• A class should have one, and only one, reason
to change.
+ CalcPay
+ ReportHours
+ WriteEmployee
Employee
Payroll
1-11
The Single Responsibility
Principle. (SRP)
+ CalcPay
Employee
Payroll
Report
Writer
Employee
Repository
1-12
Open/Closed Principle
• A principle which states that we should add
new functionality by adding new code, not by
editing old code.
• Defines a lot of the value of OO programming
• Abstraction is the key
“Modules should be open for extension, but closed for modification”
-Bertrand Meyer
1-13
Abstraction is Key
• Client/Server
relationships are “open”
• Changes to servers cause
changes to clients
• Abstract servers “close”
clients to changes in
implementation.
Abstraction is the most important word in OOD
Client Server
Client
Abstract
Server
Concrete
Server
1-14
The Shape Example
• Procedural (not closed)
implementation
• OO (closed) implementation
1-15
Procedural (open) version
enum ShapeType {circle, square};
struct Shape
{enum ShapeType itsType;};
struct Circle
{
enum ShapeType itsType;
double itsRadius;
Point itsCenter;
};
void DrawCircle(struct Circle*)
struct Square
{
enum ShapeType itsType;
double itsSide;
Point itsTopLeft;
};
void DrawSquare(struct Square*)
#include <Shape.h>
#include <Circle.h>
#include <Square.h>
typedef struct Shape* ShapePtr;
void
DrawAllShapes(ShapePtr list[], int n)
{
int i;
for( i=0; i< n, i++ )
{
ShapePtr s = list[i];
switch ( s->itsType )
{
case square:
DrawSquare((struct Square*)s);
break;
case circle:
DrawCircle((struct Circle*)s);
break;
}
}
}
Shape.h
Circle.h
Square.h
DrawAllShapes.c
1-16
What is wrong with the code?
• DrawAllShapes is not closed.
– Switch/case tend to recur in diverse places.
– If we add a shape, we add to the switch/case
– All switch/case statements must be found and
editd.
– Switch/Case statements are seldom this tidy
– When we add to the enum, we must rebuild
everything
• The software is both rigid and brittle
It can be demonstrated to work. Isn’t that the important thing?
1-17
A Closed Implementation
Class Shape
{
public:
virtual void Draw() const =0;
};
#include <Shape.h>
void
DrawAllShapes(Shape* list[],int n)
{
for(int i=0; i< n; i++)
list[i]->draw();
}
Shape.h
DrawAllShapes.cpp
Circle.h
Square.h
Class Square: public Shape
{
public:
virtual void Draw() const;
};
Class Circle: public Shape
{
public:
virtual void Draw() const;
};
1-18
Open/Closed Review
• What does the open/closed principle say?
• What does that mean practically?
• How can it be achieved?
• What is strategic closure?
– How can this be achieved in design?
– What if you can’t close completely?
1-21

More Related Content

PPTX
Return of c++
PDF
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
PDF
Survey of Program Transformation Technologies
PPT
Manage software dependencies with ioc and aop
PPTX
Introduction Of C++
PDF
Functions And Header Files In C++ | Bjarne stroustrup
PDF
Open source report writing tools for IBM i Vienna 2012
PPT
490450755-Chapter-2.ppt
Return of c++
[Td 2015] what is new in visual c++ 2015 and future directions(ulzii luvsanba...
Survey of Program Transformation Technologies
Manage software dependencies with ioc and aop
Introduction Of C++
Functions And Header Files In C++ | Bjarne stroustrup
Open source report writing tools for IBM i Vienna 2012
490450755-Chapter-2.ppt

Similar to Lec15a1-Object-Oriented Development.ppt (20)

PPT
490450755-Chapter-2.ppt
PPT
Chapter-2 edited on Programming in Can refer this ppt
PPTX
Fundamental programming Nota Topic 2.pptx
PDF
Software Engineering
PDF
College1
PPT
270_1_ChapterIntro_Up_To_Functions (1).ppt
PPTX
What's coming to c# (Tel-Aviv, 2018)
PPT
270_1_CIntro_Up_To_Functions.ppt
PPT
Survey of programming language getting started in C
PPT
270_1_CIntro_Up_To_Functions.ppt
PPT
270 1 c_intro_up_to_functions
PPT
designpatterns_blair_upe.ppt
PPTX
Intro in understanding to C programming .pptx
PPTX
Intro in understanding to C programming .pptx
PPTX
Presentation c++
PPT
270_1_CIntro_Up_To_Functions.ppt 0478 computer
PPT
CIntro_Up_To_Functions.ppt;uoooooooooooooooooooo
PPTX
Introduction of c programming unit-ii ppt
PPT
490450755-Chapter-2.ppt
Chapter-2 edited on Programming in Can refer this ppt
Fundamental programming Nota Topic 2.pptx
Software Engineering
College1
270_1_ChapterIntro_Up_To_Functions (1).ppt
What's coming to c# (Tel-Aviv, 2018)
270_1_CIntro_Up_To_Functions.ppt
Survey of programming language getting started in C
270_1_CIntro_Up_To_Functions.ppt
270 1 c_intro_up_to_functions
designpatterns_blair_upe.ppt
Intro in understanding to C programming .pptx
Intro in understanding to C programming .pptx
Presentation c++
270_1_CIntro_Up_To_Functions.ppt 0478 computer
CIntro_Up_To_Functions.ppt;uoooooooooooooooooooo
Introduction of c programming unit-ii ppt
Ad

Recently uploaded (20)

PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
Computer Software - Technology and Livelihood Education
PPTX
Introduction to Windows Operating System
PDF
MCP Security Tutorial - Beginner to Advanced
PPTX
Patient Appointment Booking in Odoo with online payment
PDF
Visual explanation of Dijkstra's Algorithm using Python
PDF
iTop VPN Crack Latest Version Full Key 2025
PDF
AI Guide for Business Growth - Arna Softech
PDF
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
PDF
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
PPTX
Trending Python Topics for Data Visualization in 2025
PDF
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
PPTX
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
PDF
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
PPTX
Weekly report ppt - harsh dattuprasad patel.pptx
PDF
Website Design Services for Small Businesses.pdf
PPTX
Cybersecurity: Protecting the Digital World
PDF
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
PDF
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
PPTX
Oracle Fusion HCM Cloud Demo for Beginners
Computer Software and OS of computer science of grade 11.pptx
Computer Software - Technology and Livelihood Education
Introduction to Windows Operating System
MCP Security Tutorial - Beginner to Advanced
Patient Appointment Booking in Odoo with online payment
Visual explanation of Dijkstra's Algorithm using Python
iTop VPN Crack Latest Version Full Key 2025
AI Guide for Business Growth - Arna Softech
How AI/LLM recommend to you ? GDG meetup 16 Aug by Fariman Guliev
AI-Powered Threat Modeling: The Future of Cybersecurity by Arun Kumar Elengov...
Trending Python Topics for Data Visualization in 2025
Product Update: Alluxio AI 3.7 Now with Sub-Millisecond Latency
Log360_SIEM_Solutions Overview PPT_Feb 2020.pptx
AI/ML Infra Meetup | LLM Agents and Implementation Challenges
Weekly report ppt - harsh dattuprasad patel.pptx
Website Design Services for Small Businesses.pdf
Cybersecurity: Protecting the Digital World
How to Make Money in the Metaverse_ Top Strategies for Beginners.pdf
The Dynamic Duo Transforming Financial Accounting Systems Through Modern Expe...
Oracle Fusion HCM Cloud Demo for Beginners
Ad

Lec15a1-Object-Oriented Development.ppt

  • 1. COMP 2710 Software Construction Object-Oriented Development Dr. Xiao Qin Auburn University http://www.eng.auburn.edu/~xqin xqin@auburn.edu Some slides are adapted from notes by Robert C. Martin 1-1
  • 3. First Version All designs start well The program is an overnight success! How could it be more simple, elegant, and maintainable? ReadKeyboard Copy WritePRinter void copy(void) { int ch; while( (ch=ReadKeyboard()) != EOF) WritePrinter(ch); } 1-3
  • 4. Second Version • We sometimes want to read from paper tape reader. • We could put a parameter in the call, but we have hundreds of users already! • No big deal, this is just an exception… we can make it work. Oh, no! Nobody said the requirements might change!
  • 5. Second Version Design ReadKeyboard Copy WritePrinter ReadTape bool GtapeReader = false; // remember to clear void copy(void) { int ch; while( (ch=GtapeReader ? ReadTape() : ReadKeyboard()) != EOF) WritePrinter(ch); } 1-5
  • 6. Third Version How unexpected! Requirements changed again! bool GtapeReader = false; Bool GtapePunch = false; // remember to clear void copy(void) { int ch; while( (ch=GtapeReader ? ReadTape() : ReadKeyboard()) != EOF) GtapePunch ? WritePunch(ch) : WritePrinter(ch); } It seems that sometimes we need to write to a paper tape punch. We’ve had this problem before, and just added a flag. Looks like it should work again. 1-6
  • 7. Example of a Good Design Why it is a good design? First and only version. void Copy() { int c; while( (c=getchar()) != EOF) putchar(c); } But wait! Aren’t we supposed to be learning OO design? This isn’t OO is it? 1-7
  • 8. …is it? • FILE is an abstraction – It represents some kind of byte stream – It has many variations • It has methods – Read, Write, getchar, putchar, etc – The methods are *dynamically* bound It is a small program based on abstractions! FILE is a class, just implemented differently. 1-8
  • 9. Rephrased in OO Copy Reader Writer KeyboardReader PrinterWriter ÂŤinterfaceÂť ÂŤinterfaceÂť class Reader { public: char read(); }; class Writer { public: void write(char c); } class Copy { Copy(Reader r, Writer w) { itsReader = r; itsWriter = w; } public: void copy() { int c; while( (c==itsReader.read()) != EOF ) itsWriter.write(c); } private: Reader itsReader; Writer itsWriter; }; 1-9
  • 10. Class Design Principles • SRP: The Single Responsibility Principle • OCP: The Open/Closed Principle • LSP: The Liskov Substitution Principle • ISP: The Interface Segregation Principle • DIP: The Dependency Inversion Principle Reference: Agile Software Development: Principles, Patterns, and Practices. Robert C. Martin, Prentice Hall, 2002. 1-10
  • 11. The Single Responsibility Principle • A class should have one, and only one, reason to change. + CalcPay + ReportHours + WriteEmployee Employee Payroll 1-11
  • 12. The Single Responsibility Principle. (SRP) + CalcPay Employee Payroll Report Writer Employee Repository 1-12
  • 13. Open/Closed Principle • A principle which states that we should add new functionality by adding new code, not by editing old code. • Defines a lot of the value of OO programming • Abstraction is the key “Modules should be open for extension, but closed for modification” -Bertrand Meyer 1-13
  • 14. Abstraction is Key • Client/Server relationships are “open” • Changes to servers cause changes to clients • Abstract servers “close” clients to changes in implementation. Abstraction is the most important word in OOD Client Server Client Abstract Server Concrete Server 1-14
  • 15. The Shape Example • Procedural (not closed) implementation • OO (closed) implementation 1-15
  • 16. Procedural (open) version enum ShapeType {circle, square}; struct Shape {enum ShapeType itsType;}; struct Circle { enum ShapeType itsType; double itsRadius; Point itsCenter; }; void DrawCircle(struct Circle*) struct Square { enum ShapeType itsType; double itsSide; Point itsTopLeft; }; void DrawSquare(struct Square*) #include <Shape.h> #include <Circle.h> #include <Square.h> typedef struct Shape* ShapePtr; void DrawAllShapes(ShapePtr list[], int n) { int i; for( i=0; i< n, i++ ) { ShapePtr s = list[i]; switch ( s->itsType ) { case square: DrawSquare((struct Square*)s); break; case circle: DrawCircle((struct Circle*)s); break; } } } Shape.h Circle.h Square.h DrawAllShapes.c 1-16
  • 17. What is wrong with the code? • DrawAllShapes is not closed. – Switch/case tend to recur in diverse places. – If we add a shape, we add to the switch/case – All switch/case statements must be found and editd. – Switch/Case statements are seldom this tidy – When we add to the enum, we must rebuild everything • The software is both rigid and brittle It can be demonstrated to work. Isn’t that the important thing? 1-17
  • 18. A Closed Implementation Class Shape { public: virtual void Draw() const =0; }; #include <Shape.h> void DrawAllShapes(Shape* list[],int n) { for(int i=0; i< n; i++) list[i]->draw(); } Shape.h DrawAllShapes.cpp Circle.h Square.h Class Square: public Shape { public: virtual void Draw() const; }; Class Circle: public Shape { public: virtual void Draw() const; }; 1-18
  • 19. Open/Closed Review • What does the open/closed principle say? • What does that mean practically? • How can it be achieved? • What is strategic closure? – How can this be achieved in design? – What if you can’t close completely? 1-21