Structural Design Patterns

Adapter Pattern

https://levelup.gitconnected.com/design-patterns-structural-patterns-of-design-classes-and-objects-79d58a6519b
namespace AdapterPattern
{
public class ExistingReportTable
{
public DataTable CreateReportDataTable()
{
//I create the datatable I needed, don't worry about the operations. Main concern is to implement Adapter Pattern.
DataTable x = new DataTable();
return x;
}
}
}
 public interface IReportDataAdapter
{
public DataTable CreateReportData();
}
public class ReportDataAdapter : IReportDataAdapter
{
public DataTable CreateReportData()
{
//I have created the necessary interface, now I am creating the Adapter itself.
ExistingReportTable reportTable = new ExistingReportTable();
return reportTable.CreateReportDataTable();
}
}
class Program
{
static void Main()
{
//As you can see, the client is able to create the report table due to adapter pattern.
IReportDataAdapter adapter = new ReportDataAdapter();
var reportTable = adapter.CreateReportData();
Console.Write(reportTable);
}
}

Composite Pattern

https://en.wikipedia.org/wiki/Composite_pattern
  • Composite : Yes, here we are going to do the necessary composition which means this is going to include informations or operations for both kind of objects.
  • After the creation of UML diagram with some theoric info, you can find an easy to-understand code implementation below :
  • Components : IPosition
  • Leafs : Attack,Defense,Midfield.
  • Composite : PositionCompositePattern.
