CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to...

17
CS 350 – Software Design CS 350 – Software Design The Object Paradigm – Chapter 1 The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes If you were tasked to write code to access a description of shapes that were stored in a database and display them you might do the that were stored in a database and display them you might do the following: following: 1. 1. Locate the list of shapes in the database Locate the list of shapes in the database 2. 2. Open up the list of shapes Open up the list of shapes 3. 3. Sort the list according to some rules Sort the list according to some rules 4. 4. Display the individual shapes on the monitor Display the individual shapes on the monitor Any step can be further broken down. For example Step 4 could be Any step can be further broken down. For example Step 4 could be broken down as follows: broken down as follows: 1. 1. Identify the type of shape Identify the type of shape 2. 2. Get the location of the shape Get the location of the shape 3. 3. Call the function that will display the shape, given the Call the function that will display the shape, given the shape’s location shape’s location This is known as This is known as functional decomposition functional decomposition.

Transcript of CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to...

Page 1: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

If you were tasked to write code to access a description of shapes that were stored If you were tasked to write code to access a description of shapes that were stored in a database and display them you might do the following:in a database and display them you might do the following:

1.1. Locate the list of shapes in the databaseLocate the list of shapes in the database

2.2. Open up the list of shapesOpen up the list of shapes

3.3. Sort the list according to some rulesSort the list according to some rules

4.4. Display the individual shapes on the monitorDisplay the individual shapes on the monitor

Any step can be further broken down. For example Step 4 could be broken down Any step can be further broken down. For example Step 4 could be broken down as follows:as follows:

1.1. Identify the type of shapeIdentify the type of shape

2.2. Get the location of the shapeGet the location of the shape

3.3. Call the function that will display the shape, given the shape’s locationCall the function that will display the shape, given the shape’s location

This is known as This is known as functional decompositionfunctional decomposition..

Page 2: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

One main program is responsible for controlling its subprogramsOne main program is responsible for controlling its subprograms

Is this a good thing?Is this a good thing?

History shows that it is not. History shows that it is not.

The book implies it is far more easier to have some of the subfunctions The book implies it is far more easier to have some of the subfunctions responsible for their own behavior. This is called responsible for their own behavior. This is called delegationdelegation..

I agree with this, however I think the book really means that a program is easier to I agree with this, however I think the book really means that a program is easier to maintain if it is written with delegation.maintain if it is written with delegation.

Many concepts in the book state that something is easier, however I think that Many concepts in the book state that something is easier, however I think that most of the book doesn’t necessarily make the initial coding easier. Instead, it most of the book doesn’t necessarily make the initial coding easier. Instead, it reduces the effort to maintain code in the long run. This should not be trivialized reduces the effort to maintain code in the long run. This should not be trivialized as writing a program that works the first time is easy. Writing a program that as writing a program that works the first time is easy. Writing a program that continues to meet the needs of its users is quite another challenge.continues to meet the needs of its users is quite another challenge.

As the book states, “Many bugs originate with changes to code.”As the book states, “Many bugs originate with changes to code.”

Page 3: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Ask yourself why do programs fail in the “real world.” Ask yourself why do programs fail in the “real world.”

Most of you should have had some co op experience and therefore a taste of the Most of you should have had some co op experience and therefore a taste of the real world. In academics the problem you are solving is usually clearly laid real world. In academics the problem you are solving is usually clearly laid out. Expectations are well defined. This is not the case in the real world. out. Expectations are well defined. This is not the case in the real world.

While a program can fail because:While a program can fail because:

1.1. Technological problemsTechnological problems

2.2. Incompetent ProgrammersIncompetent Programmers

3.3. Poor ManagementPoor Management

4.4. Poor Development ToolsPoor Development Tools

The #1 reason projects fail is poor requirements gatheringThe #1 reason projects fail is poor requirements gathering..

Page 4: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Why are requirements poor?Why are requirements poor?

1.1. Requirements are incompleteRequirements are incomplete

2.2. Requirements are often wrongRequirements are often wrong

3.3. Requirements (and users) are misleadingRequirements (and users) are misleading

