简单工厂之简单模型(uml)
下面是程序:
using System;<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />
?
namespace SimpleFactory
{
???? class Class1
???? {
???????? [STAThread]
???????? static void <?xml:namespace prefix = st1 ns = "urn:schemas-microsoft-com:office:smarttags" />Main(string[] args)
???????? {
????????????? 食物 food=大廚師.要做的食物("面條");
????????????? food.做食物();
????????????? food.賣食物();
????????????? Console.ReadLine();
???????? }
???? }
???? ///
???? /// 定義食物接口
???? ///
???? public interface 食物
???? {
???????? void 做食物();
???????? void 賣食物();
???? }
???? ///
???? /// 拋出大廚師不能做的食物
???? ///
???? public class BadFoodException : System.Exception
???? {
???????? public BadFoodException(string str)
???????? {
????????????? System.Console.WriteLine(str);
???????? }
???? }
???? ///
???? /// 面條
???? ///
???? public class 面條 : 食物
???? {
???????? public 面條()
???????? {
????????????? System.Console.WriteLine("這是面條");
???????? }
???????? public void 做食物()
???????? {
????????????? System.Console.WriteLine("現在煮面條");
???????? }
???????? public void 賣食物()
???????? {
????????????? System.Console.WriteLine("現在賣面條");
???????? }
???? }
???? ///
???? /// 面包
???? ///
???? public class 面包 : 食物
???? {
???????? public 面包()
???????? {
????????????? System.Console.WriteLine("這是面包");
???????? }
???????? public void 做食物()
???????? {
????????????? System.Console.WriteLine("現在做面包");
???????? }
???????? public void 賣食物()
???????? {
????????????? System.Console.WriteLine("現在賣米飯");
???????? }
???? }
???? ///
???? /// 米飯
???? ///
???? public class 米飯 : 食物
???? {
???????? public 米飯()
???????? {
????????????? System.Console.WriteLine("這是米飯");
???????? }
???????? public void 做食物()
???????? {
????????????? System.Console.WriteLine("現在煮米飯");
???????? }
???????? public void 賣食物()
???????? {
????????????? System.Console.WriteLine("現在賣米飯");
???????? }
???? }
???? ///
???? /// 大廚師
???? ///
???? public class 大廚師
???? {
???????? public static 食物要做的食物(string food)
???????? {
????????????? try
????????????? {
?????????????????? switch(food)
?????????????????? {
?????????????????????? case "面包":
??????????????????????????? return new 面包();
?????????????????????? case "米飯":
??????????????????????????? return new 米飯();
?????????????????????? case "面條":
??????????????????????????? return new 面條();
?????????????????????? default:
????????????? ????????????? throw new BadFoodException("沒有這種食物");
?????????????????? }
????????????? }
????????????? catch(BadFoodException e)
????????????? {
?????????????????? throw e;
????????????? }
???????? }
???? }
}
?
第一次作uml圖,請各位多多斧正阿!先謝過了。
轉載于:https://www.cnblogs.com/wssmax/archive/2005/09/08/232308.html
總結
以上是生活随笔為你收集整理的简单工厂之简单模型(uml)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: C++学习笔记之二:通过结构体来理解类
- 下一篇: 将*.STL转换成顶点索引格式