Namespaces, Cohesion and Coupling Veselin Georgiev National Academy for Software Development academy.devbg.org Svetlin Nakov Telerik Corporation www.telerik.com.

Slides:



Advertisements
Similar presentations
Classes, Constructors, Properties, Events, Static Members, Interfaces, Inheritance, Polymorphism Svetlin Nakov Telerik Corporation
Advertisements

Revealing the Secrets of Self-Documenting Code Svetlin Nakov Telerik Corporation For C# Developers.
IMPLEMENTING CLASSES Chapter 3. Black Box  Something that magically does its thing!  You know what it does but not how.  You really don’t care how.
Svetlin Nakov Telerik Corporation
“The task of leadership is not to put greatness into people, but to elicit it, for the greatness is there already.” – John Buchan Thought for the Day.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
1 Chapter 7 Inheritance, Polymorphism, and Scope.
Criteria for good design. aim to appreciate the proper and improper uses of inheritance and appreciate the concepts of coupling and cohesion.
Data Abstraction and Object- Oriented Programming CS351 – Programming Paradigms.
Computer Science 240 Principles of Software Design.
Classes, Constructors, Properties, Events, Static Members, Interfaces, Inheritance, Polymorphism Technical Trainer Telerik Corporation
1.11 Introduction to OOP academy.zariba.com 1. Lecture Content 1.What is OOP and why use it? 2.Classes and objects 3.Static classes 4.Properties, fields.
2.5 OOP Principles Part 2 academy.zariba.com 1. Lecture Content 1.Polymorphism 2.Cohesion 3.Coupling 2.
Svetlin Nakov Telerik Corporation
Programming Languages and Paradigms Object-Oriented Programming.
Module 7: Essentials of Object-Oriented Programming.
An Object-Oriented Approach to Programming Logic and Design
C# Programming Fundamentals of Object-Oriented Programming Fundamentals of Object-Oriented Programming Introducing Microsoft.NET Introducing Microsoft.NET.
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Advanced Concepts Svetlin Nakov Telerik Corporation
Object-Oriented Programming in C++
Inheritance Extending Class Functionality. Polymorphism Review Earlier in the course, we examined the topic of polymorphism. Many times in coding, we.
Inheritance, Abstraction, Encapsulation, Polymorphism
High-Quality Classes and Class Hierarchies Best Practices in Object-Oriented Design SoftUni Team Technical Trainers Software University
Improving the Quality of Existing Code Svetlin Nakov Telerik Corporation
Using the Standard.NET Framework Classes Svetlin Nakov Telerik Corporation
Encapsulation, Inheritance & Polymorphism. OOP Properties Encapsulation ­The process of creating programs so that information within a class is not accessible.
Code Correctness, Readability, Maintainability Svetlin Nakov Telerik Software Academy academy.telerik.com Technical Trainer
Session 08 Module 14: Generics and Iterator Module 15: Anonymous & partial class & Nullable type.
Encapsulation and Polymorphism Encapsulation, Polymorphism, Class Hierarchies, Cohesion and Coupling SoftUni Team Technical Trainers Software University.
Copyright ©2004 Virtusa Corporation | CONFIDENTIAL.Net Assignments K. R. C. Wijesinghe Trainer Virtusa Corporation.
Best Practices in the Object-Oriented Design Learning & Development Telerik Software Academy.
Static Members and Namespaces Static Members, Indexers, Operators, Namespaces SoftUni Team Technical Trainers Software University
Svetlin Nakov Telerik Software Academy Manager Technical Trainer
Session 02 and 03: C# OOP 1 OOP in C#: Classes and Objects in C#. Object-Oriented Design. UML Class Diagram. Object Interaction. FEN AK IT:
Programming with Java © 2002 The McGraw-Hill Companies, Inc. All rights reserved. 1 McGraw-Hill/Irwin Chapter 5 Creating Classes.
Inheritance. Inheritance - Introduction Idea behind is to create new classes that are built on existing classes – you reuse the methods and fields and.
Classes, Constructors, Properties, Events, Static Members, Interfaces, Inheritance, Polymorphism Doncho Minkov Telerik School Academy schoolacademy.telerik.com.
COM S 228 Introduction to Data Structures Instructor: Ying Cai Department of Computer Science Iowa State University Office: Atanasoff.
High-Quality Programming Code Code Correctness, Readability, Maintainability Svetlin Nakov Technical Trainer Software University
Introduction To OOP 1.0 Fundamentals Of Java Programming Language 2.0 Exception Handling 3.0 Classes, Inheritance And Polymorphism © 2011 | PN AZRINA.
Polymorphism, Class Hierarchies, Exceptions, Strong Cohesion and Loose Coupling Telerik Software Academy Object-Oriented Programming.
Inheritance and Class Hierarchies Chapter 3. Chapter 3: Inheritance and Class Hierarchies2 Chapter Objectives To understand inheritance and how it facilitates.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
Author: DoanNX Time: 45’.  OOP concepts  OOP in Java.
POLYMORPHISM Chapter 6. Chapter Polymorphism  Polymorphism concept  Abstract classes and methods  Method overriding  Concrete sub classes and.
Lecture 12 Implementation Issues with Polymorphism.
Polymorphism, Class Hierarchies, Exceptions, Strong Cohesion and Loose Coupling.
Best Practices in the Object-Oriented Design Vesko Kolev Telerik Corporation
OOP in C# - part 1 OOP in C#: –Object Interaction. –Inheritance and Polymorphism (next module). FEN 20121UCN Technology: Computer Science.
C# Fundamentals An Introduction. Before we begin How to get started writing C# – Quick tour of the dev. Environment – The current C# version is 5.0 –
 Description of Inheritance  Base Class Object  Subclass, Subtype, and Substitutability  Forms of Inheritance  Modifiers and Inheritance  The Benefits.
