34
Object-Oriented Object-Oriented Design Design Java Fundamentals and Object Java Fundamentals and Object Oriented Programming Oriented Programming The Complete Java Boot Camp The Complete Java Boot Camp MELJUN CORTES MELJUN CORTES MELJUN CORTES MELJUN CORTES

MELJUN CORTES Java Lecture OOD

Embed Size (px)

DESCRIPTION

MELJUN CORTES Java Lecture OOD

Citation preview

Page 1: MELJUN CORTES Java Lecture OOD

Object-Oriented Object-Oriented DesignDesign

Java Fundamentals and Object Java Fundamentals and Object Oriented ProgrammingOriented Programming

The Complete Java Boot CampThe Complete Java Boot Camp

MELJUN CORTESMELJUN CORTES

MELJUN CORTESMELJUN CORTES

Page 2: MELJUN CORTES Java Lecture OOD

What You Should LearnWhat You Should Learn

1.1. What is a Good OO Program?What is a Good OO Program?

2.2. What is a Bad OO Program?What is a Bad OO Program?

3.3. How Do We Write a Good OO Program?How Do We Write a Good OO Program?

4.4. Steps in Designing an OO ProgramSteps in Designing an OO Program

Page 3: MELJUN CORTES Java Lecture OOD

What is a Good OO What is a Good OO Program?Program?

MaintainableMaintainable 80% of the cost of a program is in 80% of the cost of a program is in maintenancemaintenance

New features Bug fixes

It should be easy to locate where to add new features or It should be easy to locate where to add new features or where problems exist.where problems exist.

Changes should only affect a few lines of code.Changes should only affect a few lines of code. Changes in one component should not affect others.Changes in one component should not affect others.

Page 4: MELJUN CORTES Java Lecture OOD

What is a Good OO What is a Good OO Program?Program? Unit-TestableUnit-Testable

Each component should be independent of Each component should be independent of other components, so can be tested other components, so can be tested in in isolation.isolation.

Page 5: MELJUN CORTES Java Lecture OOD

What is a Good OO What is a Good OO Program?Program? UnderstandableUnderstandable

It should be easy to read the code and It should be easy to read the code and understand what it does.understand what it does.

If you need to fix or change something, it If you need to fix or change something, it should be intuitive to know where the lines of should be intuitive to know where the lines of code you need to change are located.code you need to change are located.

Page 6: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

““Code Smells” – signs that there might be Code Smells” – signs that there might be something wrong with your code.something wrong with your code.

Page 7: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Large classes and long methods.Large classes and long methods. Means your code is trying to do too much.Means your code is trying to do too much. OOP is about creating little programs (objects) OOP is about creating little programs (objects)

that do that do just one thingjust one thing, and do it well., and do it well. Each object is so simple so as to minimize Each object is so simple so as to minimize

bugs.bugs.

Page 8: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Primitive ObsessionPrimitive Obsession Using primitives / built-in classes instead of Using primitives / built-in classes instead of

creating own classes.creating own classes. Data ClumpsData Clumps

Data that always appears together.Data that always appears together. Long Parameter ListLong Parameter List

Methods have too many parameters. A sign Methods have too many parameters. A sign that you have data clumps.that you have data clumps.

Page 9: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Primitive Obsession, Data Clumps, Primitive Obsession, Data Clumps, Long Long Parameter ListParameter List You should group data that are used together You should group data that are used together

into a more meaningful class. Abstract away into a more meaningful class. Abstract away the details.the details.

Page 10: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Too many switch statementsToo many switch statements Sign that a method or class is trying to exhibit Sign that a method or class is trying to exhibit

too many behaviors.too many behaviors. Use polymorphism to encapsulate different Use polymorphism to encapsulate different

behaviors into specific subtypes.behaviors into specific subtypes.

Page 11: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Refused BequestRefused Bequest When a subclass does not use members When a subclass does not use members

inherited from a superclassinherited from a superclass Dangerous because those methods might Dangerous because those methods might

cause undesirable behavior in the subclasscause undesirable behavior in the subclass

Page 12: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Alternative Classes with Different Alternative Classes with Different InterfacesInterfaces Closely related classes should share the same Closely related classes should share the same

interface, so that they can be pluggable to the interface, so that they can be pluggable to the client code.client code.

Example: “OracleConnectionManager” and Example: “OracleConnectionManager” and “MsSqlServerConnectionManager” should “MsSqlServerConnectionManager” should share the same interface.share the same interface.

Page 13: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Shotgun SurgeryShotgun Surgery Several classes changed for every new Several classes changed for every new

feature / change.feature / change. Maybe some code needs to be consolidated in Maybe some code needs to be consolidated in

a single class?a single class?

Page 14: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Divergent ChangeDivergent Change Same class changed for different reasons.Same class changed for different reasons.

“Well, I will have to change these three methods every time I get a new database and I have to change these four methods every time there is a new financial instrument.."

Maybe the class needs to be split?Maybe the class needs to be split? Ex. One class is modified for database changes

and a different class is modified for new financial instruments.

Page 15: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Lazy classLazy class Class that doesn't do much, better just move Class that doesn't do much, better just move

what it does to other classes then delete itwhat it does to other classes then delete it Duplicate CodeDuplicate Code

The number one sin!The number one sin! Dead CodeDead Code

unused codeunused code

Page 16: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Feature EnvyFeature Envy when a method excessively accesses the data when a method excessively accesses the data

of another classof another class better to move the method to the other classbetter to move the method to the other class

