Presentation is loading. Please wait.

Presentation is loading. Please wait.

Why OO Paradigm is better?

Similar presentations


Presentation on theme: "Why OO Paradigm is better?"— Presentation transcript:

1 Why OO Paradigm is better?
2/25/2019 Why OO is better

2 Problem Consider the following problem: A philatelist owns a number of stamps, which can be divided into group I and group II. Each stamp has a name, face value, and publication year. Also, each stamp has a commercial value: the commercial value of group I is equal to the face value while the commercial value of group II is twice as much as the face value. So write a program to calculate the commercial value of all stamps owned by a philatelist. 2/25/2019 Why OO is better

3 Your Client Code After you finish the previous program, write a client to model the following scenario: John is a philatelist owing the following stamp (name, face value, group, pub year): Shot Put, 75c, II, 1984 Men’s Gym, 100c, II, 1984 Weight Lift, 150c, II, 1984 Niagara Falss, 80c, I, 1999 Acadia Maine, 60c, I , 2000 Yosemite, 84c, I, 2001 Mile Woods, 72c, I, 2007 2/25/2019 Why OO is better

4 Example John owns 7 stamps: group I and group II. How to calculate the commercial value of the stamps owned by John. struct stamp { float face_value; int group} a[7]; float com_value () { for (i=0;i<7,i++) if a[i].group==1 total += a[i].face_value; else total += 2 * a[i].face_value; } Program Domain 2/25/2019 Why OO is better

5 OO Solution Class Philatelist { private float stamp[7];
public float com_value () { for (i=0;i<7,i++) if a[i].group==1 total += a[i].face_value; else total+=2*a[i].face_value; } Class Philatelist { private Stamp stamp[7]; public float com_value () { for (i=0;i<7,i++) total+=a[i].com_value(); } Stamp float face_value float com_value() GroupI_Stamp GroupII_Stamp 2/25/2019 Why OO is better

6 OO Design Principles 2/25/2019 Why OO is better

7 Principle The Open-Closed Principle:
Software Entities Should be Open For Extension, Yet Closed For Modification 2/25/2019 Why OO is better

8 Open-Closed Principle
The open-closed principle (OCP) says that we should attempt to design modules that never need to be changed. To extend the behavior of the system, we need to add code. We do not modify old code Modules that conform to OCP meet two criteria: Open for Extension – the behavior of the module can be extended to meet new requirements Closed for modification – the source code of the module is not allowed to change How can we do this Inheritance Abstraction Polymorphism Interface 2/25/2019 Why OO is better

9 Open-Closed Principle
It is not possible to have all the methods of a software system satisfy the OCP, but we should minimize the number of modules that do not satisfy it. The OCP is really the heart of OO design Conformance to this principle yields the greatest of reusability and maintainability. 2/25/2019 Why OO is better

10 OCP Example In the Philatelist example, we use polymorphism to calculate the commercial value: Class Philatelist { private Stamp stamp[7]; public float com_value () { for (i=0;i<7,i++) total+=a[i].com_value(); } 2/25/2019 Why OO is better

11 OCP Example Calculation in Class Philatelist becomes flexible if we use inheritance to represent the different categories of a stamp. Stamp float com_value() GroupI_Stamp GroupIII_Stamp GroupII_Stamp 2/25/2019 Why OO is better


Download ppt "Why OO Paradigm is better?"

Similar presentations


Ads by Google