4.4. Requirements do not tell the whole storyRequirements do not tell the whole story

This happens for a lot of reasons and they are often classified as the dreaded This happens for a lot of reasons and they are often classified as the dreaded communication problemscommunication problems..

While one tries to document requirements completely and properly, the reality is, While one tries to document requirements completely and properly, the reality is, for many reasons you will not achieve a perfect set of requirements. If you do not for many reasons you will not achieve a perfect set of requirements. If you do not have good requirements, how can programmers possibly be expected to develop have good requirements, how can programmers possibly be expected to develop an application that meets the needs of the end user.an application that meets the needs of the end user.

The reality is, The reality is, requirements always changerequirements always change..

Page 5: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Let’s look again at the problem of displaying shapes. Let’s look again at the problem of displaying shapes.

One might break the problem into modules as follows:One might break the problem into modules as follows:

Function: Function: Display ShapeDisplay Shape

Input: Input: Type of Shape, Description of ShapeType of Shape, Description of Shape

Action: Action: switch (type of shape)switch (type of shape)

case Square: put display function for square herecase Square: put display function for square here

case Circle: put display function for circle herecase Circle: put display function for circle here

The goal is that when a new type of shape is created, say a triangle, we only need The goal is that when a new type of shape is created, say a triangle, we only need to change one module.to change one module.

This is of course the goal, but not the reality.This is of course the goal, but not the reality.

What happens if shapes can’t be stored the same way? How can they be passed What happens if shapes can’t be stored the same way? How can they be passed to the same module?to the same module?

Page 6: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

An application’s maintainability is often measured by two factors: cohesion and An application’s maintainability is often measured by two factors: cohesion and coupling.coupling.

Cohesion refers to how “closely the operations in a routine are related.”Cohesion refers to how “closely the operations in a routine are related.”

Coupling refers to “the strength of a connection between two routines.”Coupling refers to “the strength of a connection between two routines.”

So do we want our code to have So do we want our code to have weakweak or or strong cohesionstrong cohesion??

Do we want our code to have Do we want our code to have tighttight or or looseloose coupling? coupling?

Code should have strong cohesion and loose coupling.Code should have strong cohesion and loose coupling.

Examples:Examples:

A math library of routines to support trigonometry is weak or strong cohesion?A math library of routines to support trigonometry is weak or strong cohesion?

An object calls another objects method with 8 parameter method is tight or loose An object calls another objects method with 8 parameter method is tight or loose coupling?coupling?

Page 7: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

An application’s maintainability is often measured by two factors: cohesion and An application’s maintainability is often measured by two factors: cohesion and coupling.coupling.

Cohesion refers to how “closely the operations in a routine are related.”Cohesion refers to how “closely the operations in a routine are related.”

Coupling refers to “the strength of a connection between two routines.”Coupling refers to “the strength of a connection between two routines.”

So do we want our code to have So do we want our code to have weakweak or or strong cohesionstrong cohesion??

Do we want our code to have Do we want our code to have tighttight or or looseloose coupling? coupling?

Code should have strong cohesion and loose coupling.Code should have strong cohesion and loose coupling.

Examples:Examples:

A math library of routines to support trigonometry is weak or A math library of routines to support trigonometry is weak or strong cohesionstrong cohesion? ? Since all the functions relate to one another.Since all the functions relate to one another.

An object calls another objects method with 8 parameter method is An object calls another objects method with 8 parameter method is tighttight or loose or loose couplingcoupling? Since there are many parameters, if one changes (which is likely with a ? Since there are many parameters, if one changes (which is likely with a large number) then both objects are changed instead of one.large number) then both objects are changed instead of one.

Page 8: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Fixing problems in code takes time. This is an obvious statement, I hope.Fixing problems in code takes time. This is an obvious statement, I hope.

However, not so obvious is what takes the time. Fixing the bug is not difficult.However, not so obvious is what takes the time. Fixing the bug is not difficult.

Finding a bug is.Finding a bug is.

Consider a memory leak. Once you know where the leak is occurring, setting the Consider a memory leak. Once you know where the leak is occurring, setting the pointer or disposing of the memory is easy. However, it may take you pointer or disposing of the memory is easy. However, it may take you

