chapter one the object-oriented paradigm ku-yaw chang [email protected] assistant professor,...
TRANSCRIPT
Chapter OneChapter One
The Object-Oriented ParadigmThe Object-Oriented Paradigm
Ku-Yaw ChangKu-Yaw [email protected]@mail.dyu.edu.tw
Assistant Professor, Department of Assistant Professor, Department of Computer Science and Information EngineeringComputer Science and Information Engineering
Da-Yeh UniversityDa-Yeh University
22Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OutlineOutline
OverviewOverviewBefore the Object-Oriented Paradigm: Functional Before the Object-Oriented Paradigm: Functional DecompositionDecompositionThe Problem of RequirementsThe Problem of RequirementsDealing with Changes: Using Functional DecompositionDealing with Changes: Using Functional DecompositionDealing with Changing RequirementsDealing with Changing RequirementsThe Object-Oriented ParadigmThe Object-Oriented ParadigmObject-Oriented Programming in ActionObject-Oriented Programming in ActionSpecial Object MethodsSpecial Object MethodsSummarySummary
33Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OverviewOverview
Object-Oriented ParadigmObject-Oriented Paradigm Compare and contrast withCompare and contrast with
Standard Structured ProgrammingStandard Structured Programming
ContentsContents Functional decompositionFunctional decomposition Problem of requirementsProblem of requirements Object-oriented paradigmObject-oriented paradigm Special object methodsSpecial object methods Important object terminologyImportant object terminology
44Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OutlineOutline
OverviewOverviewBefore the Object-Oriented Paradigm: Before the Object-Oriented Paradigm: Functional DecompositionFunctional DecompositionThe Problem of RequirementsThe Problem of RequirementsDealing with Changes: Using Functional Dealing with Changes: Using Functional DecompositionDecompositionDealing with Changing RequirementsDealing with Changing RequirementsThe Object-Oriented ParadigmThe Object-Oriented ParadigmObject-Oriented Programming in ActionObject-Oriented Programming in ActionSpecial Object MethodsSpecial Object MethodsSummarySummary
55Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Functional DecompositionFunctional Decomposition
A natural way to deal with complexityA natural way to deal with complexity
Example: Example: You are given a task to write code to You are given a task to write code to Access a description of shapes that were Access a description of shapes that were
stored in a databasestored in a database Display shapesDisplay shapes
66Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Functional DecompositionFunctional Decomposition
It would be natural to think in terms of the It would be natural to think in terms of the steps required:steps required:
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 Display the individual shapes on the monitor.monitor.
77Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Functional DecompositionFunctional Decomposition
Further break down Step 4.Further break down Step 4.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.a)a) Identify type of shape.Identify type of shape.
b)b) Get location of shape.Get location of shape.
c)c) Call appropriate function that will display shape, Call appropriate function that will display shape, giving it the shape’s location.giving it the shape’s location.
88Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Functional DecompositionFunctional Decomposition
A natural way to deal with complexityA natural way to deal with complexity Break down (decompose) the problem into Break down (decompose) the problem into
the functional steps that compose it.the functional steps that compose it.
ChallengeChallenge Not help us prepare the code for possible Not help us prepare the code for possible
changes in the futurechanges in the futureDealing with change – a graceful evolutionDealing with change – a graceful evolution
99Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Functional DecompositionFunctional Decomposition
Many bugs originate with changes to Many bugs originate with changes to code.code.
Change creates opportunities for mistakes Change creates opportunities for mistakes and unintended consequences.and unintended consequences.
Nothing you can do will stop change. But Nothing you can do will stop change. But you do not have to be overcome by it.you do not have to be overcome by it.
You can never get all of requirements from You can never get all of requirements from the user.the user.
Too much is unknown about the future – Too much is unknown about the future – things change.things change.
1010Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OutlineOutline
OverviewOverviewBefore the Object-Oriented Paradigm: Before the Object-Oriented Paradigm: Functional DecompositionFunctional DecompositionThe Problem of RequirementsThe Problem of RequirementsDealing with Changes: Using Functional Dealing with Changes: Using Functional DecompositionDecompositionDealing with Changing RequirementsDealing with Changing RequirementsThe Object-Oriented ParadigmThe Object-Oriented ParadigmObject-Oriented Programming in ActionObject-Oriented Programming in ActionSpecial Object MethodsSpecial Object MethodsSummarySummary
1111Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
The Problem of RequirementsThe Problem of Requirements
Requirements from users areRequirements from users are IncompleteIncomplete Usually wrongUsually wrong MisleadingMisleading Do not tell the whole storyDo not tell the whole story
Requirements always change.Requirements always change. A bad thingA bad thing Few write their code to handle changing Few write their code to handle changing
requirementrequirement
1212Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
The Problem of RequirementsThe Problem of Requirements
Requirements change for a very simple set of Requirements change for a very simple set of reasons:reasons: The users see new possibilities for the software after The users see new possibilities for the software after
discussions with developers.discussions with developers. Developers become more familiar with users’ problem Developers become more familiar with users’ problem
domain.domain. The environment in which the software is being The environment in which the software is being
developed changes.developed changes.
Must write our code to accommodate changeMust write our code to accommodate change Not give up on gathering good requirementsNot give up on gathering good requirements
1313Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OutlineOutline
OverviewOverviewBefore the Object-Oriented Paradigm: Before the Object-Oriented Paradigm: Functional DecompositionFunctional DecompositionThe Problem of RequirementsThe Problem of RequirementsDealing with Changes: Using Functional Dealing with Changes: Using Functional DecompositionDecompositionDealing with Changing RequirementsDealing with Changing RequirementsThe Object-Oriented ParadigmThe Object-Oriented ParadigmObject-Oriented Programming in ActionObject-Oriented Programming in ActionSpecial Object MethodsSpecial Object MethodsSummarySummary
1414Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Dealing with Changes:Dealing with Changes:Using Functional DecompositionUsing Functional DecompositionUsing modularity to contain variationUsing modularity to contain variation
Make code more modularMake code more modular
Step 4c of displaying shapesStep 4c of displaying shapes Call appropriate function that will display shape, giving it the Call appropriate function that will display shape, giving it the
shape’s locationshape’s location
function : display shapefunction : display shapeinput: type of shape, description of shapeinput: type of shape, description of shapeaction:action: switch (type of shape) switch (type of shape) case squarecase square: :
put display function for square hereput display function for square here case circlecase circle::
put display function for circle hereput display function for circle here
1515Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Problems with ModularityProblems with Modularity
May or may not be possible to have May or may not be possible to have a a consistent descriptionconsistent description of shapes that will of shapes that will work for all shapeswork for all shapesModularityModularity Make the code more understandableMake the code more understandable Understandability Understandability
Make the code easier to maintainMake the code easier to maintain Does not deal with all of the variation it might Does not deal with all of the variation it might
encounterencounter
1616Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Low Cohesion and Tight CouplingLow Cohesion and Tight Coupling
CohesionCohesion How closely the operations in a routine are relatedHow closely the operations in a routine are related
CouplingCoupling The strength of a connection between two routinesThe strength of a connection between two routines
GoalGoal To create routines with internal integrity (To create routines with internal integrity (strong strong
cohesioncohesion) and small, direct, visible, and flexible ) and small, direct, visible, and flexible relations to other routines (relations to other routines (loose couplingloose coupling))
1717Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Unwanted Side EffectUnwanted Side Effect
Unwanted side effectUnwanted side effect Make a change to a function or piece of data in one Make a change to a function or piece of data in one
area of the codearea of the codeHave an unexpected impact on other pieces of codeHave an unexpected impact on other pieces of code
We really do not spend much time fixing bugs.We really do not spend much time fixing bugs. The overwhelming amount of time spent in The overwhelming amount of time spent in
maintenance and debugging is onmaintenance and debugging is onFinding bugsFinding bugs
Avoiding unwanted side effectsAvoiding unwanted side effects
1818Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Wrong FocusWrong Focus
Changes to one set of functions or data Changes to one set of functions or data impact other sets of functions and other impact other sets of functions and other sets of datasets of data In turn impact other functions that must be In turn impact other functions that must be
changedchangedLike a snowball that picks up snow as it rolls Like a snowball that picks up snow as it rolls downhilldownhill
1919Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OutlineOutline
OverviewOverviewBefore the Object-Oriented Paradigm: Before the Object-Oriented Paradigm: Functional DecompositionFunctional DecompositionThe Problem of RequirementsThe Problem of RequirementsDealing with Changes: Using Functional Dealing with Changes: Using Functional DecompositionDecompositionDealing with Changing RequirementsDealing with Changing RequirementsThe Object-Oriented ParadigmThe Object-Oriented ParadigmObject-Oriented Programming in ActionObject-Oriented Programming in ActionSpecial Object MethodsSpecial Object MethodsSummarySummary
2020Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Dealing withDealing withChanging RequirementsChanging Requirements
How do people do thing?How do people do thing?ExampleExample
You were an instructor at a conferenceYou were an instructor at a conference People in your class had another class to attend People in your class had another class to attend
following yours, but didn’t know where it was located.following yours, but didn’t know where it was located.
Structured programming approachStructured programming approach1.1. Get list of people in the class.Get list of people in the class.2.2. For each person on this list:For each person on this list:
a.a. Find the next class they are takingFind the next class they are takingb.b. Find the location of that classFind the location of that classc.c. Find the way to get from your classroom to the person’s Find the way to get from your classroom to the person’s
next classnext classd.d. Tell the person how to get to their next classTell the person how to get to their next class
2121Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Dealing withDealing withChanging RequirementsChanging Requirements
Require following proceduresRequire following procedures1.1. A way of getting the list of people in the classA way of getting the list of people in the class2.2. A way of getting the schedule for each person in the A way of getting the schedule for each person in the
classclass3.3. A program that gives someone directions from your A program that gives someone directions from your
classroom to any other classroomclassroom to any other classroom4.4. A control program that works for each person in the A control program that works for each person in the
class and does the required steps for each personclass and does the required steps for each person
Would you actually follow this approach?Would you actually follow this approach? Post directions in the back of the roomPost directions in the back of the room Expect everyone would know what their next class Expect everyone would know what their next class
waswas
2222Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
DifferenceDifference
First caseFirst case Give Give explicit directionsexplicit directions to everyone. to everyone. No one other than you is responsible for anything.No one other than you is responsible for anything.
Second caseSecond case Give Give general instructionsgeneral instructions Each person figure out how to do the task himself or Each person figure out how to do the task himself or
herselfherself
Shift of responsibilityShift of responsibility The same thing must be implemented.The same thing must be implemented. The organization is very different.The organization is very different.
2323Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
The ImpactThe Impact
New requirementsNew requirements Give special instructions to graduate students Give special instructions to graduate students
who are assisting at the conferencewho are assisting at the conferenceCollect course evaluationsCollect course evaluations
Take them to the conference office before going to Take them to the conference office before going to the next classthe next class
2424Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Why the DifferenceWhy the Difference
First case First case Modify the control program to distinguish the graduate Modify the control program to distinguish the graduate
students from the undergraduatesstudents from the undergraduates Give special instructions to the graduate studentsGive special instructions to the graduate students
Modify the program considerablyModify the program considerably
Second caseSecond case Write an additional routine for graduate students to Write an additional routine for graduate students to
followfollow Control program remains the sameControl program remains the same
Significant differenceSignificant difference Control programControl program
2525Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
What Makes It HappenWhat Makes It Happen
The people are responsible for themselvesThe people are responsible for themselves Instead of the control program being Instead of the control program being
responsible for themresponsible for them
The control program can talk to different The control program can talk to different types of people as if they were exactly the types of people as if they were exactly the samesame
The control program does not need to The control program does not need to know about any special stepsknow about any special steps
2626Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Perspectives in the Perspectives in the Software Development ProcessSoftware Development Process
ConceptualConceptual The concepts in the domain under studyThe concepts in the domain under study With little or no regard for the software that With little or no regard for the software that
might implement itmight implement it
SpecificationSpecification Look at the interface of the software, not Look at the interface of the software, not
implementationimplementation
ImplementationImplementation Look at the code itselfLook at the code itself
2727Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
How Perspectives HelpHow Perspectives Help
Conceptual levelConceptual level Go to your next classGo to your next class What to do, not how to doWhat to do, not how to do
Implementation levelImplementation level The way they go to their classThe way they go to their class
Communicating at one level (conceptually) while Communicating at one level (conceptually) while performing at another level (implementation)performing at another level (implementation) The requestor (the instructor) does not know exactly The requestor (the instructor) does not know exactly
what is happeningwhat is happeningOnly know conceptually what is happeningOnly know conceptually what is happening
2828Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OutlineOutline
OverviewOverviewBefore the Object-Oriented Paradigm: Before the Object-Oriented Paradigm: Functional DecompositionFunctional DecompositionThe Problem of RequirementsThe Problem of RequirementsDealing with Changes: Using Functional Dealing with Changes: Using Functional DecompositionDecompositionDealing with Changing RequirementsDealing with Changing RequirementsThe Object-Oriented ParadigmThe Object-Oriented ParadigmObject-Oriented Programming in ActionObject-Oriented Programming in ActionSpecial Object MethodsSpecial Object MethodsSummarySummary
2929Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
The Object-Oriented ParadigmThe Object-Oriented Paradigm
Centered on the concept of the objectCentered on the concept of the object
ObjectObject Data with methodsData with methods
Data (Attributes) can be simple things like number Data (Attributes) can be simple things like number or character strings, or they can be other objectsor character strings, or they can be other objects
Define things that are responsible for Define things that are responsible for themselvesthemselves
Data: to know what state it is inData: to know what state it is in
Method (Code): to function properlyMethod (Code): to function properly
3030Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Objects and Their ResponsibilitiesObjects and Their Responsibilities
This Object…This Object… Is Responsible ForIs Responsible For
StudentStudent
Knowing which classroom they are inKnowing which classroom they are in
Know which classroom they are to go to nextKnow which classroom they are to go to next
Going from one classroom to the next classroomGoing from one classroom to the next classroom
InstructorInstructor Telling people to go to next classroomTelling people to go to next classroom
ClassroomClassroom Having a locationHaving a location
Direction GiverDirection Giver Given two classrooms, giving directions from one Given two classrooms, giving directions from one classroom to the otherclassroom to the other
3131Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
How to Think About ObjectsHow to Think About Objects
ObjectsObjects Something with responsibilitiesSomething with responsibilities
A good design ruleA good design rule Objects should be responsible for themselves Objects should be responsible for themselves
and should have those responsibilities clearly and should have those responsibilities clearly defineddefined
3232Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
How to Think About ObjectsHow to Think About Objects
At the conceptual levelAt the conceptual level An object is a set of responsibilitiesAn object is a set of responsibilities
At the specification levelAt the specification level An object is a set of methods that can be An object is a set of methods that can be
invoked by other objects or by itselfinvoked by other objects or by itself
At the implementation levelAt the implementation level An object is code and dataAn object is code and data
3333Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Public InterfacePublic Interface
Many methods of an object will be identified as Many methods of an object will be identified as callable by other objects.callable by other objects.
The collection of these methods is called the The collection of these methods is called the object’s object’s public interfacepublic interface StudentStudent object with the method gotoNextClassroom() object with the method gotoNextClassroom() More kinds of studentsMore kinds of students
Inefficient for each student type to have its own set of Inefficient for each student type to have its own set of methodsmethods
A more efficient approachA more efficient approach A general student to contain common methodsA general student to contain common methods Be used or tailored to their needsBe used or tailored to their needs
3434Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
The Object-Oriented ParadigmThe Object-Oriented Paradigm
A class is a definition of the behavior of an A class is a definition of the behavior of an object, containing a complete description ofobject, containing a complete description of 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 The way these data elements and methods can be
accessedaccessed
Objects are Objects are instancesinstances of classes of classes Creating instances of a class is called Creating instances of a class is called instantiationinstantiation Each object of the same typeEach object of the same type
Different data, but have the same functionalityDifferent data, but have the same functionality
3535Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Go to the Next ClassroomGo to the Next Classroom
1.1. Start the control programStart the control program2.2. Instantiate the collection of students in the classroomInstantiate the collection of students in the classroom3.3. Tell the collection to have the students go to their next Tell the collection to have the students go to their next
classclass4.4. The collection tells each students to go to their next The collection tells each students to go to their next
classclass5.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
3636Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
The Need for an Abstract TypeThe Need for an Abstract Type
The dilemmaThe dilemma Allow any type of student into the collectionAllow any type of student into the collection The collection is an array or something of some type The collection is an array or something of some type
of objectof objectRegularStudentRegularStudentGraduateStudentGraduateStudent
The solutionThe solution A general type that encompasses more than one A general type that encompasses more than one
specific typespecific typeStudent : an Student : an abstract classabstract class
RegularStudentRegularStudent GraduateStudentGraduateStudent
3737Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Abstract ClassAbstract Class
Abstract class define what other, related Abstract class define what other, related classes can do.classes can do. These “other” classes represent a particular These “other” classes represent a particular
type of related behavior.type of related behavior. Such a class is often called a Such a class is often called a concrete classconcrete class..
The abstract class is StudentThe abstract class is Student Concrete classes are RegularStudent and Concrete classes are RegularStudent and
GraduateStudentGraduateStudentRegularStudent is one kind of StudentRegularStudent is one kind of StudentGraduateStudent is one kind of StudentGraduateStudent is one kind of Student
3838Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Abstract ClassAbstract Class
InheritanceInheritance An An is-ais-a relationship relationship
RegularStudent class inherits from StudentRegularStudent class inherits from Student Derives fromDerives from SpecializesSpecializes A A subclasssubclass of of
Student class is the base class of Student class is the base class of GraduateStudent and of RegularStudentGraduateStudent and of RegularStudent GeneralizesGeneralizes A A superclasssuperclass of of
3939Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Abstract Class Abstract Class
Placeholders for other classesPlaceholders for other classes Define the methods their derived classes must Define the methods their derived classes must
implementimplement Contain common methods that can be used Contain common methods that can be used
by all derivationsby all derivations
Controller contains StudentsController contains Students The collection only needs to deal with The collection only needs to deal with
StudentsStudents Each kind of Student is left to implement its Each kind of Student is left to implement its
functionality in its own wayfunctionality in its own way
4040Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
VisibilityVisibility
PublicPublic Anything can see itAnything can see it
ProtectedProtected Only objects of this class and derived classes Only objects of this class and derived classes
can see itcan see it
PrivatePrivate Only objects from this class can see itOnly objects from this class can see it
4141Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Encapsulation and PolymorphismEncapsulation and Polymorphism
EncapsulationEncapsulation Hiding dataHiding data
In general, any kind of hidingIn general, any kind of hiding
PolymorphismPolymorphism Poly : manyPoly : many Morph : formMorph : form Different behaviorDifferent behavior
Depend upon the specific type of derived objectDepend upon the specific type of derived object
4242Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Review of OO TerminologyReview of OO Terminology
ObjectObjectClassClassEncapsulationEncapsulationInheritanceInheritanceInstanceInstanceInstantiationInstantiationPolymorphismPolymorphismPerspectivesPerspectives
4343Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OutlineOutline
OverviewOverviewBefore the Object-Oriented Paradigm: Before the Object-Oriented Paradigm: Functional DecompositionFunctional DecompositionThe Problem of RequirementsThe Problem of RequirementsDealing with Changes: Using Functional Dealing with Changes: Using Functional DecompositionDecompositionDealing with Changing RequirementsDealing with Changing RequirementsThe Object-Oriented ParadigmThe Object-Oriented ParadigmObject-Oriented Programming in ActionObject-Oriented Programming in ActionSpecial Object MethodsSpecial Object MethodsSummarySummary
4444Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
New ExampleNew Example
Shape ExampleShape Example1.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
4545Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Objects in Shape ProgramObjects in Shape Program
ShapeDataBaseShapeDataBase getCollection – get a specified collection of shapesgetCollection – get a specified collection of shapes
ShapeShape (an abstract class) (an abstract class) display – defines interface for Shapesdisplay – defines interface for Shapes getX – return X location of ShapegetX – return X location of Shape getY – return Y location of ShapegetY – return Y location of Shape
SquareSquare (derived from Shape) (derived from Shape) display – display a square (represented by this object)display – display a square (represented by this object)
CircleCircle (derived from Shape) (derived from Shape) Display – display a circle (represented by this object)Display – display a circle (represented by this object)
4646Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Objects in Shape ProgramObjects in Shape Program
CollectionCollection display – tell all contained shapes to display display – tell all contained shapes to display sort – sort the collection of shapessort – sort the collection of shapes
DisplayDisplay drawLine – draw a line on the screendrawLine – draw a line on the screen drawCircle – draw a circle on the screendrawCircle – draw a circle on the screen
4747Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Main ProgramMain Program
Create an instance of the database objectCreate an instance of the database object
Ask the database object to find the set of shapesAsk the database object to find the set of shapes Instantiate a collection object containing all of the Instantiate a collection object containing all of the
shapesshapes
Ask the collection to sort the shapesAsk the collection to sort the shapes
Ask the collection to display the shapesAsk the collection to display the shapes
The collection asks each shape to display itselfThe collection asks each shape to display itself
Each shape display itselfEach shape display itself
4848Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
New RequirementsNew Requirements
Add new kinds of shapes (such as a Add new kinds of shapes (such as a triangle)triangle) Create a new derivation of Shape that defines Create a new derivation of Shape that defines
the shape.the shape. Implement a version of the display method Implement a version of the display method
that is appropriate for that typethat is appropriate for that type
Change the sorting algorithmChange the sorting algorithm Modify the method in Collection. Every shape Modify the method in Collection. Every shape
will use the new algorithmwill use the new algorithm
4949Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Encapsulation RevisitedEncapsulation Revisited
Using things is easierUsing things is easier User does not need to worry about User does not need to worry about
implementation issuesimplementation issues
Implementations can be changed without Implementations can be changed without worrying about the callerworrying about the caller
The insides of an object are unknown to The insides of an object are unknown to outside objectsoutside objects
5050Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
BenefitsBenefits
The more I make my objects responsible The more I make my objects responsible for their own behaviors, the less the for their own behaviors, the less the controlling programs have to be controlling programs have to be responsible for.responsible for.Encapsulation makes changes to an Encapsulation makes changes to an object’s internal behavior transparent to object’s internal behavior transparent to other objects.other objects.Encapsulation helps to prevent unwanted Encapsulation helps to prevent unwanted side effectsside effects
5151Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OutlineOutline
OverviewOverviewBefore the Object-Oriented Paradigm: Before the Object-Oriented Paradigm: Functional DecompositionFunctional DecompositionThe Problem of RequirementsThe Problem of RequirementsDealing with Changes: Using Functional Dealing with Changes: Using Functional DecompositionDecompositionDealing with Changing RequirementsDealing with Changing RequirementsThe Object-Oriented ParadigmThe Object-Oriented ParadigmObject-Oriented Programming in ActionObject-Oriented Programming in ActionSpecial Object MethodsSpecial Object MethodsSummarySummary
5252Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
Special Object MethodsSpecial Object Methods
ConstructorConstructor A special method that is automatically called when the A special method that is automatically called when the
object is createdobject is created Handle starting up the objectHandle starting up the object
To eliminate uninitialized variablesTo eliminate uninitialized variables
DestructorDestructor A special method that helps an object clean up after A special method that helps an object clean up after
itself when the object is destroyeditself when the object is destroyed Release resourcesRelease resources JavaJava
Garbage collectionGarbage collection
5353Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
OutlineOutline
OverviewOverviewBefore the Object-Oriented Paradigm: Before the Object-Oriented Paradigm: Functional DecompositionFunctional DecompositionThe Problem of RequirementsThe Problem of RequirementsDealing with Changes: Using Functional Dealing with Changes: Using Functional DecompositionDecompositionDealing with Changing RequirementsDealing with Changing RequirementsThe Object-Oriented ParadigmThe Object-Oriented ParadigmObject-Oriented Programming in ActionObject-Oriented Programming in ActionSpecial Object MethodsSpecial Object MethodsSummarySummary
5454Ku-Yaw ChangKu-Yaw Chang The Object-Oriented ParadigmThe Object-Oriented Paradigm
SummarySummary
Object orientation helps us minimize Object orientation helps us minimize consequences of shifting requirementsconsequences of shifting requirements In contrast with functional decompositionIn contrast with functional decomposition
Essential concepts and primary Essential concepts and primary terminology in object-oriented terminology in object-oriented programmingprogramming