static void Main(string[] args){ Db a = Db.GetInstance(); Db b = Db.GetInstance(); Console.WriteLine(a == b); //True}
public class Db{ private static Db instance = new Db(); //null; private Db() { } //Private constructor public static Db GetInstance() { //if (Foo.instance == null) // Foo.instance = new Foo(); return Db.instance; }}
null = lazy
až je potřeba (šetří)
může trvat déle (init time)
new = eager (brzká)
tvoří se při prvním doteku
Factory
Info
kontrola nad vytvořenýma instancemi
Lze
vracet potomky
(ne)vrátit podle podmínky
recyklovat (cyklicky vracet)
vybrat nejvhodnější
nejméně zatížený worker
Adapter
Info ^info
Adapter
= vlastní standart
mezivrstva mezi zdrojem a cílem
middleman
adapter v zásuvce
zmenší proud pro telefon
komunikace přes prostředníka který je překladač
Adapter
zachování kontability
nahrazení staráho systému novím
legenda
Y = starý provider nahrazen Z za pomocí adapteru
Providers
Vice provideru
adapter = mezivrstva
C# ^csharp
public class Order{ public int Id { get; set; } public string Street { get; set; } public string City { get; set; }}public class Client{ //Defaultni provider public ICarrierAdapter Adapter { get; set; } = new CarrierA_Adapter(); public void CreateOrder(Order order) { //... Adapter.Create(order); //... }}public interface ICarrierAdapter{ void Create(Order order);}
Provider A
public class CarrierA_Adapter : ICarrierAdapter{ private CarrierA c = new CarrierA(); public void Create(Order o) { c.Create(o.Id.ToString(), $"{o.Street}, {o.City}"); }}public class CarrierA{ public void Create(string orderId, string address) { } //logic}
Provide B
public class CarrierB_Adapter : ICarrierAdapter{ private CarrierB c = new CarrierB(); public void Create(Order order) { c.Submit(new CarrierB.PackageInfo() { OrderId = order.Id, Street = order.Street, City = order.City }); }}public class CarrierB{ public void Submit(PackageInfo info){ } //logic public class PackageInfo { public int OrderId { get; set; } public string Street { get; set; } public string City { get; set; } }}
Decorator
Info ^info
omezit množství tříd které by vznikly z důvodu kombinace ???
řešení kombinace tříd
příklady
html tags
stream
buffer
filtrovat neplatný znaky
angular pipe
observer s dekoratérem
formuláře
textbox ⇒ label
(frame+label+texbox)
C# ^csharp
static void Main(){ IText text = new Text("Hi"); //Hi text = new Strong(text); //<strong.>Hi<./strong> text = new Div(text);//<div.><strong.>Hi<./strong><./div>}//-----------------------public interface IText{ public string Draw();}//-----------------------public class Text : IText{ public string Value { get; set; } public Text(string value) { Value = value; } public string Draw() { return Value; }}public class Strong : IText{ public IText Text { get; set; } public Strong(IText value) { Text = value; } public string Draw() { return $"<strong.>{Text.Draw()}<./strong>"; }}public class Div : IText{ public IText Text { get; set; } public Div(IText value) { Text = value; } public string Draw() { return $"<div.>{Text.Draw()}<./div>"; }}public class P : IText{ public IText Text { get; set; } public P(IText value) { Text = value; } public string Draw() { return $"<p.>{Text.Draw()}<./p>"; }}
<ul>
static void Main(){ IText text = new Ul( new Text("Text"), new Strong(new Text("Strong")), new Ul( new Text("Ul-1"), new Text("Ul-2") ) ); text = new Div(text); Console.WriteLine(text.Draw());}
public class Ul : IText{ public IText[] items; public Ul(params IText[] i) { items = i; } public string Draw() { var sb = new StringBuilder(); sb.Append("<ul.>"); foreach (var i in items) { sb.Append($"<li.>{i.Draw()}<./li>"); } sb.Append("</.ul>"); return sb.ToString(); }
Používá se tam, kde je stávající subsystém tříd příliš složitý
Poskytuje jednotné rozhraní vyšší úrovně ke všem rozhraním tohoto subsystému
Klient nemusí komunikovat s jednotlivými rozhraními subsytému, ale pouze s fasádou, která zná strukturu subsystému a volání předává
POZOR: rozhraním fasády není míněno interface, fasáda je vždy realizována jako třída
př. jednotlivá oddělení banky
Composite
Strukturální vzor
Umožňuje vytvářet stromovou strukturu reprezentující hierarchii skládající se z jednoduchých a z nich složených objektů
Výchozí uzel (kořen)
Obsahuje odkazy na potomky:
Jednotlivé objekty (listy)
Složené objekty (větve)
Př. kořen – počítačová sestava, listy – monitor, myš, klávesnice, zdroj, ….)
State
Info
používá se v GUI?
C# ^csharp
❌
public class CarOld{ public string Rotatation { get; set; } public void Draw() { if (Rotatation == "left") Console.WriteLine("<-"); else if (Rotatation == "right") Console.WriteLine("->"); } }
✔
public class Car{ public IRotation Rotatation { get; set; } = new LeftRotation(); public void Draw() { Rotatation.Draw(); }}public interface IRotation { public void Draw(); }public class LeftRotation : IRotation { public void Draw() { Console.WriteLine("<--"); } }public class RightRotation : IRotation { public void Draw() { Console.WriteLine("-->"); } }
Strategy
Behaviorální vzor
Základní podmínkou je existence různých řešení stejného problému
Strategie mají stejné rozhraní
Obvyklý model chování:
Zvolení strategie
Výchozímu objektu je klientem předán object konkrétní strategie
Klient komunikuje poze s výchozím objektem, který využívá object zvoolené strategie
Vzor Strategie by měl být užíván pouze tehdy, je-li jeho výsledek přímo svázán s klientem určujícím strategii