简单工厂模式(SimpleFactory-C#)

news/2024/7/4 1:45:43 标签: class, string, interface, system, 工作
class="baidu_pl">
class="article_content clearfix">
class="htmledit_views"> 快餐店提供很多食物,比如 面条,米饭,面包。首先定义了一个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(); 
}
 
}
 
}
 
 

http://www.niftyadmin.cn/n/1415528.html

相关文章

解决Ubuntu14.04中firefox打开网页缓慢的问题

参考第二种解决方式&#xff1a;刚装上的ubuntu 12.10 &#xff0c; 总是感觉网速有点慢&#xff0c;而且是那种有点特别的慢&#xff0c;受不鸟啊。网上查了一下&#xff0c;大概有两种说法。说法一&#xff1a;域名解析的问题。据说是每次都会进行一次域名解析&#xff0c;所…

Python类属性与实例属性

类属性为所有实例拥有&#xff0c;实例属性为单个实例拥有 class CLS(object):count 0_count 0def __init__(self):CLS.count CLS.count 1self._count self._count 1pCLS() print(类属性&#xff1a;%d%p.count) print(实例属性&#xff1a;%d%p._count)pCLS() print(…

Python 爬虫 多进程清洗代理

利用多线程检测代理网站提供的免费代理是否可用 1 import requests2 from lxml import etree3 import time4 import multiprocessing5 6 def get_all_proxy(queue):7 url http://www.xicidaili.com/nn/18 headers {9 User-Agent: Mozilla/5.0 (Windows NT 6.…

C#设计模式之简单工厂篇

首先定义一个接口&#xff0c;具体名为Idatabase,在这个接口中&#xff0c;定义好数据库操作的方法名和参数&#xff0c;以及返回值&#xff0c;本案例中我定义如下方法&#xff1a; public interface IDatabase { bool Connect(string ConnectString); bool Open(); bool…

nagios监控流量脚本

需求是我们需要对服务器上的流量进行监控&#xff0c;网络上有个流传的check_traffic.sh&#xff0c;它需要被监控机开启snmp。但是感觉都使用上了nagios还要开snmp。。。有点斧子剪刀一起用的感觉&#xff0c;所以就动手写了个监控流量的shell&#xff1a; #!/bin/shusage() {…

Jupyter交互控件

引入交互工具模块 from ipywidgets import interact import ipywidgets as widgets滑块 def f(x):return xinteract(f, x10,);复选框 interact(f, xTrue);文本输入框 interact(f, xHi there!);播放器 play widgets.Play(# interval10,value50,min0,max100,step1,descript…

如何用sql语句对性别的 约束条件让它只能填 男 或 女

ALTER TABLE tb WITH NOCHECK ADD CONSTRAINT col1_check CHECK (性别 in(男,女))

java中3种常用的日期Date

第一种 位于java.util.Date 第二种 位于java.util.Calendar 第三种 位于java.text.SimpleDateFormat 转载于:https://www.cnblogs.com/wym-1999/p/9520353.html