0% found this document useful (0 votes)
35 views

Lecture 1

This document discusses object-oriented programming concepts like classes, encapsulation, inheritance, and polymorphism. It provides an example of modeling a calculator application using these concepts, with classes like Control, Container, Button, and Form. It also covers principles of cohesion and coupling, explaining that high cohesion and loose coupling are ideal, as they reduce complexity and improve maintainability. Cohesion means classes have strongly related functionality, while coupling refers to interdependence between modules.

Uploaded by

Humza Zahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
35 views

Lecture 1

This document discusses object-oriented programming concepts like classes, encapsulation, inheritance, and polymorphism. It provides an example of modeling a calculator application using these concepts, with classes like Control, Container, Button, and Form. It also covers principles of cohesion and coupling, explaining that high cohesion and loose coupling are ideal, as they reduce complexity and improve maintainability. Cohesion means classes have strongly related functionality, while coupling refers to interdependence between modules.

Uploaded by

Humza Zahid
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 31

Lecture # 1

MUST KNOW ABOUT


• Basics for Programming
• Object Oriented Programming
• Classes
• Encapsulation
• Polymorphism
• Composition
• Inheritance
• Aggregation
What is Visual Programming?

Visual programming is
the creation of a
computer program by
utilizing pictorial
elements.
Elements
Form

Textbox

Button

Label

Listbox
Fundamental Principles of OOP
Fundamental Principles of OOP

• Inheritance
• Inherit members from parent class
• Abstraction
• Define and execute abstract actions
• Encapsulation
• Hide the internals of a class
• Polymorphism
• Access a class through its parent interface

11
Polymorphism
Real World Example: Calculator
• Creating an application like the Windows Calculator
• Typical scenario for applying the object-oriented approach

13
Real World Example: Calculator (2)

• The calculator consists of controls:


• Buttons, panels, text boxes, menus, check boxes, radio buttons, etc.
• Class Control – the root of our OO hierarchy
• All controls can be painted on the screen
• Should implement an interface IPaintable with a method Paint()
• Common properties: location, size, text, face color, font, background color,
etc.

14
Real World Example: Calculator (3)

• Some controls could contain other (nested) controls inside (e. g. panels
and toolbars)
• We should have class Container that extends Control holding a collection of
child controls
• The Calculator itself is a Form
• Form is a special kind of Container
• Contains also border, title (text derived from Control), icon and system
buttons
• How the Calculator paints itself?
• Invokes Paint() for all child controls inside it

15
Real World Example: Calculator (4)

• How a Container paints itself?


• Invokes Paint() for all controls inside it
• Each control knows how to visualize itself
• What is the common between buttons, check boxes and radio
buttons?
• Can be pressed
• Can be selected
• We can define class AbstractButton and all buttons can derive
from it

16
Calculator Classes
«interface»
IPaintable
Paint()

Control

-location
-size
-text
-bgColor
-faceColor
-font

Container AbstractButton TextBox MainMenu MenuItem

Panel Form Button CheckBox RadioButton

Calculator
17
Cohesion
• Cohesion describes how closely all the routines in a class or all the
code in a routine support a central purpose
• Cohesion must be strong
• Well-defined abstractions keep cohesion strong
• Classes must contain strongly related functionality and aim for single
purpose
• Cohesion is a useful tool for managing complexity

18
Cohesion
• In object-oriented programming, if the methods that serve a class
tend to be similar in many aspects, then the class is said to have high
cohesion.
• In a highly cohesive system, code readability and reusability is
increased, while complexity is kept manageable.
How to increase Cohesion
• Cohesion is increased if:

• The functionalities embedded in a class, accessed through its methods, have


much in common.
• Methods carry out a small number of related activities, by avoiding coarsely
grained or unrelated sets of data.
Advantages - Cohesion
• Reduced module complexity
• Because they are simpler, having fewer operations
• Increased system maintainability,
• Because logical changes in the domain affect fewer modules, and because
changes in one module require fewer changes in other modules
• Increased module reusability
• Because application developers will find the component they need more
easily among the cohesive set of operations provided by the module.
Good and Bad Cohesion
• Good: hard disk, cdrom, floppy

• BAD: spaghetti code

22
Strong Cohesion
• Strong cohesion example
• Class Math that has methods:
Sin(), Cos(), Asin()
Sqrt(), Pow(), Exp()
Math.PI, Math.E
double sideA = 40, sideB = 69;
double angleAB = Math.PI / 3;

double sideC =
Math.Pow(sideA, 2) + Math.Pow(sideB, 2)
- 2 * sideA * sideB * Math.Cos(angleAB);

double sidesSqrtSum = Math.Sqrt(sideA) +


Math.Sqrt(sideB) + Math.Sqrt(sideC);
23
Bad Cohesion
• Bad cohesion example
• Class Magic that has these methods:
MagicClass.MakePizza("Fat Pepperoni");
MagicClass.WithdrawMoney("999e6");
MagicClass.OpenDBConnection();

• Another example
public void PrintDocument(Document d);
public void SendEmail(
string recipient, string subject, string text);
public void CalculateDistanceBetweenPoints(
int x1, int y1, int x2, int y2)
24
Another Example – Good and bad
cohesion
• You have a class that adds two numbers, but the same class creates a
window displaying the result. This is a low cohesive class because the
window and the adding operation don't have much in common. The
window is the visual part of the program and the adding function is
the logic behind it.

• To create a high cohesive solution, you would have to create a class


Window and a class Sum. The window will call Sum's method to get
the result and display it. This way you will develop separately the logic
and the GUI of your application.
Summary - Cohesion
• High cohesion is when you have a class that does a well defined job.
Low cohesion is when a class does a lot of jobs that don't have much
in common.
Coupling
• Coupling describes how tightly a class or routine is related to other
classes or routines
• Coupling must be kept loose
• Modules must depend little on each other
• All classes and routines must have small, direct, visible, and flexible relations
to other classes and routines
• One module must be easily used by other modules

27
Coupling
• In software engineering, coupling is the degree of interdependence
between software modules; a measure of how closely connected two
routines or modules are
Loose and Tight Coupling
• Loose Coupling:
• Easily replace old HDD
• Easily place this HDD to another motherboard

• Tight Coupling:
• Where is the video adapter?
• Can you change the video controller?

29
Spaghetti Code
• Combination of bad cohesion and tight coupling:
class Report
{
public void Print() {…}
public void InitPrinter() {…}
public void LoadPrinterDriver(string fileName) {…}
public bool SaveReport(string fileName) {…}
public void SetPrinter(string printer) {…}
}
class Printer
{
public void SetFileName() {…}
public static bool LoadReport() {…}
public static bool CheckReport() {…}
}

30
Summary
• OOP fundamental principals are: inheritance, encapsulation, abstraction,
polymorphism
• Inheritance allows inheriting members from another class
• Abstraction and encapsulation hide internal data and allow working through abstract
interface
• Polymorphism allows working with objects through their parent interface and invoke abstract
actions
• Strong cohesion and loose coupling avoid spaghetti code

31

You might also like