设计模式--工厂方法

 

/*
 * Created by SharpDevelop.
 * User: anson.wu
 * Date: 2007-4-5
 * Time: 10:55
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 
*/
using  System;

namespace
 Structural
{
    
class
 MainClass
    {
        
public static void Main(string
[] args)
        {
            Product[] product
=new Product[2
];
            product[
0]=new
 CreatorA().FactoryMethod();
            product[
1]=new
 CreatorB().FactoryMethod();            
            Console.Read();
        }
    }
    
//
    public abstract  class
 Product
    {
    
    }
    
public class
 ProductA:Product
    {
        
public
 ProductA()
        {
            Console.WriteLine(
"ProductA Been Create"
);
        }
    }
    
public class
 ProductB:Product
    {
        
public
 ProductB()
        {
            Console.WriteLine(
"ProductB Been Create"
);
        }
    }
    
public abstract class
 Creator
    {
        
public abstract
 Product FactoryMethod();
        
    }
    
public class
 CreatorA:Creator
    {
        
public override
 Product FactoryMethod()
        {
            
return new
 ProductA();
        }
    }
    
public class
 CreatorB:Creator
    {
        
public override
 Product FactoryMethod()
        {
            
return new
 ProductB();
        }
    }
}

你可能感兴趣的:(设计模式)