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

Home
C# / CSharp Tutorial
1.Language Basics
2.Data Type
3.Operator
4.Statement
5.String
6.struct
7.Class
8.Operator Overload
9.delegate
10.Attribute
11.Data Structure
12.Assembly
13.Date Time
14.Development
15.File Directory Stream
16.Preprocessing Directives
17.Regular Expression
18.Generic
19.Reflection
20.Thread
21.I18N Internationalization
22.LINQ
23.GUI Windows Forms
24.Windows Presentation Foundation
25.Windows Communication Foundation
26.Workflow
27.2D
28.Design Patterns
29.Windows
30.XML
31.XML LINQ
32.ADO.Net
33.Network
34.Directory Services
35.Security
36.unsafe
C# / C Sharp
C# / C Sharp by API
C# / CSharp Open Source
C# / CSharp Tutorial » Design Patterns » Factory Pattern 
28.2.3.Abstract Factory
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
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.