GOF pattern - Factory Method

Definition
Define an interface for creating an object, but let subclasses decide which class to instantiate. Factory Method lets a class defer instantiation to subclasses.

Frequency of use: use_medium_high.gif medium high
UML class diagram
factory.gif
Participants
The classes and/or objects participating in this pattern are:
  • Product (Page)
    • defines the interface of objects the factory method creates
  • ConcreteProduct (SkillsPage, EducationPage, ExperiencePage)
    • implements the Product interface
  • Creator (Document)
    • declares the factory method, which returns an object of type Product. Creator may also define a default implementation of the factory method that returns a default ConcreteProduct object.
    • may call the factory method to create a Product object.
  • ConcreteCreator (Report, Resume)
    • overrides the factory method to return an instance of a ConcreteProduct.
Sample code
This structural code demonstrates the Factory method offering great flexibility in creating different objects. The Abstract class may provide a default object, but each subclass can instantiate an extended version of the object.
// Factory Method pattern -- Structural example

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

using System;
using System.Collections;

// "Product" 
abstract class Product {
}

// "ConcreteProductA" 
class ConcreteProductA : Product {
}

// "ConcreteProductB" 
class ConcreteProductB : Product {
}

// "Creator"
abstract class Creator {
  
// Methods
  abstract public Product FactoryMethod();
}

// "ConcreteCreator"
class ConcreteCreatorA : Creator {
  
// Methods
  override public Product FactoryMethod() {
    
return new ConcreteProductA();
  }
}

// "ConcreteCreator"
class ConcreteCreatorB : Creator {
  
// Methods
  override public Product FactoryMethod() {
    
return new ConcreteProductB();
  }
}

/// <summary>
///  Client test
/// </summary>
class Client {
  
public static void Main( string[] args ) {
    
// FactoryMethod returns ProductA
    Creator c = new ConcreteCreatorA();
    Product p 
= c.FactoryMethod();
    Console.WriteLine( 
"Created {0}", p );

    
// FactoryMethod returns ProductB
    c = new ConcreteCreatorB();
    p 
= c.FactoryMethod();
    Console.WriteLine( 
"Created {0}", p );
  }
}
This real-world code demonstrates the Factory method offering flexibility in creating different documents. The derived Document classes Report and Resume instantiate extended versions of the Document class. Here, the Factory Method is called in the constructor of the Document base class.
// Factory Method pattern -- Real World example

//--------------------------------------------------------
// Copyright (C) 2001 - 2002, Data & Object Factory
// All rights reserved. www.dofactory.com
//
// You are free to use this source code in your 
// applications as long as the original copyright
// notice is included.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT 
// WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, 
// INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF 
// MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
//--------------------------------------------------------

using System;
using System.Collections;

// "Product"
abstract class Page {
}

// "ConcreteProduct"
class SkillsPage : Page {
}

// "ConcreteProduct"
class EducationPage : Page {
}

// "ConcreteProduct"
class ExperiencePage : Page {
}

// "ConcreteProduct"
class IntroductionPage : Page {
}

// "ConcreteProduct"
class ResultsPage : Page {
}

// "ConcreteProduct"
class ConclusionPage : Page {
}

// "ConcreteProduct"
class SummaryPage : Page {
}

// "ConcreteProduct"
class BibliographyPage : Page {
}

// "Creator"
abstract class Document {
  
// Fields
  protected ArrayList pages = new ArrayList();

  
// Constructor
  public Document() {
    
this.CreatePages();
  }

  
// Properties 
  public ArrayList Pages {
    
getreturn pages; }
  }

  
// Factory Method
  abstract public void CreatePages();
}

// "ConcreteCreator"

class Resume : Document {
  
// Factory Method implementation
  override public void CreatePages() {
    pages.Add( 
new SkillsPage() );
    pages.Add( 
new EducationPage() );
    pages.Add( 
new ExperiencePage() );
  }
}

// "ConcreteCreator"
class Report : Document {
  
// Factory Method implementation
  override public void CreatePages() {
    pages.Add( 
new IntroductionPage() );
    pages.Add( 
new ResultsPage() );
    pages.Add( 
new ConclusionPage() );
    pages.Add( 
new SummaryPage() );
    pages.Add( 
new BibliographyPage() );
  }
}

///
 <summary>
///  FactoryMethodApp test
/// </summary>
class FactoryMethodApp {
  
public static void Main( string[] args ) {
    Document[] docs 
= new Document[ 2 ];

    
// Note: constructors call Factory Method
    docs[0= new Resume();
    docs[
1= new Report();

    
// Display document pages
    foreach( Document document in docs ) {
      Console.WriteLine( 
"\n" + document + " ------- " );
      
foreach( Page page in document.Pages )
        Console.WriteLine( 
" " + page );
    }

    Console.Read();
  }
}

posted on 2005-07-06 10:18 毒菇求Buy 阅读(355) 评论(0)  编辑 收藏 引用 所属分类: Design PatternC#

只有注册用户登录后才能发表评论。
<2006年3月>
2627281234
567891011
12131415161718
19202122232425
2627282930311
2345678

导航

统计

常用链接

留言簿(7)

随笔分类(133)

随笔档案(111)

文章分类(65)

文章档案(53)

相册

收藏夹(30)

BLOG

Book store

Graphics Design

搜索

最新评论

阅读排行榜

评论排行榜