惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Microsoft Security Blog
Microsoft Security Blog
Jina AI
Jina AI
量子位
博客园 - 叶小钗
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
IT之家
IT之家
S
SegmentFault 最新的问题
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
雷峰网
雷峰网
博客园 - 聂微东
美团技术团队
Last Week in AI
Last Week in AI
罗磊的独立博客
酷 壳 – CoolShell
酷 壳 – CoolShell
博客园 - 三生石上(FineUI控件)
WordPress大学
WordPress大学
宝玉的分享
宝玉的分享
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园_首页
V
Visual Studio Blog
大猫的无限游戏
大猫的无限游戏
The Cloudflare Blog

博客园 - DODONG

在现有PDF文件上添加水印 c#获取硬件信息 一个读写csv文件的C#类 . C#操作Excel文件(读取Excel,写入Excel) . 解决全球化时区问题 利用ReportViewer读取Reporting Service数据 [转]用反射来处理多字段提交 查询数据表中重复的记录 CVS文件导入SQL ComponentArt.Web.UI中AJAX TreeView 抽象工厂(Abstract Factory)模式 工厂方法(Factory Method)模式 用.NET创建Windows服务 关于SharePoint中查询写法和注意的地方 关于&运算符和^ 初识WAP开发时.. C#操作XML XMLHttp客户端操作数据 asp.net网页智能导航SmartNavigation的替代实现方式
简单工厂(Simple Factory)模式
DODONG · 2006-10-24 · via 博客园 - DODONG

using System;public abstract class Light
{
   
public abstract void TurnOn();
   
public abstract void TurnOff();
}
public class BulbLight : Light
{
   
public override void TurnOn()
   {
      Console.WriteLine(
"Bulb Light is Turned on");
   }
public override void TurnOff()
   {
      Console.WriteLine(
"Bulb Light is Turned off");
   }
}
public class TubeLight : Light
{
   
public override void TurnOn()
   {
      Console.WriteLine(
"Tube Light is Turned on");
   }
public override void TurnOff()
   {
      Console.WriteLine(
"Tube Light is Turned off");
   }
}
public class LightSimpleFactory
{
   
public Light Create(string LightType)
   {
      
if(LightType == "Bulb")
         
return new BulbLight();
      
else if(LightType == "Tube")
         
return new TubeLight();
      
else
         
return null;
   }
}
public class Client
{
   
public static void Main()
   {
      LightSimpleFactory lsf 
= new LightSimpleFactory();

      Light l 

= lsf.Create("Bulb");
      l.TurnOn();
      l.TurnOff();

      Console.WriteLine(

"-----------------");

      l 

= lsf.Create("Tube");
      l.TurnOn();
      l.TurnOff();
   }
}