68
1 http://simply-the-test.blogspot.in/2009 /12/eat-it.html

Object oriented design part-1

Embed Size (px)

DESCRIPTION

One of the internal training session presentation I gave in my current compnay

Citation preview

Page 1: Object oriented design part-1

1

http://simply-the-test.blogspot.in/2009/12/eat-it.html

Page 2: Object oriented design part-1

2

No matter how far down the wrong road you’ve gone, turn back.

- Turkish proverb

Page 3: Object oriented design part-1

Object-Oriented DesignPart-I

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 3

Presented by Ashok GuduruTechnical Architect

Page 4: Object oriented design part-1

Object-Oriented Design

• How many people here write object-oriented code?

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 4

Page 5: Object oriented design part-1

Object-Oriented Design

• How many people think they are writing object-oriented code but write procedural code?

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 5

Page 6: Object oriented design part-1

6

It works on my machine syndrome!

http://simply-the-test.blogspot.in/2010/05/it-works-on-my-machine.html

Page 7: Object oriented design part-1

Object-Oriented Design

A Use Case:Imagine that we have a system that manages room temperatures by applying heat to the rooms when necessary.

The basic business, as explained by our expert, is that each room has a desired temperature and a current temperature. If the current temperature is lower than the desired temperature, then heat must be pumped into the room.

However, if there is no one in the room, then the temperature can be left to fall 10 degrees below the desired temperature.

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 7

Page 8: Object oriented design part-1

Object-Oriented Design

Procedural Approach:

We create a HeatFlowRegulator program that gets the: - desired temperature for a given room ID, - the current temperature for a given room ID, and then a - boolean for whether or not someone is in the room.

With this data the HeatFlowRegulator program figures out if heat should be applied to the room with the given room ID.

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 8

Page 9: Object oriented design part-1

Object-Oriented Design

Object-Based Approach:

• We create a HeatFlowRegulator program that gets a Room object. The HeatFlowRegulator program asks the object for its current temp, desired temp and whether or not someone is in the room.

• With this data the HeatFlowRegulator program figures out if heat should be applied to the room.

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 9

Page 10: Object oriented design part-1

Object-Oriented Design

Object-Oriented Approach:

• We create a HeatFlowRegulator program that gets a Room object. The HeatFlowRegulator program asks the Room if it needs heat.

Note: The aim of this example is to help you to see the difference

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 10

Page 11: Object oriented design part-1

Object-Oriented Design

What is Object-Oriented Design:

Object-oriented design is a method of design encompassing the process of object-orienteddecomposition and a notation for depicting both logical and physical as well as static and dynamicmodels of the system under design.

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 11

Page 12: Object oriented design part-1

Procedural code gets information then makes

decisions.

Object-Oriented code tells objects to do things.

--Alec Sharp

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 12

Page 13: Object oriented design part-1

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 13

Page 14: Object oriented design part-1

Object-Oriented Design

Why we prefer an O-O design approach when developing software?

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 14

Page 15: Object oriented design part-1

Object-Oriented Design

The number one reason to employ an OO design when building software is to reduce and manage complexity through

clarity and organization

Object-Oriented Design reduces the Complexity when done it right

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 15

Page 16: Object oriented design part-1

Object-Oriented Design

Off course! You cannot reduce the complexity of a given task beyond a certain point.

Once you've reached that point, you can only shift the burden around

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 16

Page 17: Object oriented design part-1

Object-Oriented Design

What is Object-Oriented Design?

Wikipedia: Object-oriented design is the process of planning a system of interacting objects for the purpose of solving a software problem.

It is one approach to software design.

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 17

Page 18: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 18

• Abstraction– Allows conceptualize and model the problem

domain without much details• Encapsulation– Enforces Modularity

• Inheritance– Passes "Knowledge" Down

• Polymorphism– Takes any Shape

The 4 Tenets (Characteristics or Principles) of Object-Oriented Programming

Page 19: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 19

• The ability to represent data at a very conceptual level (Model) without any concrete implementation details.

• Specifies what to do but not how to do

• The best definition is: “An abstraction denotes the essential characteristics of an object that distinguish it from all other kinds of object and thus provide crisply defined conceptual boundaries, relative to the perspective of the viewer.”

— Object-Oriented Design With Applications, by G. Booch

Abstraction:

Page 20: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 20

Abstraction:

Page 21: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 21

• Encapsulation is the hiding of data implementation by restricting access to accessors and mutators.

• Immutable: Not directly changeable without the knowledge of the object

• Object should be able to change its internal implementation without breaking the consumers

Encapsulation:

Page 22: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 22

Encapsulation:

Page 23: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 23

Modularity:

Modularity packages abstractions into discrete units.

Page 24: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 24

• Inheritance is the process by which a class can be derived from a base class with all the features of base class and some of its own.

• Inheritance is a way to establish “Is-a” relationships between classes or objects

Inheritance:

Page 25: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 25

• Polymorphism means one name, many forms. (Poly + Morph) Poly = Many, Morph = Form or Shape

• This is the ability to exist in various forms

