diff --git a/High-Quality-Code/17. Design Patterns/Creational/Abstract-Factory-Pattern.md b/High-Quality-Code/17. Design Patterns/Creational/Abstract-Factory-Pattern.md index e2f6e11..542ada9 100644 --- a/High-Quality-Code/17. Design Patterns/Creational/Abstract-Factory-Pattern.md +++ b/High-Quality-Code/17. Design Patterns/Creational/Abstract-Factory-Pattern.md @@ -7,3 +7,162 @@ * В системи, които се променят често Шаблонът предоставя гъвкав механизъм, чрез който да се подменят различни множества от обекти. + +###Code sample +```csharp +using System; +using System.Collections.Generic; +using System.Text; + +public class MainApp +{ + public static void Main() + { + var furnitureFactory = new FamousSwedishChairManufacturer(); + var myShop = new FurnitureShop(furnitureFactory); + + var kitchenChair = myShop.OrderKitchenChair(); + Console.WriteLine(kitchenChair.ToString()); + + var officeChair = myShop.OrderOfficeChair(); + Console.WriteLine(officeChair.ToString()); + + var anotherFurnitureFactory = new InfamousImaginaryChairManufacturer(); + myShop = new FurnitureShop(anotherFurnitureFactory); + + var anotherKitchenChair = myShop.OrderKitchenChair(); + Console.WriteLine(anotherKitchenChair.ToString()); + + var anotherOfficeChair = myShop.OrderOfficeChair(); + Console.WriteLine(anotherOfficeChair.ToString()); + } +} + +public abstract class Chair +{ + private readonly IReadOnlyCollection features; + + protected Chair(IEnumerable features) + { + this.features = new List(features); + } + + protected abstract string Name { get; } + + private IEnumerable Features + { + get { return this.features; } + } + + public override string ToString() + { + var stringBuilder = new StringBuilder(); + stringBuilder.AppendLine(this.Name); + stringBuilder.AppendLine(string.Join(", ", this.Features)); + return stringBuilder.ToString(); + } + +} + +public class KitchenChair : Chair +{ + private readonly string manufacturer; + + public KitchenChair(IEnumerable features, string manufacturer) + : base(features) + { + this.manufacturer = manufacturer; + } + + protected override string Name + { + get + { + return string.Format("Kitchen chair manufactured by: {0}", this.manufacturer); + } + } +} + +public class OfficeChair : Chair +{ + private readonly string manufacturer; + + public OfficeChair(IEnumerable features, string manufacturer) + : base(features) + { + this.manufacturer = manufacturer; + } + + protected override string Name + { + get + { + return string.Format("Office chair manufactured by: {0}", this.manufacturer); + } + } +} + +public abstract class ChairManufacturer +{ + public abstract KitchenChair ManufactureKitchenChair(); + public abstract OfficeChair ManufactureOfficeChair(); +} + +public class FamousSwedishChairManufacturer : ChairManufacturer +{ + private const string Name = "FSCM"; + + public override KitchenChair ManufactureKitchenChair() + { + var features = new List { "padded seat", "padded back" }; + var chair = new KitchenChair(features, Name); + return chair; + } + + public override OfficeChair ManufactureOfficeChair() + { + var features = new List { "white leather", "comfort armrests", "wheels" }; + var chair = new OfficeChair(features, Name); + return chair; + } +} + +public class InfamousImaginaryChairManufacturer : ChairManufacturer +{ + private const string Name = "IICM"; + + public override KitchenChair ManufactureKitchenChair() + { + var features = new List { "thorned seat", "no back" }; + var chair = new KitchenChair(features, Name); + return chair; + } + + public override OfficeChair ManufactureOfficeChair() + { + var features = new List { "all wooden framework", "stiff back", "bolted to ground" }; + var chair = new OfficeChair(features, Name); + return chair; + } +} + +public class FurnitureShop +{ + private readonly ChairManufacturer supplier; + + public FurnitureShop(ChairManufacturer manufacturer) + { + this.supplier = manufacturer; + } + + public KitchenChair OrderKitchenChair() + { + return this.supplier.ManufactureKitchenChair(); + } + + public OfficeChair OrderOfficeChair() + { + return this.supplier.ManufactureOfficeChair(); + } +} +``` diff --git a/High-Quality-Code/17. Design Patterns/Creational/Builder-Pattern.md b/High-Quality-Code/17. Design Patterns/Creational/Builder-Pattern.md index aa96a1b..7054bd8 100644 --- a/High-Quality-Code/17. Design Patterns/Creational/Builder-Pattern.md +++ b/High-Quality-Code/17. Design Patterns/Creational/Builder-Pattern.md @@ -14,3 +14,148 @@ * **Builder** – дефинира всички стъпки. Използва се от **Concrete Builder**. * **Concrete Builder** – определя изпълнението. Имплементира **Director**. * **Product** – създава се от **Concrete Builder**. Продуктите са от еднакъв тип, но съдържат различни данни. + + +###Code sample +```csharp +using System; +using System.Collections.Generic; + +public class MainApp +{ + public static void Main() + { + IVelocipedeConstructor bikeShop = new VelocipedeConstructor(); + + VelocipedeBuilder myVelocipede = new UnicycleBuilder(); + bikeShop.Construct(myVelocipede); + myVelocipede.Velocipede.Describe(); + + myVelocipede = new TricycleBuilder(); + bikeShop.Construct(myVelocipede); + myVelocipede.Velocipede.Describe(); + } +} + +public interface IVelocipedeConstructor +{ + void Construct(VelocipedeBuilder velocipede); +} + +public class VelocipedeConstructor : IVelocipedeConstructor +{ + public void Construct(VelocipedeBuilder velocipede) + { + velocipede.BuildFrame(); + velocipede.AddWheels(); + velocipede.AddChain(); + velocipede.AddPedals(); + velocipede.AddSeat(); + } +} + +public abstract class VelocipedeBuilder +{ + public Velocipede Velocipede { get; set; } + + public abstract void BuildFrame(); + public abstract void AddWheels(); + public abstract void AddChain(); + public abstract void AddPedals(); + public abstract void AddSeat(); +} + +public class UnicycleBuilder : VelocipedeBuilder +{ + public UnicycleBuilder() + { + this.Velocipede = new Velocipede("Unicycle"); + } + + public override void BuildFrame() + { + this.Velocipede.AddPart("Frame", "Unicycle frame"); + } + + public override void AddWheels() + { + this.Velocipede.AddPart("Wheels", "1"); + } + + public override void AddChain() + { + this.Velocipede.AddPart("Chain", "Yes"); + } + + public override void AddPedals() + { + this.Velocipede.AddPart("Pedals", "Yes"); + } + + public override void AddSeat() + { + this.Velocipede.AddPart("Seat", "Yes"); + } + +} + +public class TricycleBuilder : VelocipedeBuilder +{ + public TricycleBuilder() + { + this.Velocipede = new Velocipede("Tricycle"); + } + + public override void BuildFrame() + { + this.Velocipede.AddPart("Frame", "Tricycle frame"); + } + + public override void AddWheels() + { + this.Velocipede.AddPart("Wheels", "3"); + } + + public override void AddChain() + { + this.Velocipede.AddPart("Chain", "No"); + } + + public override void AddPedals() + { + this.Velocipede.AddPart("Pedals", "Yes"); + } + + public override void AddSeat() + { + this.Velocipede.AddPart("Seat", "Yes"); + } + +} + +public class Velocipede +{ + private readonly string velocipedeType; + private readonly Dictionary parts = new Dictionary(); + + public Velocipede(string velocipedeType) + { + this.velocipedeType = velocipedeType; + } + + public void AddPart(string name, string description) + { + this.parts.Add(name, description); + } + + public void Describe() + { + Console.WriteLine("--------============--------"); + Console.WriteLine("Velocipede Type: {0}", this.velocipedeType); + foreach (var part in this.parts) + { + Console.WriteLine("{0} --> {1}", part.Key, part.Value); + } + } +} +``` diff --git a/High-Quality-Code/17. Design Patterns/Creational/Singleton-Pattern.md b/High-Quality-Code/17. Design Patterns/Creational/Singleton-Pattern.md index a1b949f..5b4cc02 100644 --- a/High-Quality-Code/17. Design Patterns/Creational/Singleton-Pattern.md +++ b/High-Quality-Code/17. Design Patterns/Creational/Singleton-Pattern.md @@ -18,3 +18,54 @@ - Внася тясна свързаност м/у себе си и всеки клас, който го използва. Класовете използващи **Singleton** класът не могат да съществуват самостоятелно и независимо от него. - Не е сигурен в многонишкова среда. Причината е за това е, че две (или повече нишки) могат да достъпят едновременно проверката, дали съществува инстанция на класа и едновремнно да я изчислят като невярна. В този случай ще се създадат две инстанции на **Singleton** класа, което нарушава шаблона. + + +###Code sample + +```csharp +using System; + +public class MainApp +{ + public static void Main() + { + var config = Configuration.Instance; + + config.Load(); + config.SaveSettings(); + } +} + +public sealed class Configuration +{ + private static readonly Configuration instance = new Configuration(); + + static Configuration() + { + + } + + private Configuration() + { + + } + + public static Configuration Instance + { + get + { + return instance; + } + } + + public void SaveSettings() + { + Console.WriteLine("Settings Saved!"); + } + + public void Load() + { + Console.WriteLine("Configuration Loaded!"); + } +} +```