namespace CompositePattern
{
public interface IPosition
{
public string PositionName { get; set; }
public void Render(double percentageOfPerformance);
}
public class Attack : IPosition
{
public Attack(string _positionName)
{
PositionName = _positionName;
}
public string PositionName { get; set; }
public void Render(double percentageOfPerformance)
{
//
}
}
public class MidField : IPosition
{
public MidField(string _positionName)
{
PositionName = _positionName;
}
public string PositionName { get; set; }
public void Render(double percentageOfPerformance)
{
//
}
}
public class Defence : IPosition
{
public Defence(string _positionName)
{
PositionName = _positionName;
}
public string PositionName { get; set; }
public void Render(double percentageOfPerformance)
{
//
}
}
public class PositionCompositePattern :IPosition
{
public PositionCompositePattern(string _positionName)
{
PositionName = _positionName;
}
public string PositionName { get; set; }
private readonly List<IPosition> positionList = new List<IPosition>();
public void Render(double percentageOfPerformance)
{
foreach(var item in positionList)
{
item.Render(percentageOfPerformance);
}
}
public void AddPosition(IPosition position)
{
positionList.Add(position);
}
public void RemovePosition(IPosition position)
{
positionList.Remove(position);
}
}
class Program
{
static void Main(string[] args)
{
//Now, it is possible to work with any type of position that implements IPosition interface !
PositionCompositePattern newPosition = new PositionCompositePattern("New Position");
Attack position1 = new Attack("Position1");
newPosition.AddPosition(position1);
Defence position2 = new Defence("Position2");
newPosition.AddPosition(position2);
position1.Render(56.95);
}
}

Proxy Pattern

https://ar.pinterest.com/pin/365284219749221850/
https://subscription.packtpub.com/book/application_development/9781785888038/5/ch05lvl1sec38/a-uml-class-diagram-for-the-proxy-pattern
namespace ProxyPattern
{
public interface IBankAccount
{
//This is our interface that both real object and proxy classes are going to inherit.
//You can have anything you need from the resource in this interface but our main purpose is to understand the logic so I am not going
//to implement all the code but an example.
public decimal ShowUpdatedAccountBalance(int accountId);
}
public class PaymentTransaction
{
int _accountId;
decimal _price;
public PaymentTransaction(int accountId,decimal price)
{
_accountId = accountId;
_price = price;
}
public Decimal Price { get { return _price; } }
}
public class BankAccount : IBankAccount
{
//BankAccount Class is our real object, I mean the resource. We have all main responsibilities or aythorities here.
int _accountId = 0;
public BankAccount(int accountId)
{
_accountId = accountId;

//do the necessary operations here.Reach the PaymentTransaction and find out the price.
//PaymentTransaction codes are not necesarry here. So, I do not implement. The goal is to understand
//main logic and usage of proxy pattern.
}
public decimal ShowUpdatedAccountBalance(int accountId)
{
decimal accountBalance = 0;
decimal price = _price;
return accountBalance - price;
}
}
public class BankAccountProxy : IBankAccount
{
//As the name indicates, this is our Proxy class. Here is the access point.
//I reach the ShowUpdatedAccountBalance() that is in real object.
BankAccount realSubject = null;
int accountId = 0;
public BankAccountProxy(int accountId)
{
}
public decimal ShowUpdatedAccountBalance(int accountId)
{
realSubject = new BankAccount(accountId);
var result = realSubject.ShowUpdatedAccountBalance(accountId);
return result;
}
}
class Program
{
static void Main(string[] args)
{
//Client doesn't reach the resource aka real object (BankAccount)
//Client calls the proxy object (BankAccountProxy) and reaches the real object via proxy.
BankAccountProxy myProxy = new BankAccountProxy(0);
myProxy.ShowUpdatedAccountBalance(0);
}
}

FlyWeight Pattern

https://www.pinterest.com/pin/123708320999692064/

Facade Pattern

https://www.123rf.com/photo_128884970_stock-illustration-fake-person-bad-tired-man-deceive-concept-businessman-wear-smile-mask-isolated-flat-cartoon-characte.html
http://gyanendushekhar.com/2016/08/21/facade-design-pattern-c/
namespace FacadePattern
{
public class Analysts
{
public string Name { get; set; }
public double Age { get; set; }
public int ExperienceOfYears { get; set; }
public List<string > Tools { get; set; }
}
public class Developers
{
public string Name { get; set; }
public double Age { get; set; }
public int ExperienceOfYears { get; set; }
public string Framework { get; set; }
public List<string> Techs { get; set; }
}
public class ProjectOwners
{
public string Name { get; set; }
public double Age { get; set; }
public int ExperienceOfYears { get; set; }
public List<string> Languages { get; set; }
public List<string> Certificates { get; set; }
}
public class Testers
{
public string Name { get; set; }
public double Age { get; set; }
public int ExperienceOfYears { get; set; }
public bool WriteTestCode { get; set; }
public List<string> Techs { get; set; }
public List<string> Tools { get; set; }
public bool UseSql { get; set; }
}
public class TheTeam
{
public TheTeam CreateMyteam(int numberOfDevs, int numberOfAnalysts, int numberOfTesters, int numberOfPOS)
{
TheTeam myTeam = new TheTeam();
return myTeam;
}
}
public class TeamCreator
{
public Developers Developers { get; set; }
public Analysts Analysts { get; set; }
public Testers Testers { get; set; }
public ProjectOwners ProjectOwners { get; set; }
public TeamCreator(Developers DeveloperTeam, Analysts AnalystTeam,Testers TestTeam,ProjectOwners ProjectOwnersTeam)
{
this.Developers = DeveloperTeam;
this.Analysts = AnalystTeam;
this.Testers = Testers;
this.ProjectOwners = ProjectOwnersTeam;
}
public TheTeam CreateTheTeam(int numberOfDevs, int numberOfAnalysts,int numberOfTesters,int numberOfPOS)
{
TheTeam creator = new TheTeam();
return creator.CreateMyteam(5, 3, 4, 1);
}
}
class Program
{
static void Main()
{
TeamFacade facade = new TeamFacade();
facade.UniteTheTeam();
}
}
}

Bridge Pattern

https://www.codeproject.com/Articles/29729/Most-Commonly-Used-dEsign-pAttern
namespace BridgePattern
{
public class Car
{
public IFuelType FuelType { get; set; }
public Car(IFuelType fuelType)
{
FuelType = fuelType;
}
public virtual void Complete()
{
FuelType.Manufacture();
}
}
public interface IFuelType
{
void Manufacture();
}
public class DieselFuelType : IFuelType
{
public void Manufacture()
{
Console.WriteLine("Fuel type is diesel.");
}
}
public class GasolineFuelType : IFuelType
{
public void Manufacture()
{
Console.WriteLine("Fuel Type is Gasoline.");
}
}
public class ManuelCar : Car
{
public ManuelCar(IFuelType fuelType) : base(fuelType)
{
}
public override void Complete()
{
Console.WriteLine("Manuel Car is Ready !");
base.Complete();
}
}
public class AutomaticCar : Car
{
public AutomaticCar(IFuelType fuelType) : base(fuelType)
{
}
public override void Complete()
{
Console.WriteLine("Automatic car is Ready !");
base.Complete();
}
}
class Program
{
static void Main()
{
Car car = new ManuelCar(new DieselFuelType());
car.Complete();
Car newCar = new AutomaticCar(new GasolineFuelType());
newCar.Complete();
}
}

Decorator Pattern

namespace DecoratorPattern
{
public interface ICar
{
//public string Model { get; set; }
decimal CalculatePrice();
string GetInformation();
}
public class DieselCar : ICar
{
public decimal CalculatePrice()
{
return 1;
}
public string GetInformation()
{
return "";
}
}
public class AutomaticCar : ICar
{
public decimal CalculatePrice()
{
return 1;
}
public string GetInformation()
{
return "";
}
}
public class HighlineCar : ICar
{
public decimal CalculatePrice()
{
return 1;
}
public string GetInformation()
{
return "";
}
}
public abstract class CarDecoratorPattern : ICar
{
private ICar car;
public CarDecoratorPattern(ICar _car)
{
this.car = _car;
}
public virtual decimal CalculatePrice()
{
return this.car.CalculatePrice();
}
public virtual string GetInformation()
{
return this.car.GetInformation();
}
}
public class Package1 : CarDecoratorPattern
{
public Package1(ICar _car) : base(_car)
{
}
public override decimal CalculatePrice()
{
return base.CalculatePrice();
}
public override string GetInformation()
{
return base.GetInformation();
}
}
public class Package2 : CarDecoratorPattern
{
public Package2(ICar _car) : base(_car)
{
}
//Do not forget adding virtual keyword in decorator to not to get any erros here, overriding the function.
public override decimal CalculatePrice()
{
return base.CalculatePrice();
}
public override string GetInformation()
{
return base.GetInformation();
}
}
}
class Program
{
static void Main(string[] args)
{
ICar car = new DieselCar();
CarDecoratorPattern carDecorator = new Package1(car);
carDecorator = new Package2(carDecorator);
Console.WriteLine("Car's Price : " + carDecorator.CalculatePrice());
Console.WriteLine("Car's Information : " + carDecorator.GetInformation());
}
}

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store