Introduction to object oriented design
Download
1 / 16

Introduction to Object-Oriented Design - PowerPoint PPT Presentation


  • 129 Views
  • Uploaded on

Introduction to Object-Oriented Design. Object-oriented design requires that we interact with a problem in much the same way that we interact with our world – we treat it as a set of separate objects that perform actions and relate to each other.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Introduction to Object-Oriented Design' - schuyler


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
Introduction to object oriented design
Introduction to Object-Oriented Design

  • Object-oriented design requires that we interact with a problem in much the same way that we interact with our world – we treat it as a set of separate objects that perform actions and relate to each other.

  • An object-oriented program is a set of interacting objects, each one responsible for its own activities and data. The objects interact or communicate by passing messages to one another.


What are objects
What are Objects?

  • An object can be considered a container that includes:

    • data in the form of a set of attributes or properties, each of which has a value at any given point in time.

    • a set of operations or methods that can be performed on the data. We often say objects receive messages from other objects, asking them to perform services or operations which are the object’s methods.


Encapsulation and information hiding
Encapsulation and Information Hiding

  • Objects are said to encapsulate (enclose together in a single indivisible unit, like in a capsule) their data and the processes that act on that data.

  • In object-oriented design, each object can be regarded as a ‘black box’ whose internal workings are hidden from all other objects. This is known as information hiding.

  • The principles of encapsulation and information hiding simplifies the use of objects, because the rest of the system does not need to know how they are structured or how they perform their operations.This makes the object as robust and independent as possible.


Where do objects come from
Where Do Objects Come From?

  • An object is created from a template or pattern, called a class, which defines the basic relationships, attributes, and operations available to the objects of that class.

  • The process of creating objects from classes is called instantiation, and an object is described as an instance.


Constructors
Constructors

  • The process of instantiating an object from the class template is performed when a constructor (a function) is called or invoked. Constructors have the same name as their class.

  • Constructors assign initial values to an object’s attributes.

    • Some constructors have parameters that initialize the attributes with specific values.

    • Other constructors have no parameters, in which case a new object is assigned all the default values for its attributes.


Accessors and mutators
Accessors and Mutators

  • An accessor is simply a descriptive name for an operation that accesses a value. By convention, accessor names start with the word get, such as getPayslip().

  • A mutator is simply a descriptive name for an operation that changes the value of a property. By convention, mutator names start with the word set, such as setPayRate().

  • Values stored in the properties of an object may only be changed by the accessors and mutators encapsulated inside that object.


Visibility public and private methods
Visibility: Public and Private Methods

  • Some methods in an object are public and some are private. By declaring an object’s operations as public or private, we are describing their visibility to the system.

  • Operations that perform services for other objects must be visible to those other objects, i.e., they are public methods. (Use + to indicate public methods.) Accessors and mutators are public methods.

  • Operations used only by the object are private methods. They perform the internal actions in an object and cannot be accessed directly from outside the object. (Use – to indicate private methods.)


Steps in creating an object oriented solution
Steps in Creating an Object-Oriented Solution

  • Identify the classes, together with their attributes, responsibilities, and operations.

  • Determine the relationship between the objects of those classes.

  • Design the algorithms for the operations, using structured design.

  • Develop a test or driver program.


Example swimmingpool class
Example: SwimmingPool Class

width

length

maxDepth

minDepth


Swimming pool class pseudocode

Class SwimmingPool

length

width

minDepth

maxDepth

setDimensions(l, w, minD, maxD)

length = l

width = w

minDepth = minD

maxDepth = maxD

END

calcPerimeter()

RETURN (2*length + 2*width)

END

calcVolume()

boxvol = calcBox()

wedgevol = calcWedge()

RETURN (boxvol + wedgevol)

END

calcBox()

RETURN (length*width*minDepth)

END

calcWedge()

RETURN (0.5 * length * width *(maxDepth-minDepth))

END

testSwimmingPool()

Create backyardPool as new SwimmingPool()

backyardPool.setDimensions (20, 40, 3, 8)

PRINT “Perimeter = “ backyardPool.calcPerimeter()

PRINT “Volume = “ backyardPool.calcVolume()

END

Swimming Pool Class Pseudocode


How do you implement classes and objects in javascript
How Do You Implement Classes and Objects in Javascript?

  • Javascript does not support a formal notion of a class like Java or C++.

  • To implement a “class” in Javascript you write your own constructor to create methods and initialize properties in a new object.

  • A constructor in Javascript is a function with two special features:

    • it is invoked through the new operator

    • it uses a special keyword, this, for initialization.


The swimming pool class in javascript 1

// First, define the methods for the class

function setDimensions(l, w, minD, maxD){

this.length = l;

this.width = w;

this.minDepth = minD;

this.maxDepth = maxD;

}

function calcPerimeter(){

return (2*this.length + 2*this.width);

}

function calcVolume(){

var boxvol, wedgevol;

boxvol = this.calcBox();

wedgevol = this.calcWedge();

return (boxvol + wedgevol);

}

The Swimming Pool Class in Javascript - 1


The swimming pool class in javascript 2

function calcBox(){

return (this.length * this.width * this.minDepth);

}

function calcWedge(){

return (0.5 * this.length * this.width *(this.maxDepth-this.minDepth));

}

// Then define a constructor method for our

// SwimmingPool objects. The constructor

// initializes properties and assigns methods

The Swimming Pool Class in Javascript - 2


The swimming pool class in javascript 3

function SwimmingPool(l, w, minD, maxD)

{

// Initialize the properties

this.length = l;

this.width = w;

this.minDepth = minD;

this.maxDepth = maxD;

// Define the methods

this.setDimensions = setDimensions;

this.calcPerimeter = calcPerimeter;

this.calcVolume = calcVolume;

this.calcBox = calcBox;

this.calcWedge = calcWedge;

}

The Swimming Pool Class in Javascript - 3


The swimming pool class in javascript 4

// Now we can use the “class” to construct objects

var backyardPool = new SwimmingPool(20, 40, 3, 8);

var olympicPool = new SwimmingPool(164, 82, 6.5, 6.5);

var perimeter, volume;

perimeter = backyardPool.calcPerimeter();

alert(“Backyard pool perimeter = “ + perimeter);

volume = olympicPool.calcVolume();

alert(“Olympic pool volume = “ + volume);

alert(“Olympic pool depth = “ + olympicPool.minDepth);

The Swimming Pool Class in Javascript - 4


Lab exercise

In Javascript, implement a class called Vacation.

Include 4 properties:

number of days

air/ ship costs

cost per day for hotel

cost per day for meals

Include 2 methods;

setExpenses(days, air, hotel, meals)

calcTotal() which calculates the total cost of the vacation

Using the Vacation class, write a test program that creates 2 Vacation objects and prints out the total cost of each vacation:

var cruiseVacation = new Vacation(7, 400, 200, 0);

var bahamasVacation = new Vacation(10, 300, 200, 100);

Lab Exercise

Show me your work when done.


ad