Posts with the tag Object-Oriented:
This design patterns guides to create a wrap around existing class to reuse existing class for a new requirement.
Assume, I have two logging classes named WriteLogsToLocalFile and WriteLogsToServer.
class WriteLogsToLocalFile
{
public void Log(string fileName, string message)
{
//write logs to file }
}
class WriteLogsToServer
{
public void Log(string urL, string message, string token)
{
//call rest API with oAuth // Note: here token is used for oAuth
}
}
static void Main(string[] args)
{
WriteLogsToLocalFile fileLogs = new WriteLogsToLocalFile();
fileLogs.Log("hello.xml", "login success");
WriteLogsToServer serverLogs = new WriteLogsToServer();
serverLogs.Log("https://server.logs/upload", "login success", "oAuthToken");
}
Here user is manually creating objects to write logs separately to local file and server.
The Composite Design pattern is structural design pattern. This design pattern is used when we want to treat a group of objects in same way. The composite design pattern has following elements.
Component This is a class that contain all the members that needs to be implemented by all the objects.
Composite This class is used to add, remove and traverse components.
Leaf The leaf object in tree structure is defined as leaf element.
Code Sample I want to print salaries of all employees in my organization with names and designation. The root of organization tree is CEO. The CEO has directors as child composite.
To understand Double Dispatch, understanding of Overloading and Overriding is must. I already talked about Overloading and Overriding in post Overloading & Overriding.
One level of virtual dispatching derived types override a base types: as shown below
class Farms
{
public virtual void Irrigation()
{
Console.WriteLine("Farm Type");
}
}
class WheatFarm : Farms
{
public override void Irrigation()
{
Console.WriteLine("WheatFarm");
}
}
class RicaFarm : WheatFarm
{
public override void Irrigation()
{
Console.WriteLine("RicaFarm");
}
}
static void Main(string[] args)
{
var a = new Farms();
var b = new WheatFarm();
var c = new RicaFarm();
a.Irrigation();
b.Irrigation();
c.Irrigation();
}
//output
Farm Type
WheatFarm
RicaFarm
Two level of virtual dispatching (Double Dispatch) This concept is used in Visitor Design Pattern.
To learn about Single & Double Dispatch, many design patterns, we need to understand Overloading and Overriding.
Overloading Overloading is compile-time polymorphism. The methods/functions with same name but different number/type parameters are example of Overloading.
As Overloading is compile-time, means during run-time the base type is considered. Example:
class Crop
{
public virtual void CropName()
{
Console.WriteLine("Hey, My type is Crop");
}
}
class Wheat : Crop
{
public override void CropName()
{
Console.WriteLine("Hey, My type is Wheat");
}
}
/* An example of overloading (Method with same name but different parameter type)
* */
class CropWatering
{
public void WaterSupply(Crop crop)
{
Console.
To learn about Single & Double Dispatch, many design patterns, we need to understand Overloading and Overriding.
Overloading Overloading is compile-time polymorphism. The methods/functions with same name but different number/type parameters are example of Overloading.
As Overloading is compile-time, means during run-time the base type is considered. Example:
class Crop
{
public virtual void CropName()
{
Console.WriteLine("Hey, My type is Crop");
}
}
class Wheat : Crop
{
public override void CropName()
{
Console.WriteLine("Hey, My type is Wheat");
}
}
/* An example of overloading (Method with same name but different parameter type)
* */
class CropWatering
{
public void WaterSupply(Crop crop)
{
Console.