Page 9: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Consider the example of an instructor at a conference. People in your class have Consider the example of an instructor at a conference. People in your class have another class to attend following yours, but don’t know where it is located. You are another class to attend following yours, but don’t know where it is located. You are responsible to make sure everyone knows how to get to the next class.responsible to make sure everyone knows how to get to the next class.

STRUCTURED PROGRAMMING APPROACH:STRUCTURED PROGRAMMING APPROACH:

1.1. Get a list of people in the classGet a list of people in the class

2.2. For each person on this list, do the following:For each person on this list, do the following:1.1. Find the next class he or she is takingFind the next class he or she is taking

2.2. Find the location of that classFind the location of that class

3.3. Find the way to get from your classroom to the next person’s classFind the way to get from your classroom to the next person’s class

4.4. Tell the person how to get to his or her next class.Tell the person how to get to his or her next class.

To do this would require the following procedures:To do this would require the following procedures:

1.1. A way of getting the list of people in the classA way of getting the list of people in the class

2.2. A way of getting the schedule for each person in the class.A way of getting the schedule for each person in the class.

3.3. A program that gives someone directions from your classroom to any other A program that gives someone directions from your classroom to any other classroomclassroom

4.4. A control program that works for each person in the class and does the A control program that works for each person in the class and does the required steps for each personrequired steps for each person

Page 10: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Is that reality?Is that reality?

Probably not.Probably not.

More likely, you would post directions to go from this classroom to all other More likely, you would post directions to go from this classroom to all other classrooms and then tell everyone in the class what you did and to use the classrooms and then tell everyone in the class what you did and to use the directions to go to their next class.directions to go to their next class.

This is a philosophical difference. It’s a shift in responsibility.This is a philosophical difference. It’s a shift in responsibility.

First Method: Responsibility is solely with the instructor, thus you, thus leads to First Method: Responsibility is solely with the instructor, thus you, thus leads to insantity.insantity.

Second Method: Responsibility is divided amongst others. Second Method: Responsibility is divided amongst others.

Page 11: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

What happens when we change requirements?What happens when we change requirements?

Say have a new type of student, aka a grad student.Say have a new type of student, aka a grad student.

FIRST METHODFIRST METHOD

The control program would have to be modified to distinguish grad students from The control program would have to be modified to distinguish grad students from undergrad students. undergrad students.

SECOND METHODSECOND METHOD

People are responsible for themselves, therefore the change would be localized to People are responsible for themselves, therefore the change would be localized to a routine for the new duty of the grad student. a routine for the new duty of the grad student. The control program does not The control program does not changechange!!

Page 12: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

The Object Oriented Paradigm, centered on the concept of the object. Duh. The Object Oriented Paradigm, centered on the concept of the object. Duh.

What is an object?What is an object?

Traditionally: data with method.Traditionally: data with method.

I myself used this loose definition.I myself used this loose definition.

Better Definition: Things responsible for themselves.Better Definition: Things responsible for themselves.

Conceptual Level View of an Object:Conceptual Level View of an Object:

An object is a set of responsibilities.An object is a set of responsibilities.

Specification Level View of an Object:Specification Level View of an Object:

An object is a set of methods (behaviors) that can be invoked by other An object is a set of methods (behaviors) that can be invoked by other objects or itself.objects or itself.

Implementation Level View of an Object:Implementation Level View of an Object:

An object is code and data and the computational interactions between An object is code and data and the computational interactions between them.them.

Page 13: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

An object’s An object’s interfaceinterface is it’s collection of methods. is it’s collection of methods.

A class has the following:A class has the following:– The data elements the object containsThe data elements the object contains– The methods the object can doThe methods the object can do– The way these data elements and methods can be accessedThe way these data elements and methods can be accessed

Page 14: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Object Oriented Approach to the “Classroom” ProblemObject Oriented Approach to the “Classroom” Problem

1.1. Start the control programStart the control program

2.2. Instantiate the collection of students in the classroomInstantiate the collection of students in the classroom