Page 17: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Inappropriate IntimacyInappropriate Intimacy when two classes are constantly accessing when two classes are constantly accessing

each others memberseach others members maybe members of one should be moved to maybe members of one should be moved to

the other or vice-versathe other or vice-versa maybe a new class should be created to hold maybe a new class should be created to hold

common memberscommon members

Page 18: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Message ChainsMessage Chains When a client asks for an object to get another When a client asks for an object to get another

object, which it asks to get another object, object, which it asks to get another object, which it asks to get another object... before which it asks to get another object... before finally being able to call the method it needsfinally being able to call the method it needs

Better to create a method in the first object Better to create a method in the first object that returns the data that the client needsthat returns the data that the client needs

it could navigate the chain itself or the other classes in the chain also need to be changed to give more immediate access to the required data

Page 19: MELJUN CORTES Java Lecture OOD

What is a Bad OO Program?What is a Bad OO Program?

Too many commentsToo many comments Comments are important, but maybe you’re Comments are important, but maybe you’re

writing comments because the code is not writing comments because the code is not readable in the first place?readable in the first place?

Code should be readable even without Code should be readable even without comments.comments.

Page 20: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The Once and Only Once RuleThe Once and Only Once Rule The One Responsibility RuleThe One Responsibility Rule The OO Prime DirectiveThe OO Prime Directive The Law of DemeterThe Law of Demeter Well-Defined Interfaces, Hidden Well-Defined Interfaces, Hidden

ImplementationsImplementations

Page 21: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The Once and Only Once RuleThe Once and Only Once Rule

don't copy-pastedon't copy-paste keep functionality in one spotkeep functionality in one spot avoid shotgun-surgery, changes are easieravoid shotgun-surgery, changes are easier bugs are easier to find and fixbugs are easier to find and fix

Page 22: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The One Responsibility RuleThe One Responsibility Rule

Each class/method should only do Each class/method should only do ONE ONE THINGTHING

Name of class/method should express that Name of class/method should express that one thingone thing

Long Class, Long Method and Switch Long Class, Long Method and Switch Statements are smells indicating this principle Statements are smells indicating this principle is being brokenis being broken

Also known as “Cohesiveness”Also known as “Cohesiveness”

Page 23: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The OO Prime DirectiveThe OO Prime Directive

““Never ask an object for information that you Never ask an object for information that you need to do something; rather, ask the object need to do something; rather, ask the object that has the information to do the work for that has the information to do the work for you.”you.”

- Allen Holub- Allen Holub

Page 24: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The OO Prime DirectiveThe OO Prime Directive

““Ask for help, not information.”Ask for help, not information.”

Page 25: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The OO Prime DirectiveThe OO Prime Directive

““The maintainability of a program is inversely The maintainability of a program is inversely proportional to the amount of data that flows proportional to the amount of data that flows between objects.” between objects.”

- James Gosling- James Gosling inventor of Javainventor of Java

Page 26: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The OO Prime DirectiveThe OO Prime Directive

The more one object has access to the The more one object has access to the internals of another object, the more the internals of another object, the more the program can break when one object changes.program can break when one object changes.

Limiting the flow of data makes bugs easier to Limiting the flow of data makes bugs easier to find.find.

Page 27: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The Law of DemeterThe Law of Demeter

““Each unit should have only limited Each unit should have only limited knowledge about other units: only units knowledge about other units: only units 'closely' related to the current unit.”'closely' related to the current unit.”

Page 28: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The Law of DemeterThe Law of Demeter

““Don't talk to strangers.”Don't talk to strangers.”

Page 29: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The Law of DemeterThe Law of Demeter

Minimize the number of collaborators of a Minimize the number of collaborators of a class.class.

Page 30: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? The Law of DemeterThe Law of Demeter

A method "M" of an object "O" should invoke A method "M" of an object "O" should invoke only the methods of the following kinds of only the methods of the following kinds of objects:objects:

1. itself1. itself

2. its parameters2. its parameters

3. any objects it creates/instantiates3. any objects it creates/instantiates

4. its direct component objects4. its direct component objects

Page 31: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? Well-Defined Interfaces, Hidden Well-Defined Interfaces, Hidden

ImplementationsImplementations

Page 32: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? Well-Defined Interfaces, Hidden Well-Defined Interfaces, Hidden

ImplementationsImplementations Coordination between programmers Coordination between programmers

in a team is made easier if you in a team is made easier if you decide on the interfaces of each decide on the interfaces of each class first.class first.

Each programmer is then at liberty to Each programmer is then at liberty to change the implementations of his or change the implementations of his or her classes without affecting the her classes without affecting the work of other programers.work of other programers.

Page 33: MELJUN CORTES Java Lecture OOD

How Do We Write a Good OO How Do We Write a Good OO Program?Program? Well-Defined Interfaces, Well-Defined Interfaces,

Hidden ImplementationsHidden Implementations Supports parallel development.Supports parallel development. Supports unit testing.Supports unit testing. Supports pluggability and Supports pluggability and

reuse.reuse.

Page 34: MELJUN CORTES Java Lecture OOD

Steps in Designing an OO Steps in Designing an OO ProgramProgram1.1. Understand the Understand the business requirementsbusiness requirements

2.2. Model the business domainModel the business domain

3.3. Define the layers of your applicationDefine the layers of your application• Usually three: Presentation, Business & Usually three: Presentation, Business &

IntegrationIntegration

4.4. Define the interfaces of the classes you Define the interfaces of the classes you need to implement each requirementneed to implement each requirement

5.5. Review and repeat as necessaryReview and repeat as necessary