• There are 2 basic types of polymorphism. Overriding, also called run-time polymorphism, and Overloading, which is referred to as compile-time polymorphism

Polymorphism:

Page 26: Object oriented design part-1

26

One test is worth a thousand expert opinions. - Bill Nye, The Science Guy

Page 27: Object oriented design part-1

Object-Oriented Design

Class != Object

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 27

Page 28: Object oriented design part-1

Object-Oriented Design

Class is a Template. Nothing More!

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 28

Page 29: Object oriented design part-1

Object-Oriented Design

Objects are little things and do stuff for us.

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 29

Page 30: Object oriented design part-1

Object-Oriented Design

Objects are instances. Instances dot not necessarily look like

class

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 30

Page 31: Object oriented design part-1

Object-Oriented Design

A good example of a Business Object:

Public class Blender{ _Jug _Base Public Juice Blend (Ingredients)}

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 31

Page 32: Object oriented design part-1

Object-Oriented Design

Method Call = Message

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 32

Message/Method is a Pipeline. Using with you can get the object do the required action

Page 33: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 33

• Object contains:– Identification– State (Data)– Behavior (Operations and Capabilities)• Capabilities: Flyable, Enumerable, Printable, Drinkable, Shootable, Rotatable, Readable, Writable, Serializable, Observable, Groupable etc.)

What is Object?Objects are usually instances of classes, are used to interact with one another to design applications.

Page 34: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 34

What is Object?

An object has state, exhibits some well-defined behavior, and has a unique identity.

Page 35: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 35

• Objects are about behavior (exchanging messages by method calls)

• Objects plays Roles. Roles will have Responsibilities

• Objects have Capabilities (to do things like e.g. Walk, Run, Execute) and Collaborate with other objects to do the things

