Translate
Wednesday, 24 December 2014
Monday, 22 December 2014
what is the difference between loose coupling and tight coupling in object oriented paradigm?
Tight coupling is when a group of classes are highly dependent on one another.
This scenario arises when a class assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class.
Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns.
A loosely-coupled class can be consumed and tested independently of other (concrete) classes.
Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces rather than other concrete classes, and any class can be on the other end of that communication simply by implementing the interface.
Example of tight coupling:
class CustomerRepository
{
private readonly Database database;
public CustomerRepository(Database database)
{
this.database = database;
}
public void Add(string CustomerName)
{
database.AddRow("Customer", CustomerName);
}
}
class Database
{
public void AddRow(string Table, string Value)
{
}
}
Example of loose coupling:
class CustomerRepository
{
private readonly IDatabase database;
public CustomerRepository(IDatabase database)
{
this.database = database;
}
public void Add(string CustomerName)
{
database.AddRow("Customer", CustomerName);
}
}
interface IDatabase
{
void AddRow(string Table, string Value);
}
class Database : IDatabase
{
public void AddRow(string Table, string Value)
{
}
}
This scenario arises when a class assumes too many responsibilities, or when one concern is spread over many classes rather than having its own class.
Loose coupling is achieved by means of a design that promotes single-responsibility and separation of concerns.
A loosely-coupled class can be consumed and tested independently of other (concrete) classes.
Interfaces are a powerful tool to use for decoupling. Classes can communicate through interfaces rather than other concrete classes, and any class can be on the other end of that communication simply by implementing the interface.
Example of tight coupling:
class CustomerRepository
{
private readonly Database database;
public CustomerRepository(Database database)
{
this.database = database;
}
public void Add(string CustomerName)
{
database.AddRow("Customer", CustomerName);
}
}
class Database
{
public void AddRow(string Table, string Value)
{
}
}
Example of loose coupling:
class CustomerRepository
{
private readonly IDatabase database;
public CustomerRepository(IDatabase database)
{
this.database = database;
}
public void Add(string CustomerName)
{
database.AddRow("Customer", CustomerName);
}
}
interface IDatabase
{
void AddRow(string Table, string Value);
}
class Database : IDatabase
{
public void AddRow(string Table, string Value)
{
}
}
Wednesday, 17 December 2014
Class Properties example in C#
Create
a reference type called Person.
Populate the Person class
with the following properties to store the following information:
-
First name
-
Last name
-
Email address
-
Date of birth
Add
constructors that accept the following parameter lists:
-
All four parameters
-
First, Last, Email
-
First, Last, Date of birth
Add
read-only properties that return the following computed information:
-
Adult - whether or not the person is over 18
-
Sun sign - the traditional western sun sign of this person
-
Birthday - whether or not today is the person's birthday
using System;
using
System.Collections.Generic;
using
System.Text;
namespace
TestProperties
{
class Person
{
private String FirstName;
private string LastName;
private string Email;
private DateTime Dob;
public string SetfirstName
{
get
{
return FirstName;
}
set
{
FirstName = value;
}
}
public string SetLastName
{
get
{
return LastName;
}
set
{
LastName = value;
}
}
public string SetEmail
{
set
{
Email = value;
}
}
public DateTime SetDob
{
set
{
Dob = value;
}
}
public Person(String First, String Last, String email, DateTime DOfB)
{
FirstName = First;
LastName = Last;
Email = email;
Dob = DOfB;
}
public Person(String First, String Last, String email)
{
FirstName = First;
LastName = Last;
Email = email;
}
public Person(String First, String Last, DateTime DOfB)
{
FirstName = First;
LastName = Last;
Dob = DOfB;
}
public int age(DateTime dtDOB)
{
TimeSpan ts = DateTime.Now.Subtract(dtDOB);
int years = ts.Days / 365;
return years;
}
public string ZodiacSign(DateTime DateOfBirth)
{
string returnString = string.Empty;
string[] dateAndMonth = DateOfBirth.ToLongDateString().Split(new char[] { ',' });
string[] ckhString = dateAndMonth[1].ToString().Split(new char[] { ' ' });
if (ckhString[1].ToString() == "March")
{
if (Convert.ToInt32(ckhString[2]) <= 20)
{
returnString = "Pisces";
}
else
{
returnString = "Aries";
}
}
else if (ckhString[1].ToString() == "April")
{
if (Convert.ToInt32(ckhString[2]) <= 19)
{
returnString = "Aries";
}
else
{
returnString = "Taurus";
}
}
return returnString;
}
public bool IsAdult
{
get
{
if (age(this.Dob) >= 18)
{
return true;
}
else
{
return false;
}
}
}
public bool isBirthdayToday
{
get
{
if ((this.Dob.Day == DateTime.Now.Day) && (this.Dob.Month == DateTime.Now.Month))
{
return true;
}
else
{
return false;
}
}
}
public string GetSunSign
{
get
{
return (ZodiacSign(this.Dob));
}
}
}
class Program
{
private String getAdultString(Person obj)
{
if (obj.IsAdult)
{
return obj.SetfirstName + " Is Adult";
}
else
{
return obj.SetfirstName + " Is not Adult";
}
}
private String getBdayString(Person obj)
{
if (obj.isBirthdayToday)
{
return "Today Is " + obj.SetfirstName + "'s B'day";
}
else
{
return "Today Is not " + obj.SetfirstName + "'s B'day";
}
}
private String getSunSignString(Person obj)
{
return obj.SetfirstName + "'s Sun sign is " + obj.GetSunSign;
}
static void Main(string[] args)
{
Person p = new Person("Test1", "Test1", new DateTime(1984, 10, 30));
Person p1 = new Person("Test2", "Test2", new DateTime(2010, 4, 30));
Program objProgram = new Program();
Console.WriteLine(objProgram.getAdultString(p));
Console.WriteLine(objProgram.getBdayString(p));
Console.WriteLine(objProgram.getSunSignString(p));
Console.WriteLine();
Console.WriteLine(objProgram.getAdultString(p1));
Console.WriteLine(objProgram.getBdayString(p1));
Console.WriteLine(objProgram.getSunSignString(p1));
Console.ReadLine();
}
}
{
private String FirstName;
private string LastName;
private string Email;
private DateTime Dob;
public string SetfirstName
{
get
{
return FirstName;
}
set
{
FirstName = value;
}
}
public string SetLastName
{
get
{
return LastName;
}
set
{
LastName = value;
}
}
public string SetEmail
{
set
{
Email = value;
}
}
public DateTime SetDob
{
set
{
Dob = value;
}
}
public Person(String First, String Last, String email, DateTime DOfB)
{
FirstName = First;
LastName = Last;
Email = email;
Dob = DOfB;
}
public Person(String First, String Last, String email)
{
FirstName = First;
LastName = Last;
Email = email;
}
public Person(String First, String Last, DateTime DOfB)
{
FirstName = First;
LastName = Last;
Dob = DOfB;
}
public int age(DateTime dtDOB)
{
TimeSpan ts = DateTime.Now.Subtract(dtDOB);
int years = ts.Days / 365;
return years;
}
public string ZodiacSign(DateTime DateOfBirth)
{
string returnString = string.Empty;
string[] dateAndMonth = DateOfBirth.ToLongDateString().Split(new char[] { ',' });
string[] ckhString = dateAndMonth[1].ToString().Split(new char[] { ' ' });
if (ckhString[1].ToString() == "March")
{
if (Convert.ToInt32(ckhString[2]) <= 20)
{
returnString = "Pisces";
}
else
{
returnString = "Aries";
}
}
else if (ckhString[1].ToString() == "April")
{
if (Convert.ToInt32(ckhString[2]) <= 19)
{
returnString = "Aries";
}
else
{
returnString = "Taurus";
}
}
return returnString;
}
public bool IsAdult
{
get
{
if (age(this.Dob) >= 18)
{
return true;
}
else
{
return false;
}
}
}
public bool isBirthdayToday
{
get
{
if ((this.Dob.Day == DateTime.Now.Day) && (this.Dob.Month == DateTime.Now.Month))
{
return true;
}
else
{
return false;
}
}
}
public string GetSunSign
{
get
{
return (ZodiacSign(this.Dob));
}
}
}
class Program
{
private String getAdultString(Person obj)
{
if (obj.IsAdult)
{
return obj.SetfirstName + " Is Adult";
}
else
{
return obj.SetfirstName + " Is not Adult";
}
}
private String getBdayString(Person obj)
{
if (obj.isBirthdayToday)
{
return "Today Is " + obj.SetfirstName + "'s B'day";
}
else
{
return "Today Is not " + obj.SetfirstName + "'s B'day";
}
}
private String getSunSignString(Person obj)
{
return obj.SetfirstName + "'s Sun sign is " + obj.GetSunSign;
}
static void Main(string[] args)
{
Person p = new Person("Test1", "Test1", new DateTime(1984, 10, 30));
Person p1 = new Person("Test2", "Test2", new DateTime(2010, 4, 30));
Program objProgram = new Program();
Console.WriteLine(objProgram.getAdultString(p));
Console.WriteLine(objProgram.getBdayString(p));
Console.WriteLine(objProgram.getSunSignString(p));
Console.WriteLine();
Console.WriteLine(objProgram.getAdultString(p1));
Console.WriteLine(objProgram.getBdayString(p1));
Console.WriteLine(objProgram.getSunSignString(p1));
Console.ReadLine();
}
}
}
Subscribe to:
Posts (Atom)