zoukankan      html  css  js  c++  java
  • 设计模式

    设计模式

    设计这个项目的一种方式。

    简单工厂设计模式

     

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 
     7 namespace _03简单工厂设计模式
     8 {
     9     class Program
    10     {
    11         static void Main(string[] args)
    12         {
    13             Console.WriteLine("请输入电脑的品牌");
    14             string input = Console.ReadLine();
    15             NoteBook nb = GetComupter(input);
    16             nb.SayHello();
    17             Console.ReadKey();
    18         }
    19 
    20         /// <summary>
    21         /// 简单工厂设计模式,根据用户的输入来返回一个父类,里面装的是个子类对象
    22         /// </summary>
    23         /// <param name="name"></param>
    24         /// <returns></returns>
    25         public static NoteBook GetComupter(string name)
    26         {
    27             NoteBook nb = null;
    28             switch (name)
    29             {
    30                 case "戴尔":
    31                     nb = new Dell();
    32                     break;
    33                 case "IBM":
    34                     nb = new IBM();
    35                     break;
    36                 case "宏基":
    37                     nb = new Acer();
    38                     break;
    39                 case "联想":
    40                     nb = new Lenovo();
    41                     break;
    42             }
    43 
    44             return nb;
    45         }
    46     }
    47     public abstract class NoteBook
    48     {
    49         public abstract void SayHello();
    50     }
    51 
    52     public class Lenovo : NoteBook
    53     {
    54         public override void SayHello()
    55         {
    56             Console.WriteLine("我是联想电脑");
    57         }
    58     }
    59 
    60     public class Acer : NoteBook
    61     {
    62         public override void SayHello()
    63         {
    64             Console.WriteLine("我是宏基电脑");
    65         }
    66     }
    67 
    68     public class Dell : NoteBook
    69     {
    70         public override void SayHello()
    71         {
    72             Console.WriteLine("我是戴尔电脑");
    73         }
    74     }
    75 
    76     public class IBM : NoteBook
    77     {
    78         public override void SayHello()
    79         {
    80             Console.WriteLine("我是IBM电脑");
    81         }
    82     }
    83 
    84 
    85 }
    View Code
  • 相关阅读:
    N个数字每X个数字组成一组,求组数
    生成带文本的UIImage
    Linux创建环境变量(Mac OS)
    为UIView绘制单边的boder
    ecshop之随机文章
    微软继MVC5后,出现ASP.NET VNEXT
    本科毕业生转正之前谈待遇
    ecshop title优化
    百度地图开发之一】申请Key和配置初览显示地图
    项目总结—jQuery EasyUI-DataGrid 拼表及查看详情
  • 原文地址:https://www.cnblogs.com/luozihua/p/9979598.html
Copyright © 2011-2022 走看看