Lecture 1
Lecture 1
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)
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)
16
Calculator Classes
«interface»
IPaintable
Paint()
Control
-location
-size
-text
-bgColor
-faceColor
-font
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:
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);
• 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.
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