3.3. Tell the collection to have the students go to their next classTell the collection to have the students go to their next class

4.4. The collection tells each student to go to his or her classThe collection tells each student to go to his or her class

5.5. Each studentEach student– Finds where his next class isFinds where his next class is– Determines how to get thereDetermines how to get there– Goes thereGoes there

6.6. DoneDone

This works great with students, but if I add a grad student, then if the student This works great with students, but if I add a grad student, then if the student class was called undergrad student I would have a problem, thus the need for an class was called undergrad student I would have a problem, thus the need for an abstract classabstract class. .

Therefore, I need a general type that encompasses more than one specific type. I Therefore, I need a general type that encompasses more than one specific type. I can have a Student class that includes both an undergrad student and a graduate can have a Student class that includes both an undergrad student and a graduate student.student.

Page 15: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Abstract classes define what other, related classes can do.Abstract classes define what other, related classes can do.

These “other” classes are classes that represent a particular type of related These “other” classes are classes that represent a particular type of related behavior. Such a class is called a behavior. Such a class is called a concrete classconcrete class. .

Therefore, UndergradStudent and GraduateStudent would be concrete classes.Therefore, UndergradStudent and GraduateStudent would be concrete classes.

The relationship between an UndergradStudent and a Student class is called a The relationship between an UndergradStudent and a Student class is called a is-a is-a relationshiprelationship..

An is-a relationship is a form of An is-a relationship is a form of inheritanceinheritance. .

Abstract classes act as placeholders for other concrete classes.Abstract classes act as placeholders for other concrete classes.

Abstract classes define the methods their derived classes Abstract classes define the methods their derived classes mustmust implement. implement.

Page 16: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Three levels of accessibility:Three levels of accessibility:– Public – Anything can see itPublic – Anything can see it– Protected – Only objects of this class and derived classes can see itProtected – Only objects of this class and derived classes can see it– Private – Only objects from this class can see itPrivate – Only objects from this class can see it

What level should you use?What level should you use?

Many say the most restrictive, which would imply private. I think, in most cases, Many say the most restrictive, which would imply private. I think, in most cases, protected is good enough unless you have a really good reason not to want an protected is good enough unless you have a really good reason not to want an inherited class to access or modify something.inherited class to access or modify something.

Encapsulation is not just about data, it’s about any kind of hiding!Encapsulation is not just about data, it’s about any kind of hiding!

In our previous examples, the type of student is in essence hidden from the In our previous examples, the type of student is in essence hidden from the control program (in essence, because it has to instantiate it and thus know what control program (in essence, because it has to instantiate it and thus know what type of object it is).type of object it is).

This encapsulation is known as This encapsulation is known as polymorphismpolymorphism..

Polymorphism can be defined as the ability to refer to different derivation of a Polymorphism can be defined as the ability to refer to different derivation of a class in the same way, but getting the behavior appropriate to the derived class class in the same way, but getting the behavior appropriate to the derived class being referred to.being referred to.

Page 17: CS 350 – Software Design The Object Paradigm – Chapter 1 If you were tasked to write code to access a description of shapes that were stored in a database.

CS 350 – Software DesignCS 350 – Software DesignThe Object Paradigm – Chapter 1The Object Paradigm – Chapter 1

Special Methods:Special Methods:

Constructors – Initialize or set up an objectConstructors – Initialize or set up an object

Destructors – Finalize or clean up an object when it is no longer needed.Destructors – Finalize or clean up an object when it is no longer needed.

The book says destructors are called “when it has been deleted”. This is The book says destructors are called “when it has been deleted”. This is misleading. It depends on the language and the environment. When an object is misleading. It depends on the language and the environment. When an object is deleted in some languages the run time environment decides when the object’s deleted in some languages the run time environment decides when the object’s destructor is called. To me this is incredibly problematic as I like to put things like destructor is called. To me this is incredibly problematic as I like to put things like closing a database connection or saving data in a form or object’s destructor. If I closing a database connection or saving data in a form or object’s destructor. If I don’t know what is called, very bad things can happen.don’t know what is called, very bad things can happen.