Top Banner
1 CS1001 CS1001 Lecture 18 Lecture 18
29

1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

Dec 21, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

11

CS1001CS1001

Lecture 18Lecture 18

Page 2: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

22

OverviewOverview

Object Oriented DesignObject Oriented Design

Page 3: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

33

GoalsGoals

Learn Object-Oriented Design Learn Object-Oriented Design MethodologiesMethodologies

Page 4: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

44

AssignmentsAssignments

Brookshear: Ch 5.5, Ch Brookshear: Ch 5.5, Ch 6.3/6.4, Ch 7 (especially 7.7) 6.3/6.4, Ch 7 (especially 7.7) (Read)(Read)

Read linked documents on these Read linked documents on these slides (slides will be posted in slides (slides will be posted in courseworks)courseworks)

Page 5: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

55

AbstractionAbstraction Abstraction means ignoring irrelevant Abstraction means ignoring irrelevant

features, properties, or functions and features, properties, or functions and emphasizing the relevant ones...emphasizing the relevant ones...

... relevant to the given project (with ... relevant to the given project (with an eye to future reuse in similar an eye to future reuse in similar projects).projects).

“Relevant” to what?

Page 6: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

66

Abstraction (cont’d)Abstraction (cont’d) Example from Example from javax.swing:javax.swing:

public abstract class AbstractButtonpublic abstract class AbstractButton

Fields:Fields:protected protected ButtonModelButtonModel model modeletc. etc.

Methods:Methods:void void addActionListeneraddActionListener (ActionListener l); (ActionListener l);String String getActionCommandgetActionCommand();();String String getTextgetText()()etc.etc.

Apply to any button: “regular” button, a checkbox, a toggle button, etc.

The data model that determines the button’s state

Page 7: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

77

Abstraction (cont’d)Abstraction (cont’d)

java.lang.Objectjava.lang.Object

||

+--java.awt.Component+--java.awt.Component

||

+--java.awt.Container+--java.awt.Container

||

+--javax.swing.JComponent+--javax.swing.JComponent

||

+--javax.swing.+--javax.swing.AbstractButtonAbstractButton

Extends features of other abstract and concrete classes

Page 8: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

88

EncapsulationEncapsulation Encapsulation means that all data members Encapsulation means that all data members

((fieldsfields) of a class are declared ) of a class are declared privateprivate. . Some methods may be private, too.Some methods may be private, too.

The class interacts with other classes (called The class interacts with other classes (called the the clientsclients of this class) only through the of this class) only through the class’s constructors and public methods.class’s constructors and public methods.

Constructors and public methods of a class Constructors and public methods of a class serve as the serve as the interfaceinterface to class’s clients. to class’s clients.

Page 9: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

99

Encapsulation (cont’d) Encapsulation (cont’d)

Ensures that structural changes Ensures that structural changes remain remain locallocal::

– Usually, the structure of a class (as Usually, the structure of a class (as defined by its fields) changes more often defined by its fields) changes more often than the class’s constructors and than the class’s constructors and methods.methods.

– Encapsulation ensures that when fields Encapsulation ensures that when fields change, no changes are needed in other change, no changes are needed in other classes (a principle known as “locality”).classes (a principle known as “locality”).

Page 10: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1010

QuizQuiz

True or False? Abstraction and True or False? Abstraction and encapsulation are helpful for the encapsulation are helpful for the following:following:

Team development ________Team development ________ Reusable software ________Reusable software ________ GUI programming ________GUI programming ________ Easier program maintenance Easier program maintenance ________________

Page 11: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1111

AnswerAnswer

True or False? Abstraction and True or False? Abstraction and encapsulation are helpful for the encapsulation are helpful for the following:following:

Team development ________Team development ________ Reusable software ________Reusable software ________ GUI programming ________GUI programming ________ Easier program maintenance Easier program maintenance ________________

Page 12: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1212

UMLUML

