Csharp/CSharp Tutorial/Design Patterns/Factory Pattern

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

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.ruputerBrand();
      return rad;
    }
    public override ComputerDesk BuildComputerDesk()
    {
      ComputerDesk cap = new AComputerDesk();
      cap.ruputerDeskBrand();
      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.ruputerBrand();
      return rad;
    }
    public override ComputerDesk BuildComputerDesk()
    {
      ComputerDesk cap = new BComputerDesk();
      cap.ruputerDeskBrand();
      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.ruputerBrand();
      return rad;
    }
    public override ComputerDesk BuildComputerDesk()
    {
      ComputerDesk cap = new CComputerDesk();
      cap.ruputerDeskBrand();
      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);
    }
    
  }

Factory Method Patterns

using System;
using System.Collections;
  public abstract class Section
  {
    public abstract void SomeSection();
  }
  public class TitleSection: Section
  {
    
    public override void SomeSection()
    {
      Console.WriteLine("title.");
    }  
        
    public TitleSection(){;}
    
  }
  public class HeaderSection: Section
  {
    public override void SomeSection()
    {
      Console.WriteLine("header.");
    }
    public HeaderSection(){;}
  }
  public class FooterSection: Section
  {
        public override void SomeSection()
        {
            Console.WriteLine("footer.");
        }
    public FooterSection(){;}
  }
  public class MenuSection: Section
  {
    public override void SomeSection()
    {
      Console.WriteLine("menu.");
        }
            
    public MenuSection(){;}
  }
  public class CatalogPage: Page
  {
    public override void AddSection(){
      this.pageCompositor.Clear();
      this.pageCompositor.Add(new TitleSection());
      this.pageCompositor.Add(new FooterSection());
    }
    public override void DisplayPage(){
      Console.WriteLine("Catalog page contains:");
      foreach(Section c in this.pageCompositor)
        c.SomeSection();
    }
    public CatalogPage()
    {
      this.AddSection();
    }
  }
  public class ManualPage: Page
  {
    public override void AddSection()
    {
      this.pageCompositor.Clear();
      this.pageCompositor.Add(new MenuSection());
      this.pageCompositor.Add(new FooterSection());
      this.pageCompositor.Add(new HeaderSection());
    }
    public override void DisplayPage()
    {
      Console.WriteLine("Manual page contains:");
        foreach(Section c in this.pageCompositor)
        c.SomeSection();
      Console.WriteLine();
    }
    public ManualPage(){;}
  }
  public abstract class Page
  {
    protected ArrayList pageCompositor = new ArrayList();
    public abstract void AddSection();
    public abstract void DisplayPage();
  }  
  class Client
  {
    static void Main(string[] args)
    {
      Page p = new  CatalogPage();
      p.AddSection();
      p.DisplayPage();
  
      p = new ManualPage();
      p.AddSection();
      p.DisplayPage();
      
    }
  }

Use a static class factory

using System; 
 
class MyClass { 
  int a, b; 
 
  // Create a class factory for MyClass. 
  static public MyClass factory(int i, int j) { 
    MyClass t = new MyClass(); 
    
    t.a = i; 
    t.b = j; 
 
    return t; // return an object 
  } 
 
  public void show() { 
    Console.WriteLine("a and b: " + a + " " + b); 
  } 
} 
  
class MainClass { 
  public static void Main() {   
    int i, j; 
 
    // generate objects using the factory 
    for(i=0, j=10; i < 10; i++, j--) { 
      MyClass ob = MyClass.factory(i, j); // get an object 
      ob.show(); 
    } 
       
    Console.WriteLine();    
  } 
}
a and b: 0 10
a and b: 1 9
a and b: 2 8
a and b: 3 7
a and b: 4 6
a and b: 5 5
a and b: 6 4
a and b: 7 3
a and b: 8 2
a and b: 9 1