Skip to content

Commit

Permalink
Added Code Samples
Browse files Browse the repository at this point in the history
C# code samples added to all files.
  • Loading branch information
nikolavn committed Sep 13, 2015
1 parent e6d5ad1 commit 62af9c7
Show file tree
Hide file tree
Showing 3 changed files with 355 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> features;

protected Chair(IEnumerable<string> features)
{
this.features = new List<string>(features);
}

protected abstract string Name { get; }

private IEnumerable<string> 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<string> 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<string> 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<string> { "padded seat", "padded back" };
var chair = new KitchenChair(features, Name);
return chair;
}

public override OfficeChair ManufactureOfficeChair()
{
var features = new List<string> { "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<string> { "thorned seat", "no back" };
var chair = new KitchenChair(features, Name);
return chair;
}

public override OfficeChair ManufactureOfficeChair()
{
var features = new List<string> { "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();
}
}
```
145 changes: 145 additions & 0 deletions High-Quality-Code/17. Design Patterns/Creational/Builder-Pattern.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> parts = new Dictionary<string, string>();

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);
}
}
}
```
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
}
```

0 comments on commit 62af9c7

Please sign in to comment.