““Unified Modeling Language”Unified Modeling Language” Not so much a language, but Not so much a language, but

more a process for designing more a process for designing softwaresoftware

Provides a rigorous way of Provides a rigorous way of describing the high-level describing the high-level architecture and design of a architecture and design of a software systemsoftware system

Page 13: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1313

Elevator ProblemElevator Problem

A product is to be installed to control elevators in a building A product is to be installed to control elevators in a building with m floors. The problem concerns the logic required to with m floors. The problem concerns the logic required to move elevators between floors according to the following move elevators between floors according to the following constraints:  constraints:     

Each elevator has a set of m buttons, one for each floor. These Each elevator has a set of m buttons, one for each floor. These illuminate when pressed and cause the elevator to visit the illuminate when pressed and cause the elevator to visit the corresponding floor. The illumination is canceled when the corresponding floor. The illumination is canceled when the elevator visits the corresponding floor.  elevator visits the corresponding floor. 

Each floor, except the first floor and top floor has two buttons, Each floor, except the first floor and top floor has two buttons, one to request and up-elevator and one to request a down-one to request and up-elevator and one to request a down-elevator. These buttons illuminate when pressed. The elevator. These buttons illuminate when pressed. The illumination is canceled when an elevator visits the floor and illumination is canceled when an elevator visits the floor and then moves in the desired direction.  then moves in the desired direction. 

  When an elevator has no requests, it remains at its current When an elevator has no requests, it remains at its current floor with its doors closed. floor with its doors closed.

Page 14: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1414

UML ComponentsUML Components

UML is a modeling language that UML is a modeling language that only specifies only specifies semanticssemantics and and notation  notation 

Use Case Diagram  Use Case Diagram  Class Diagram  Class Diagram  Sequence Diagram  Sequence Diagram  Collaboration Diagram  Collaboration Diagram  State Diagram State Diagram

Page 15: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1515

Use CaseUse Case

A generalized A generalized description of how a description of how a system will be used.  system will be used. 

Provides an overview Provides an overview of the intended of the intended functionality of the functionality of the system.  system. 

Understandable by Understandable by laymen as well as laymen as well as professionals.professionals.

Page 16: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1616

Class DiagramClass Diagram

Class diagrams show the static Class diagrams show the static structure of the object, their internal structure of the object, their internal structure, and their relationships.structure, and their relationships.

Page 17: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1717

Sequence DiagramSequence Diagram

A sequence diagram and A sequence diagram and collaboration diagram collaboration diagram conveys similar conveys similar information but information but expressed in different expressed in different ways. A Sequence ways. A Sequence diagram shows the diagram shows the explicit sequence of explicit sequence of messages suitable for messages suitable for modeling a real-time modeling a real-time system, whereas a system, whereas a collaboration diagram collaboration diagram shows the relationships shows the relationships between objects.between objects.

Page 18: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1818

Collaboration DiagramCollaboration Diagram

Describes the set of interactions between classes or Describes the set of interactions between classes or types  types 

  Shows the relationships among objects Shows the relationships among objects 

Page 19: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

1919

State DiagramState Diagram

A state diagram shows the A state diagram shows the sequences of states an object sequences of states an object goes through during it's life cycle goes through during it's life cycle in response to stimuli, together in response to stimuli, together with its responses and actions.  with its responses and actions. 

Page 20: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2020

DetailDetail

Page 21: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2121

PolymorphismPolymorphism We often want to refer to an object by its We often want to refer to an object by its

primary, most specific, data type.primary, most specific, data type.

This is necessary when we call methods This is necessary when we call methods specific to this particular type of object:specific to this particular type of object:

ComputerPlayer player1 = new ComputerPlayer(); HumanPlayer player2 = new HumanPlayer("Nancy", 8); ... if ( player2.getAge () < 10 ) player1.setStrategy (new Level1Strategy ());

Page 22: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2222