Object means a real word entity such as pen, chair, table etc. Object-Oriented Programming is a methodology or paradigm to design a program using classes.
Static Members, Structures, Enumerations, Generic Classes, Namespaces Telerik Software Academy Object-Oriented Programming.
Chapter 12: Support for Object- Oriented Programming Lecture # 18.
High-Quality Programming Code Code Correctness, Readability, Maintainability, Testability, Etc. SoftUni Team Technical Trainers Software University
Encapsulation and Polymorphism
Static Members and Namespaces
Abstract Classes, Abstract Methods, Override Methods
Creating and Using Objects, Exceptions, Strings
Object-Oriented Programming Concepts
Inheritance and Polymorphism
Packages, Interfaces & Exception Handling
Object Oriented Analysis and Design
Using Classes and Objects
Java Programming Language
Interfaces.
By Rajanikanth B OOP Concepts By Rajanikanth B
Chengyu Sun California State University, Los Angeles
Presentation transcript:

Namespaces, Cohesion and Coupling Veselin Georgiev National Academy for Software Development academy.devbg.org Svetlin Nakov Telerik Corporation

1. Cohesion and Coupling 2. Inheritance 3. Polymorphism 4. Namespaces 2

 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  Classes must contain strongly related functionality and aim for single purpose  Cohesion is a useful tool for managing complexity  Well-defined abstractions keep cohesion strong 4

 Good cohesion: hard disk, CD-ROM, floppy  BAD: spaghetti code 5

 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) Math.Pow(sideA, 2) + Math.Pow(sideB, 2) - 2 * sideA * sideB * Math.Cos(angleAB); - 2 * sideA * sideB * Math.Cos(angleAB); double sidesSqrtSum = Math.Sqrt(sideA) + Math.Sqrt(sideB) + Math.Sqrt(sideC); 6

 Example of bad cohesion  Class Magic that has all these methods:  Another example: MagicClass.MakePizza("Fat Pepperoni"); MagicClass.WithdrawMoney("999e6");MagicClass.OpenDBConnection(); public void PrintDocument(Document d); public void Send (string recipient, string subject, string text); public void CalculateDistanceBetweenPoints(int x1, int y1, int x2, int y2) 7

 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 8

 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? 9

class Report { public bool LoadFromFile(string fileName) {…} public bool LoadFromFile(string fileName) {…} public bool SaveToFile(string fileName) {…} public bool SaveToFile(string fileName) {…}} class Printer { public static int Print(Report report) {…} public static int Print(Report report) {…}} class Program { static void Main() static void Main() { Report myReport = new Report(); Report myReport = new Report(); myReport.LoadFromFile("C:\\DailyReport.rep"); myReport.LoadFromFile("C:\\DailyReport.rep"); Printer.Print(myReport); Printer.Print(myReport); }} 10

class MathParams { public static double operand; public static double operand; public static double result; public static double result;} class MathUtil { public static void Sqrt() public static void Sqrt() { MathParams.result = CalcSqrt(MathParams.operand); MathParams.result = CalcSqrt(MathParams.operand); }} class Example { static void Main() static void Main() { MathParams.operand = 64; MathParams.operand = 64; MathUtil.Sqrt(); MathUtil.Sqrt(); Console.WriteLine(MathParams.result); Console.WriteLine(MathParams.result); }} 11

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

 Inheritance is the ability of a class to implicitly gain all members from another class  Inheritance is fundamental concept in OOP  The class whose methods are inherited is called base (parent) class  The class that gains new functionality is called derived (child) class 14

 All class members are inherited  Fields, methods, properties, …  In C# classes could be inherited  The structures in C# could not be inherited  Inheritance allows creating deep inheritance hierarchies  In.NET there is no multiple inheritance, except when implementing interfaces 15

