• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
AnsonWu的博客
博客园    首页    新随笔    联系   管理    订阅  订阅

设计模式--工厂方法

 

/*
 * 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();
        }
    }
}
posted @ 2007-04-05 11:15  AnsonWu  阅读(162)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3