Polymorphism (cont’d)Polymorphism (cont’d) But sometimes we want to refer to But sometimes we want to refer to

an object by its inherited, more an object by its inherited, more generic type:generic type:

Player players[ ] = new Player[2]; players[0] = new ComputerPlayer(); players[1] = new HumanPlayer("Nancy”, 8);

game.addPlayer(players[0]); game.addPlayer(players[1]);

Both ComputerPlayerand HumanPlayer implement Player

Page 23: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2323

Polymorphism (cont’d)Polymorphism (cont’d)

Why disguise an object as a more Why disguise an object as a more generic type?generic type?

– To mix different related types in the To mix different related types in the same collectionsame collection

– To pass it to a method that expects a To pass it to a method that expects a parameter of a more generic typeparameter of a more generic type

– To declare a more generic field To declare a more generic field (especially in an abstract class) which (especially in an abstract class) which will be initialized and “specialized” later.will be initialized and “specialized” later.

Page 24: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2424

Polymorphism (cont’d)Polymorphism (cont’d) Polymorphism ensures that the Polymorphism ensures that the

appropriate method is called for an appropriate method is called for an object of a specific type when the object of a specific type when the object is disguised as a more generic object is disguised as a more generic type:type:

while ( game.notDone() ) { players[k].makeMove(); k = (k + 1) % numPlayers; }

The appropriate makeMove method is called for all players (e.g., for a HumanPlayer and a ComputerPlayer).

Page 25: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2525

Polymorphism (cont’d)Polymorphism (cont’d) Good news:Good news: polymorphism is already polymorphism is already

supported in Java — all you have to supported in Java — all you have to do is use it properly.do is use it properly.

Polymorphism is implemented using Polymorphism is implemented using a technique called a technique called latelate (or (or dynamicdynamic) ) method bindingmethod binding: which : which exact method to call is determined exact method to call is determined at run time.at run time.

Page 26: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2626

OO Software DesignOO Software Design Designing a good OOP application is a Designing a good OOP application is a

daunting task.daunting task.

It is largely an art: there are no precise It is largely an art: there are no precise rules for identifying classes, objects, rules for identifying classes, objects, and methods.and methods.

Many considerations determine which Many considerations determine which classes should be defined and their classes should be defined and their responsibilities.responsibilities.

A bad design can nullify all the potential A bad design can nullify all the potential OOP benefits.OOP benefits.

Page 27: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2727

OO Design (cont’d)OO Design (cont’d) A few considerations that determine A few considerations that determine

which classes are defined and their which classes are defined and their responsibilities:responsibilities:

– Manageable sizeManageable size– Clear limited functionalityClear limited functionality– Potential reusePotential reuse– Support for multiple objectsSupport for multiple objects– The need to derive from a library classThe need to derive from a library class– The need to make a listener or to implement The need to make a listener or to implement

a particular interfacea particular interface– The need to collect a few data elements in The need to collect a few data elements in

one entityone entity

Page 28: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2828

Review:Review: Name the main software Name the main software

development concerns that are development concerns that are believed to be addressed by OOP.believed to be addressed by OOP.

Explain the dual role of inheritance.Explain the dual role of inheritance. Can an interface extend another Can an interface extend another

interface? If so, what does it mean?interface? If so, what does it mean? Can an interface extend a class? If Can an interface extend a class? If

so, what does it mean?so, what does it mean? Why do you think Java does not allow Why do you think Java does not allow

a class to extend several classes?a class to extend several classes?

Page 29: 1 CS1001 Lecture 18. 2 Overview Object Oriented Design Object Oriented Design.

2929

Review (cont’d):Review (cont’d): What is abstraction?What is abstraction? Explain how encapsulation helps in Explain how encapsulation helps in

software maintenance.software maintenance. Why sometimes objects end up Why sometimes objects end up

disguised as objects of more generic disguised as objects of more generic types?types?

What is polymorphism?What is polymorphism?