• Roles and Capabilities are implemented using the interfaces (and not by inheritance. You use Interfaces to implement object's capabilities)

What is Object?

Page 36: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 36

• Objects are NOT data. Objects are NOT data clumps/buckets either

• An object without a Behavior (i.e., Only State (Data)) is known as Anemic Domain Model

What is Object?

Page 37: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 37

• Objects are always reference types (in C# or Java)

• Whenever you check for object's equality. it will use its system generated identity (that is in the memory). The contents are not used rather the address of the memory is used.

What is Object?

Page 38: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 38

• Object-Oriented is a Mental Model of the Business. Highest level of Abstraction of the Business

• Objects are principally about people and their mental models—not polymorphism, coupling and cohesion

What is Object?

Page 39: Object oriented design part-1

39

Start with the hardest. Always tackle the most difficult problems first, and leave the simple one towards the end.

Page 40: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 40

• "Is a" (which is Inheritance)• "Has a“ (which is Composition)

• Always favor Composition (delegation) over Inheritance.

• Use Composition instead of Inheritance.

• Inheritance creates the strongest form of coupling

• An Employee is a Person hence you use inheritance which leads to dependency and increases complexity.

Object Relationships:

Page 41: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 41

• Is a– e.g. • Apple is a Fruit • Employee is a Person• Car is Vehicle

– Based on Inheritance• Class Inheritance or Interface Inheritance

– Inheritance is unidirectional• e.g. House is a Building. But Building is not a House

Object Relationships:

Page 42: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 42

• Has a (Composition)– e.g.

• Car has a Engine, Has 4 Tires (Aggregation)• Car has a Chassis (Composition)• House has Bathroom

– Means use of instance variables that are references to other objects.

Object Relationships:

filled diamond

Composition

unfilled diamond Aggregation

Page 43: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 43

• Is a kind of• Part of/Made Up of/Composition• Containment/Aggregation• Associated With• Helps Describe/Identifies• Feature of/Implementation• Uses

More Object Relationships

Page 44: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 44

• Is a kind of – A Car is a kind of Vehicle, An Orange is a kind of Fruit

– Implies inheritance

– Typically this relationship is called is-a, however that is just not as clear. Using is-a often results in a poor model with things like a Boss is-a Employee which leads to roles being implemented via inheritance.

Object Relationships:

Page 45: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 45

• Part Of / Made Up Of /Composition – A page is part of a book

– A book is made up of / composed of a Cover and Pages

– Implies that the part does not exist outside of the whole. If you destroy a book, you destroy the cover and pages.

– Part-of relationships suggest that internal objects may be best left hidden and accessed via methods on the parent

– myBook.TurnPage() instead of myBook.Pages.TurnPage()

Object Relationships:

Page 46: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 46

• Containment / Aggregation – Point of Interests and Paths are contained in a Map

– One object acts as a container, grouping other objects together for a purpose. The parts are not dependent on the whole. If we destroy a Map, the Points of Interest are not destroyed.

Object Relationships:

Page 47: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 47

• Associated With – An author is associated with a book and vice versa

– Associated With relationships suggest that the related object is not owned by the parent like the part of relationship, and is often accessible via navigation through the domain model

– myBook.GetAuthors()– myRaceCar.GetDriver()

Object Relationships:

Page 48: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 48

• Helps Describe / Identifies - A Title helps describe a Book

– Implies that the object is inherent to the parent object and there is often less danger in exposing the object outside of the parent

Object Relationships:

Page 49: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 49

• Feature of / Implementation - Features of a Book include its Width and Height

– Unlike an object that helps describe or identify another object, a feature of an object is one that should rarely if ever be exposed

– Business logic is naturally written around features of objects. In fact, if an implementation-related object is not referenced in the business logic of the parent object we should consider removing the object

Object Relationships:

Page 50: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 50

• Uses – A Book uses the Pages to get the number of times a word appears in the book

– Clarifies collaboration among objects

– The uses relationships is often another relationship aside from how two objects might be related (Part of, Associated with, etc).

Object Relationships:

Page 51: Object oriented design part-1

51http://simply-the-test.blogspot.in/2012/07/about-bugs-and-monsters.html

Page 52: Object oriented design part-1

52

Blame doesn’t fix bugs. Instead of pointing fingers, point to possible solutions. It’s the positive outcome that counts.

Page 53: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 53

• Create an object that is an attribute of another object without any identity relative to the domain and that can be interchangeable between objects that use the same attribute.

• You have a Person and a Person has an Address. You have a Company and a Company has an address. The database may have a PersonAddress table and a CompanyAddress table. However, there is no need to create a PersonAddress object and a Company Address object.

Value Object (VO):

Page 54: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 54

• Value Objects are most important: VO are probably the most important objects you guys should create

• Are tiny, highly focused, immutable objects

• Are always value type. If you need to compare two VO’s (for Equality), the contents will decide its equality

Value Object (VO):

Page 55: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 55

• VOs can your improve your design, reusability and code quality

• e.g. Money, Address, Range, Quantity, Size, Date, Point, Identity (Id), Key and Enums

• Ideally there never be any objects of type ints, longs, strings, doubles, decimals in real business.

Value Object (VO):

Page 56: Object oriented design part-1

Object-Oriented Design

Value Object (VO): Quantity

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 56

Quantity-amount: Number

+,-,*,/<,>, =To(unit): quantityToString()Parse(String): quantityEquals() bool

-unit: Unit

Page 57: Object oriented design part-1

Object-Oriented DesignMoney Class…

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 57

public class Money: IComparable {

private double amount;private Currency currency;

public double amount() {}

public Currency currency() {}

public Money(double amount, Currency currency) {}

public Money add(Money arg) {}

public Money subtract (Money arg) {}

public bool equals(Object arg) {}

public int compareTo (Object arg) {}

}

Page 58: Object oriented design part-1

58

Don’t fall for the quick hack. Invest the energy to keep code clean and out in the open.

Page 59: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 59

• Separates the commands (actions) from queries (asking for information). When designing the objects, stick to and make sure the following principles applied.

• Command: a Void method. Does something and can optionally mutate the object’s state (data)

• Query: Always returns some value (answers to your query). Never allow mutating the state (data)

Command Query Segregation (CQS):

Page 60: Object oriented design part-1

Next …

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 60

• Business Object• Business Object Collection• Candidate Object• Entity Object• Factory Object• Identity Object• Key Object• Manager Object• Service Object• Value Object• Data Transfer Object (POCO/POJO)

Many Kinds of Objects:

Page 61: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 61

Model-View-Controller (MVC) Pattern• MVC is Invented by Trygve Reenskaug

• It is NOT a UI Pattern: It is an Architectural pattern.

• MVC is more about mental model and we conceptualize the system with it.

• It is mental model and user’s view of the system

• We see this pattern time and time again in several places. It has good Separation of Concerns (SoC)

Page 62: Object oriented design part-1

Object-Oriented Design

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 62

Model-View-Controller (MVC) Pattern

The goal of the Controller is to coordinate multiple views

Page 63: Object oriented design part-1

Next Time…

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 63

• Rigidity• Fragility• Inseparability• Viscosity

Symptoms of Bad Software

Page 64: Object oriented design part-1

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 64

http://simply-the-test.blogspot.in/2010/04/different-goals.html

Page 65: Object oriented design part-1

When a tester didn't find any cool bugs, then he basically has two options to think about.

a) The software is good enough to go liveb) The software wasn't tested well enough

Typically, the developer thinks it is "a". And, of course, the tester always fears it could be option "b".

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 65

http://simply-the-test.blogspot.in/2010/04/different-goals.html

Page 66: Object oriented design part-1

66

Learn the new; unlearn the old. When learning a new technology, unlearn any old habits that might hold you back.

Page 67: Object oriented design part-1

References

All the illustrations, images, and quotes are taken from the following books and web sites:

1. Object-Oriented Analysis and Design with Applications by Grady Booch2. Practices of an Agile Developer by Venkat Subramaniam3. Zelger’s Blog: http://www.zelger.org (http://simply-the-test.blogspot.in)4. Martin Fowler Web Site: http://martinfowler.com

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 67

Page 68: Object oriented design part-1

Thank you!

You have to experience what you want to express

-- Vincent van Gogh

Copyright © 2013 Coextrix Technologies Pvt. Ltd., Bangalore 68