Factory Method Patterns : 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.2.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();
      
    }
  }
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.