{BoyLee's Blog}
目前有点菜,正在变得越来越不菜,頑張って .........
博客园
社区
首页
新随笔
管理
订阅
随笔- 54 文章- 0 评论- 193
收藏:简单工厂模式(SimpleFactory-C#)
快餐店提供很多食物,比如 面条,米饭,面包。首先定义了一个Food接口,然后这些食物都从它来继承,定义了一个大厨
他包办所有食物的制作工作,这就是我所理解的简单工厂模式的概念,下面是源代码:
using
System;
namespace
SimpleFactoryPattern
{
/**/
///
<summary>
///
简单工厂模式示例
///
</summary>
class
SimpleFactoryPattern
{
//
定义Food接口
public
interface
Food
{
//
烹饪
void
Cook();
//
卖出
void
Sell();
}
//
Noodle
public
class
Noodle:Food
{
public
Noodle()
{
Console.WriteLine(
"
\nThe Noodle is made..
"
);
}
private
int
price;
//
面条Noodle的Cook方法接口实现
public
void
Cook()
{
Console.WriteLine(
"
\nNoodle is cooking
"
);
}
//
面条Noodle的Sell方法接口实现
public
void
Sell()
{
Console.WriteLine(
"
\nNoodle has been sold
"
);
}
public
int
Price
{
get
{
return
this
.price;}
set
{price
=
value;}
}
}
//
Rice
public
class
Rice:Food
{
public
Rice()
{
Console.WriteLine(
"
\nThe Rice is made ..
"
);
}
private
int
price;
public
void
Cook()
{
Console.WriteLine(
"
\nRice is cooking
"
);
}
public
void
Sell()
{
Console.WriteLine(
"
\nRice has been sold
"
);
}
public
int
Price
{
get
{
return
this
.price;}
set
{price
=
value;}
}
}
//
Bread
public
class
Bread:Food
{
public
Bread()
{
Console.WriteLine(
"
\nThe Bread is made
.
"
);
}
private
int
price;
public
void
Cook()
{
Console.WriteLine(
"
\nBread is cooking
"
);
}
public
void
Sell()
{
Console.WriteLine(
"
\nBread has been sold
"
);
}
public
int
Price
{
get
{
return
this
.price;}
set
{price
=
value;}
}
}
//
定义大厨,他包办这个快餐店里的所有Food,包括面条,面包和米饭
class
Chef
{
public
static
Food MakeFood(
string
foodName)
{
try
{
switch
(foodName)
{
case
"
noodle
"
:
return
new
Noodle();
case
"
rice
"
:
return
new
Rice();
case
"
bread
"
:
return
new
Bread();
default
:
throw
new
BadFoodException(
"
Bad food request!
"
);
}
}
catch
(BadFoodException e)
{
throw
e;
}
}
}
//
异常类,该餐馆没有的食品
class
BadFoodException: System.Exception
{
public
BadFoodException(
string
strMsg)
{
Console.WriteLine(strMsg);
}
}
/**/
///
<summary>
///
应用程序的主入口点。
///
</summary>
[STAThread]
static
void
Main(
string
[] args)
{
Food food
=
Chef.MakeFood(
"
bread
"
);
food.Cook();
food.Sell();
Console.ReadLine();
}
}
}
posted @ 2007-04-11 13:59
BoyLee
阅读(427)
评论(0)
编辑
收藏
所属分类:
Asp.Net
社区
新闻
新用户注册
刷新评论列表
标题
姓名
主页
Email
(只有博主才能看到)
验证码
*
看不清,换一张
[
登录
][
注册
]
内容(请不要发表任何与政治相关的内容)
Remember Me?
登录
使用高级评论
新用户注册
返回页首
恢复上次提交
[使用Ctrl+Enter键可以直接提交]
该文被作者在 2007-07-24 10:12 编辑过
另存
打印
所属分类的其他文章:
·
[转]petshop4.0设计说明
·
推荐几个asp.net测试空间
·
收藏: C#设计模式之简单工厂篇
·
收藏:简单工厂模式(SimpleFactory-C#)
·
vb.net版中文验证码
·
验证码 收藏
·
asp.net操作csv
·
asp.net操作excel
·
UltraWebGrid多条数据scoll bar定位解决
·
UltraWebGrid使用指南(原创)
最新IT新闻:
·
奇虎回应:瑞星半年免费版像是一个恶意软件
·
鲍尔默致员工信:部署09年微软5大工作重心
·
陈一舟:Facebook也抄袭过校内 不怕打官司
·
微软重组视窗与在线业务
·
我国网民数达2.53亿超美国居世界首位
博客园新闻频道
博客园首页
社区
公告
<
2007年4月
>
日
一
二
三
四
五
六
25
26
27
28
29
30
31
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
1
2
3
4
5
与我联系
发短消息
随笔分类
(49)
Asp.Net(14)
(rss)
C/C++(1)
(rss)
JS/AJAX(1)
(rss)
Others(2)
(rss)
Win Form(1)
(rss)
非技术类(27)
(rss)
日语学习(1)
(rss)
数据库(2)
(rss)
随笔档案
(54)
2008年7月 (1)
2008年4月 (2)
2008年2月 (1)
2007年12月 (1)
2007年11月 (3)
2007年10月 (1)
2007年9月 (3)
2007年8月 (11)
2007年7月 (6)
2007年5月 (2)
2007年4月 (9)
2007年3月 (8)
2007年1月 (1)
2006年11月 (1)
2006年10月 (1)
2006年9月 (3)
友情链接
BoyLee's Blog
(rss)
Ktgu
Temptation
(rss)
戏水
(rss)
小 8
搜索
积分与排名
积分 - 40900
排名 - 977
最新随笔
1. 希望大家不要为了首页而首页
2. 突然发现很少在这里写东西了
3. 哎......
4. 周末公司新人见闻录
5. 招聘面试趣闻录
6. 程序产品商业包装的重要性
7. 多收了三五斗(在日IT民工版)
8. Microsoft IME 操作(日文输入)
9. 刚颁布的中国贫富标准线,来看看程序员生活在哪个阶层
10. 不知道是我技术提高了,还是园子堕落了
最新评论
1. re: UltraWebGrid使用系列(三)
什么东西? 笑死个人~~~~~~ (教学改革)
阅读排行榜
1. UltraWebGrid使用系列(一)(3495)
2. UltraWebGrid使用系列(三) (2528)
3. UltraWebGrid使用系列(二)(1408)
4. UltraWebGrid使用系列(四)(1343)
5. UltraWebGrid使用指南(原创) (1235)