Шаблоны проектирования ООП
Принципы ООП Инкапсуляция Наследование Полиморфизм Абстракция данных
Паттерны Основные шаблоны (Fundamental) Порождающие шаблоны проектирования (Creational) Структурные шаблоны (Structural) Поведенческие шаблоны (Behavioral) Шаблоны параллельного программирования (Concurrency) MVC Enterprise И тд.
Основные шаблоны (Fundamental) Delegation pattern/Шаблон делегирования Functional design/Шаблон функционального дизайна Immutable/Неизменяемый объект Interface Property Container
Порождающие шаблоны проектирования (Creational) Abstract Factory Builder Factory Method Lazy initialization Multiton Object Pool Prototype Resource acquisition is initialization (RAII) Singleton
Abstract Factory abstract class AbstractFactory { public abstract AbstractProductA CreateProductA(); public abstract AbstractProductB CreateProductB(); } class ConcreteFactory1 : AbstractFactory { public override AbstractProductA CreateProductA() { return new ProductA1(); } public override AbstractProductB CreateProductB() { return new ProductB1(); } abstract class AbstractProductB { public abstract void Interact(AbstractProductA a); } class Client { private AbstractProductA abstractProductA; private AbstractProductB abstractProductB; public Client(AbstractFactory factory) { abstractProductB = factory.CreateProductB(); abstractProductA = factory.CreateProductA(); } public void Run() { abstractProductB.Interact(abstractProductA); } public static void Main() { AbstractFactory factory1 = new ConcreteFactory1(); Client c1 = new Client(factory1); c1.Run(); }
Abstract Factory
Factory Method using System; class MainApp { static void Main() { Creator[] creators = new Creator[2]; creators[0] = new ConcreteCreatorA(); creators[1] = new ConcreteCreatorB(); foreach(Creator creator in creators) { Product product = creator.FactoryMethod(); Console.WriteLine("Created {0}", product.GetType().Name); } // Wait for user Console.Read(); } abstract class Product{ } class ConcreteProductA : Product { } class ConcreteProductB : Product { } abstract class Creator { public abstract Product FactoryMethod(); } class ConcreteCreatorA : Creator { public override Product FactoryMethod() { return new ConcreteProductA(); } class ConcreteCreatorB : Creator { public override Product FactoryMethod() { return new ConcreteProductB(); }
Factory Method
Builder abstract class Builder { public abstract void BuildPartA(); public abstract void BuildPartB(); public abstract Product GetResult(); } class ConcreteBuilder1 : Builder { private readonly Product product = new Product(); public override void BuildPartA() { product.Add("PartA"); } public override void BuildPartB() { product.Add("PartB"); } public override Product GetResult() { return product; } }
Builder