51
Introduction to Object Oriented Design

Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Embed Size (px)

Citation preview

Page 1: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Introduction toObject Oriented

Design

Page 2: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Topics

Designing Your Own ClassesAttributes and BehaviorsClass Diagrams

Page 3: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Objectives

At the completion of this topic, students should be able to:

Design classes for use in a C# programExplain the difference between a class and an objectExplain what attributes and behaviors areExplain the terms encapsulation and data hidingCreate accurate class diagrams using UML

Page 4: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Motivation

Page 5: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Consider the following simple program

Page 6: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Press this buttonto add 1 to the

counter

Page 7: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Press this buttonto subtract 1 from

the counter

Page 8: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Press this buttonto reset the

count to zero.

Page 9: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

To make the counter work, we have to

(1) Declare a variable to hold the value of the counter. This variable must be visible to all methods in the Form.

Page 10: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

To make the counter work, we have to

(2) In the Form constructor, set this value to zero.

Page 11: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

To make the counter work, we have to

(3) Write methods for each button, for example

Page 12: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

The Problem!

The user interface code gets all tangledup with the “business logic” of the program.This makes the code hard to maintain, hard to debug, and makes the code hard to re-use.

Page 13: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

To solve this problem, good programmers keep everything in neat, separate piles.

Page 14: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

To solve this problem, good programmers keep everything in neat, separate piles.

Userinterface

codeBusiness logic

Page 15: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

The User Interface code belongs in the Form.It’s main tasks are to display information to

the user, and to get input from the user.

Page 16: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

We need a way of packaging up the application’sdata and the methods that operate on the data in one unit, so that the data is visible to all of themethods that will work on it, but keep it separatefrom the user interface logic.

We can, if we use objects!

Page 17: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Objects

Page 18: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Key Concept

An object often models things in the real world

A counter

Page 19: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Real world objects have attributes

An object’s attributes describe its“state of being”

depending upon the application, some attributes are more important than others

value

sizecolor

Page 20: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Real world objects have attributes

An object’s attributes describe its“state of being”

For our application, we are interested in

value

Page 21: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

An object also has behaviors

behaviors define how you interact with the object

Get the current valueof the counter

Subtract one from

The counter

Add one to the counter

Reset the counterTo zero

Page 22: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

An Object’s Attributes and Behaviors Should Work Together

this is called cohesion

Page 23: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

An Object’s Attributes and Behaviors Should Work Together

This object has strong cohesion, becauseall of the operations work on the single

data value in the counter, it’s value.

Page 24: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

A Class is a blueprint that a programuses when it creates an object.

A class reserves no space in memory

When an object is created from the classblueprint, memory is reserved to hold the

object’s attributes.

An object is known as an instance of the class.

Each object has it’s own space for data.

Page 25: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

A class is said to be an abstraction of thereal world object that we are modeling.

Page 26: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

EncapsulationCounter object

Add( )

theValue

calling method

we should not allow codeoutside of the object to reach in and change the data directly. Instead, we call methods in theobject to do it for us.

member data is declared as private

member methods are declared as public

public and private are called access modifiers

Page 27: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

We use a UML Class Diagram to documentthe data and methods contained in

our class.

Page 28: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Counter

A UML class diagram is usedto describe a class in a very preciseway.

A class diagram is a rectangle.

At the top of the rectangle is theclass name. A line separates theclass name from the rest of thediagram.

class Counter{}Code represented by the UML diagram

Page 29: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Counter

- counterValue: intFollowing the class name we writethe data members of the class. Aline separates the data membersfrom the rest of the diagram.

access modifier:+ public- private

data member name

data type

class BowlingTeam{ private int counterValue;}Code represented by the UML diagram

Page 30: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

+ Add( ): void

Following the data members, wewrite the member methods.

access modifier + public - private

method name

parameters

return type

class Counter{ private int counterValue; public void Add( ){ }}Code represented by the UML diagram

Counter

- counterValue: int

Page 31: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

+ Add( ): void + Subtract: void + Reset( ): void + GetValue( ): int

Following the data members, wewrite the member methods.

class Counter{ private int counterValue; public void Add( ){ } public void Subtract( ) { } public void Reset( ) { } public int GetValue( ) { }}Code represented by the UML diagram

Counter

- counterValue: int

Page 32: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

It is important that class diagrams be drawnprecisely and that they conform to the form

shown in these examples.

When you submit a class diagram, thepreferred file format is pdf.

Page 33: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

The Form object now(1) Creates a Counter object(2) Initializes it(3) Sends messages to the object

Counter

Page 34: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

The ability to create good models of the realworld objects in our programs takes a lot of

practice and a long time to develop.

Page 35: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Practice

Page 36: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Design a class that represents “Integer” objects.

What are the data members of the class?

Page 37: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Design a class that represents “Integer” objects.

Suppose we want methods to set the integer value in the object retrieve the integer value in the object retrieve the reciprocal of the value in the object

Page 38: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Create the UML class diagramInteger

Page 39: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Design a class that represents “StudentInfo” objects.You could use an object of this class to hold thestudent information you print out at the beginningof each of your programming projects.

What are the data members of the class?

Page 40: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Design a class that represents “StudentInfo” objects.

Suppose we want methods to set the name, course, and section values in the object retrieve the name, course and section values from the object output the data in the student object

Page 41: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Create the UML class diagram

StudentInfo

Page 42: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Design a class that represents a car.The important attributes of a car for thisapplication are how much gas it has in its tank,and what kind of mileage (mpg) it gets.

We need member methods (behaviors) that provide the following:- Create a Car object with a given mpg rating- add n gallons of gas to the tank- drive the car y miles- report on how much gas is in the tank

Page 43: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Car

Page 44: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Design a class that represents a student. The important properties of a student for this application are the student’s name, and the scores for two quizzes (10 pts possible on each) and two exams (100 pts possible on each).

We need member methods that• Create a student object – set all scores to zero• Save the score for quiz 1• Save the score for quiz 2• Save the score for exam 1• Save the score for exam 2• Calculates the student’s percent of points possible

Page 45: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Create the UML class diagram

Student

Page 46: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Checking Account

Page 47: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Create the UML class diagram

Checking Account

Page 48: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

PayCheck

Page 49: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Create the UML class diagram

Paycheck

Page 50: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

A Coin Purse

Page 51: Introduction to Object Oriented Design. Topics Designing Your Own Classes Attributes and Behaviors Class Diagrams

Create the UML class diagram

CoinPurse