Creational -> Abstract Factory
Die Abstract Factory implementiert eine Schnittstelle zur Erzeugung von ähnlichen und/oder abhängigen Objektgruppen ohne diese direkt anzugeben oder zu kennen.
Hauptklasse:
1: class Program
2: {
3: static void Main(string[] args)
4: {
5: // Factory #2
6: Abstract.AbstractFactory factory1 = new ConcreteFactory1();
7: var c1 = new Program(factory1);
8: c1.Run();
9: // Factory #1
10: Abstract.AbstractFactory factory2 = new ConcreteFactory2();
11: var c2 = new Program(factory2);
12: c2.Run();
13: Console.Read();
14: }
15: public Program(Abstract.AbstractFactory factory)
16: {
17: _abstractProductA = factory.CreateProductA();
18: _abstractProductB = factory.CreateProductB();
19: }
20: public void Run()
21: {
22: _abstractProductB.Interact(_abstractProductA);
23: }
24: private readonly AbstractProductA _abstractProductA;
25: private readonly AbstractProductB _abstractProductB;
26: }
AbstractFactory.cs
1: namespace AbstractFactory.Abstract
2: {
3: abstract class AbstractFactory
4: {
5: public abstract AbstractProductA CreateProductA();
6: public abstract AbstractProductB CreateProductB();
7: }
8: }
AbstractProductA.cs
1: namespace AbstractFactory.Abstract
2: {
3: abstract class AbstractProductA
4: {
5: }
6: }
AbstractProductB.cs
1: namespace AbstractFactory.Abstract
2: {
3: abstract class AbstractProductB
4: {
5: public abstract void Interact(AbstractProductA a);
6: }
7: }
ConcreteFactory1.cs
1: using AbstractFactory.Abstract;
2: using AbstractFactory.Product;
3: namespace AbstractFactory.Concrete
4: {
5: class ConcreteFactory1 : Abstract.AbstractFactory
6: {
7: public override AbstractProductA CreateProductA()
8: {
9: return new ProductA1();
10: }
11: public override AbstractProductB CreateProductB()
12: {
13: return new ProductB1();
14: }
15: }
16: }
ConcreteFactory2.cs
1: using AbstractFactory.Abstract;
2: using AbstractFactory.Product;
3: namespace AbstractFactory.Concrete
4: {
5: class ConcreteFactory2 : Abstract.AbstractFactory
6: {
7: public override AbstractProductA CreateProductA()
8: {
9: return new ProductA2();
10: }
11: public override AbstractProductB CreateProductB()
12: {
13: return new ProductB2();
14: }
15: }
16: }
AbstractProductA1.cs
1: using AbstractFactory.Abstract;
2: namespace AbstractFactory.Product
3: {
4: class ProductA1 : AbstractProductA
5: {
6: }
7: }
AbstractProductA2.cs
1: using AbstractFactory.Abstract;
2: namespace AbstractFactory.Product
3: {
4: class ProductA2 : AbstractProductA
5: {
6: }
7: }
AbstractProductB1.cs
1: using System;
2: using AbstractFactory.Abstract;
3: namespace AbstractFactory.Product
4: {
5: class ProductB1 : AbstractProductB
6: {
7: public override void Interact(AbstractProductA a)
8: {
9: Console.WriteLine(GetType().Name + " interacts with " + a.GetType().Name);
10: }
11: }
12: }
AbstractProductB2.cs
1: using System;
2: using AbstractFactory.Abstract;
3: namespace AbstractFactory.Product
4: {
5: class ProductB2 : AbstractProductB
6: {
7: public override void Interact(AbstractProductA a)
8: {
9: Console.WriteLine(GetType().Name + " interacts with " + a.GetType().Name);
10: }
11: }
12: }