C#-単純ファクトリモード
11171 ワード
コード:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace
8 {
9 class Program
10 {
11 static void Main(string[] args)
12 {
13 Console.Write(" :");
14 string brand = Console.ReadLine();
15 Notepad nt = GC(brand);
16 nt.SayHello();
17 Console.ReadKey();
18 }
19
20
21 /// <summary>
22 ///
23 /// </summary>
24 /// <param name="brand"></param>
25 /// <returns></returns>
26 public static Notepad GC(string brand)
27 {
28 Notepad nt = null;
29
30 switch (brand)
31 {
32 //
33 case "Lenovo": nt = new Lenovo(); break;
34 case "Acer": nt = new Acer(); break;
35 case "IBM": nt = new IBM(); break;
36 default:break;
37 }
38
39 return nt;
40 }
41 }
42
43 /// <summary>
44 ///
45 /// </summary>
46 public abstract class Notepad
47 {
48 public abstract void SayHello();
49 }
50
51 /// <summary>
52 ///
53 /// </summary>
54 public class Acer : Notepad
55 {
56 public override void SayHello()
57 {
58 Console.WriteLine(" !");
59 }
60 }
61
62 /// <summary>
63 ///
64 /// </summary>
65 public class Lenovo : Notepad
66 {
67 public override void SayHello()
68 {
69 Console.WriteLine(" !");
70 }
71 }
72
73 /// <summary>
74 /// IBM
75 /// </summary>
76 public class IBM : Notepad
77 {
78 public override void SayHello()
79 {
80 Console.WriteLine(" IBM!");
81 }
82 }
83 }