Live Demo

 Polymorphism is fundamental concept in OOP  The ability to handle the objects of a specific class as instances of its parent class and to call abstract functionality  Polymorphism allows creating hierarchies with more valuable logical structure  Allows invoking abstract functionality without caring how and where it is implemented 18

 Polymorphism is usually implemented through:  Virtual methods ( virtual )  Abstract methods ( abstract )  Methods from an interface ( interface )  In C# to override virtual method the keyword override is used  C# allows hiding virtual methods in derived classes by the keyword new 19

class Person { public virtual void PrintName() public virtual void PrintName() { Console.WriteLine("I am a person."); Console.WriteLine("I am a person."); }} class Trainer : Person { public override void PrintName() public override void PrintName() { Console.WriteLine("I am a trainer."); Console.WriteLine("I am a trainer."); }} class Student : Person { public override void PrintName() public override void PrintName() { Console.WriteLine("I am a student."); Console.WriteLine("I am a student."); }} 20

static void Main() { Person person = new Person(); Person person = new Person(); person.PrintName(); person.PrintName(); // I am a person. // I am a person. Person trainer = new Trainer(); Person trainer = new Trainer(); trainer.PrintName(); trainer.PrintName(); // I am a trainer. // I am a trainer. Person student = new Student(); Person student = new Student(); student.PrintName(); student.PrintName(); // I am a student. // I am a student.} 21

Live Demo

 Similar to namespaces in C++ and packages in Java  Ensure logical grouping of type definition aggregations  May contain classes, structures, interfaces, enumerators and other namespaces  Can not contain methods and data  Allows definition of types with equal names (they must be in different namespaces)  Can be allocated in one or several files 24

 To include a namespace – using directive is used  using allows direct use of all types in the namespace  Including is applied to the current file  The directive is written at the begging of the file  When includes a namespace with using its subset of namespaces is not included using System.Windows.Forms; 25

 Types, placed in namespaces, can be used and without using directive, by their full name:  using can create allies for namespaces : using IO = System.IO; using WinForms = System.Windows.Forms; IO.StreamReader reader = IO.File.OpenText("file.txt"); IO.File.OpenText("file.txt"); WinForms.Form form = new WinForms.Form(); System.IO.StreamReader reader = System.IO.File.OpenText("file.txt"); System.IO.File.OpenText("file.txt"); 26

 Divide the types in your applications in namespaces always when they are too much (above 15-20)  Classify the types logically in namespaces according to their purpose  If the types are too much use subsets of namespaces 27

 Distribute all public types in files identical with their names  Arrange the files in directories, corresponding to their namespaces  The directory structure from your project course-code have to reflect the structure of the defined namespaces 28

namespace SofiaUniversity.Data { public struct Faculty public struct Faculty { //... //... } public class Student public class Student { //... //... } public class Professor public class Professor { //... //... } public enum Specialty public enum Specialty { //... //... }}

namespace SofiaUniversity.UI { public class StudentAdminForm : System.Windows.Forms.Form public class StudentAdminForm : System.Windows.Forms.Form { //... //... } public class ProfessorAdminForm : System.Windows.Forms.Form public class ProfessorAdminForm : System.Windows.Forms.Form { //... //... }} namespace SofiaUniversity { public class AdministrationSystem public class AdministrationSystem { public static void Main() public static void Main() { //... //... } }}

 Recommended directory structure and classes organization in them 31

Questions?

1. A bank holds different types of accounts for its customers: deposit accounts, loan accounts and mortgage accounts. Customers could be physical persons or companies. All accounts have customer, balance and interest rate (monthly based). Deposit accounts are allowed to deposit and withdraw money. Loan and mortgage accounts can only deposit money. All accounts have customer, balance and interest rate (monthly based). Deposit accounts are allowed to deposit and withdraw money. Loan and mortgage accounts can only deposit money. All customers have name. Persons have social security number and monthly income. Companies have Bulstat. All customers have name. Persons have social security number and monthly income. Companies have Bulstat. 33

All accounts can calculate their interest amount for a given period (in months). In the common case it is calculated as follows: numberOfMonths * interestRate. Loan accounts have no interest for the first 3 months if are held by persons and for the first 2 months if are held by a company. Deposit accounts have no interest if their balance is positive and less than

Mortgage accounts have ½ interest for the first 12 months for companies and no interest for the first 6 months for persons. Your task is to write a program to model the bank system. You should identify the classes, base classes and abstract actions and implement the calculation of the interest for the different accounts. 35