Abstract Factory : Factory Pattern « Design Patterns « C# / CSharp Tutorial






using System;


  public class AComputer: Computer
  {
    public override void ComputerBrand()
    {
      Console.WriteLine ("A Computer.");
    }
    public AComputer(){;}

  }
  public class AComputerDesk: ComputerDesk
  {
    public override void ComputerDeskBrand()
    {
      Console.WriteLine("A Computer cap.");
    }
    public AComputerDesk(){;}
  }
  public class AComputerFactory: ComputerFactory
  {
    public override Computer BuildComputer()
    {
      Computer rad = new AComputer();
      rad.ComputerBrand();
      return rad;
    }
    public override ComputerDesk BuildComputerDesk()
    {
      ComputerDesk cap = new AComputerDesk();
      cap.ComputerDeskBrand();
      return cap;
    }
        
    public AComputerFactory(){;}

  }

  public class BComputer:Computer
  {
    public override void ComputerBrand()
    {
      Console.WriteLine ("B Computer.");
    }
    
    
    public BComputer(){;}

  }
  public class BComputerDesk: ComputerDesk
  {
      
    public override void ComputerDeskBrand()
    {
      Console.WriteLine("B Computer cap.");
    }

    public BComputerDesk(){;}

  }
  public class BComputerFactory: ComputerFactory
  {
    
    public override Computer BuildComputer()
    {
      Computer rad = new  BComputer();
      rad.ComputerBrand();
      return rad;
    }

    public override ComputerDesk BuildComputerDesk()
    {
      ComputerDesk cap = new BComputerDesk();
      cap.ComputerDeskBrand();
      return cap;
    }

    
    public BComputerFactory(){;}

  }
  public class CComputer: Computer
  {
    
    public override void ComputerBrand()
    {
      Console.WriteLine ("C Computer.");
    }
  
    public CComputer() {;}

  }
  public class CComputerDesk: ComputerDesk
  {
    
    public override void ComputerDeskBrand()
    {
      Console.WriteLine("C Computer cap.");
    }
    
    public CComputerDesk(){;}
  }
    public class CComputerFactory : ComputerFactory
  {
    
    public override Computer BuildComputer()
    {
      Computer rad = new  CComputer();
      rad.ComputerBrand();
      return rad;
    }

    public override ComputerDesk BuildComputerDesk()
    {
      ComputerDesk cap = new CComputerDesk();
      cap.ComputerDeskBrand();
      return cap;
    }
    
    public CComputerFactory(){;}

  }
  public abstract class Computer
  {
    public abstract void ComputerBrand();
  }
  public abstract class ComputerDesk
  {
    public abstract void ComputerDeskBrand();
  }
  public abstract class ComputerFactory
  {
        public abstract Computer BuildComputer();
    public abstract ComputerDesk BuildComputerDesk();
  }
  public class WorkOrder
  {
        public void AssembleComputer(ComputerFactory fact)
    {
      Computer rad = fact.BuildComputer();
      ComputerDesk cap = fact.BuildComputerDesk();
      this.Assemble();
    }
    
    private void Assemble()
    {
      Console.WriteLine ("Assembling Computer and cap.");
      Console.WriteLine();
    }
    
    public WorkOrder(){;}

  }
  class Client
  {
    
    static void Main(string[] args)
    {
            ComputerFactory factoryOne = new AComputerFactory();
      WorkOrder workOne = new WorkOrder();
      workOne.AssembleComputer(factoryOne);
        
            ComputerFactory factoryTwo = new BComputerFactory();
      WorkOrder workTwo = new WorkOrder();
      workTwo.AssembleComputer(factoryTwo);

            ComputerFactory factoryThree = new CComputerFactory();
      WorkOrder workThree = new WorkOrder();
      workThree.AssembleComputer(factoryThree);

    }
    
  }








28.2.Factory Pattern
28.2.1.Use a static class factory
28.2.2.Factory Method Patterns
28.2.3.Abstract Factory