CSharp: Facade Pattern in donet core 3

 

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
31
32
33
34
35
36
37
38
39
40
41
42
43
/// <summary>
  /// 外观模式Facade Pattern
  /// geovindu,Geovin Du eidt
  /// 机器人颜色
  /// </summary>
  public class RobotColor
  {
 
      /// <summary>
      /// 颜色名称
      /// </summary>
      string color;
 
      /// <summary>
      ///
      /// </summary>
      /// <param name="color"></param>
      public RobotColor(string color)
      {
          this.color = color;
      }
      /// <summary>
      /// 设置颜色
      /// </summary>
      public void SetColor()
      {
          if (color == "steel")
          {
              Console.WriteLine($"The default color {color} is set for the robot.");
          }
          else
          {
              Console.WriteLine($"Painting the robot with your favourite {color} color.");
          }
      }
      /// <summary>
      /// 移除颜色
      /// </summary>
      public void RemoveColor()
      {
          Console.WriteLine("Attempting to remove the colors from the robot.");
      }
  }

  

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
/// <summary>
 /// 外观模式Facade Pattern
 /// geovindu,Geovin Du eidt
 /// 机器人身体
 /// </summary>
 class RobotBody
 {
     /// <summary>
     /// 类型
     /// </summary>
     string robotType;
     /*
     * To keep a count of number of robots.
     * This operation is optional for you.
    */
     /// <summary>
     /// 数量
     /// </summary>
     static int count = 0;
 
     /// <summary>
     ///
     /// </summary>
     /// <param name="robotType">输入类型名称</param>
     public RobotBody(string robotType)
     {
         this.robotType = robotType;
     }
 
     /// <summary>
     /// 制造机器人身体
     /// </summary>
     public void MakeRobotBody()
     {
       Console.WriteLine($"Constructing one {robotType} robot.");
       Console.WriteLine("Robot creation finished.");
       Console.WriteLine($"Total number of robot created at this moment:(目前机器人总数为){++count}");  //制造一个,数量上加一
     }
     /// <summary>
     /// 破坏机器人身体
     /// </summary>
     public void DestroyRobotBody()
     {
         if (count > 0)
         {
             --count; //破坏一个,数量减一
             Console.WriteLine($"Robot's destruction process is over.剩余总数为:{count}");
         }
         else
         {
             Console.WriteLine("All robots are destroyed already.所有都已经损坏,没有可以做的。");
             Console.WriteLine("Color removal operation will not continue.");
         }
     }
 }

  

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/// <summary>
 /// 外观模式Facade Pattern
 /// geovindu,Geovin Du eidt
 ///
 /// </summary>
 class RobotFacade
 {
     RobotBody robotBody;
     RobotColor robotColor;   
      
     /// <summary>
     /// 构造一个类
     /// </summary>
     /// <param name="robotType">输入类型名称</param>
     /// <param name="color">输入设置颜色</param>
     public RobotFacade(string robotType, string color = "steel")
     {
         robotBody = new RobotBody(robotType);
         robotColor = new RobotColor(color);
     }
     /// <summary>
     /// 创建一个机器人方法
     /// </summary>
     public void ConstructRobot()
     {
         Console.WriteLine("Robot creation through facade starts...(造造一个)");
 
         robotBody.MakeRobotBody();
          
         robotColor.SetColor();
 
         Console.WriteLine();
     }
     /// <summary>
     /// 损坏一个机器人方法
     /// </summary>
     public void DestroyRobot()
     {
         Console.WriteLine("Making an attempt to destroy one robot using the facade now.(损坏一个)");
 
         robotColor.RemoveColor();
 
         robotBody.DestroyRobotBody();
 
         Console.WriteLine();
     }
 }

  

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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
/// <summary>
  /// 外观模式Facade Pattern
  /// geovindu,Geovin Du eidt
  /// </summary>
  public class Generator
  {
      private static readonly Random random = new Random();
      /// <summary>
      ///
      /// </summary>
      /// <param name="count">3</param>
      /// <returns></returns>
      public List<int> Generate(int count)
      {
          return Enumerable.Range(0, count)
            .Select(_ => random.Next(1, 6))
            .ToList();
      }
  }
  /// <summary>
  ///
  /// </summary>
  public class Splitter
  {
      public List<List<int>> Split(List<List<int>> array)
      {
          var result = new List<List<int>>();
 
          var rowCount = array.Count;
          var colCount = array[0].Count;
 
          // get the rows
          for (int r = 0; r < rowCount; ++r)
          {
              var theRow = new List<int>();
              for (int c = 0; c < colCount; ++c)
                  theRow.Add(array[r][c]);
              result.Add(theRow);
          }
 
          // get the columns
          for (int c = 0; c < colCount; ++c)
          {
              var theCol = new List<int>();
              for (int r = 0; r < rowCount; ++r)
                  theCol.Add(array[r][c]);
              result.Add(theCol);
          }
 
          // now the diagonals
          var diag1 = new List<int>();
          var diag2 = new List<int>();
          for (int c = 0; c < colCount; ++c)
          {
              for (int r = 0; r < rowCount; ++r)
              {
                  if (c == r)
                      diag1.Add(array[r][c]);
                  var r2 = rowCount - r - 1;
                  if (c == r2)
                      diag2.Add(array[r][c]);
              }
          }
 
          result.Add(diag1);
          result.Add(diag2);
 
          return result;
      }
  }
  /// <summary>
  ///
  /// </summary>
  public class Verifier
  {
      public bool Verify(List<List<int>> array)
      {
          if (!array.Any()) return false;
 
          var expected = array.First().Sum();
 
          return array.All(t => t.Sum() == expected);
      }
  }
  /// <summary>
  ///
  /// </summary>
  public class MagicSquareGenerator
  {
 
      /// <summary>
      /// 生成实例
      /// </summary>
      /// <param name="size"></param>
      /// <param name="outstr"></param>
      /// <returns></returns>
      public List<List<int>> Generate(int size,out string outstr)
      {
          var g = new Generator();
          var s = new Splitter();
          var v = new Verifier();
 
          var square = new List<List<int>>();
 
          do
          {
              square = new List<List<int>>();
              for (int i = 0; i < size; ++i)
                  square.Add(g.Generate(size));
          } while (!v.Verify(s.Split(square)));
 
          string str = SquareToString(square);
          outstr = str;
          //Console.WriteLine(str);
          return square;
      }
      /// <summary>
      ///
      /// </summary>
      /// <param name="square"></param>
      /// <returns></returns>
      private string SquareToString(List<List<int>> square)
      {
          var sb = new StringBuilder();
          foreach (var row in square)
          {
              sb.AppendLine(string.Join(" ",
                row.Select(x => x.ToString())));
          }
 
          return sb.ToString();
      }
  }
  /// <summary>
  ///
  /// </summary>
  public class MyVerifier
  {
 
      /// <summary>
      ///
      /// </summary>
      /// <param name="array"></param>
      /// <returns></returns>
      public bool Verify(List<List<int>> array)
      {
          if (!array.Any()) return false;
 
          var rowCount = array.Count;
          var colCount = array[0].Count;
 
          var expected = array.First().Sum();
 
          for (var row = 0; row < rowCount; ++row)
              if (array[row].Sum() != expected)
                  return false;
 
          for (var col = 0; col < colCount; ++col)
              if (array.Select(a => a[col]).Sum() != expected)
                  return false;
 
          var diag1 = new List<int>();
          var diag2 = new List<int>();
          for (var r = 0; r < rowCount; ++r)
              for (var c = 0; c < colCount; ++c)
              {
                  if (r == c)
                      diag1.Add(array[r][c]);
                  var r2 = rowCount - r - 1;
                  if (r2 == c)
                      diag2.Add(array[r][c]);
              }
 
          return diag1.Sum() == expected && diag2.Sum() == expected;
      }
  }

  

调用:

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
//外观模式Facade Pattern
           Console.WriteLine("***外观模式 Facade Pattern Demo.***\n");           
           //Making a Milano robot with green color.
           RobotFacade facade = new RobotFacade("Milano","green");
           facade.ConstructRobot();
           //Making a robonaut robot with default steel color.          
           facade = new RobotFacade("Robonaut");
           facade.ConstructRobot();
           //Destroying one robot
           facade.DestroyRobot();
           //Destroying another robot
           facade.DestroyRobot();
           //This destrcution attempt should fail.
           facade.DestroyRobot();
 
 
           //
           string geovindu = "";
           var gen = new MagicSquareGenerator();
           var square = gen.Generate(3,out geovindu);
 
           Console.WriteLine(geovindu);
 
           var v = new MyVerifier(); // prevents cheating :)
           Console.WriteLine(v.Verify(square));
 
           Console.ReadLine();

  

输出:

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
31
32
***外观模式 Facade Pattern Demo.***
 
Robot creation through facade starts...(造造一个)
Constructing one Milano robot.
Robot creation finished.
Total number of robot created at this moment:(目前机器人总数为)1
Painting the robot with your favourite green color.
 
Robot creation through facade starts...(造造一个)
Constructing one Robonaut robot.
Robot creation finished.
Total number of robot created at this moment:(目前机器人总数为)2
The default color steel is set for the robot.
 
Making an attempt to destroy one robot using the facade now.(损坏一个)
Attempting to remove the colors from the robot.
Robot's destruction process is over.剩余总数为:1
 
Making an attempt to destroy one robot using the facade now.(损坏一个)
Attempting to remove the colors from the robot.
Robot's destruction process is over.剩余总数为:0
 
Making an attempt to destroy one robot using the facade now.(损坏一个)
Attempting to remove the colors from the robot.
All robots are destroyed already.所有都已经损坏,没有可以做的。
Color removal operation will not continue.
 
5 5 5
5 5 5
5 5 5
 
True

  

posted @   ®Geovin Du Dream Park™  阅读(45)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
< 2025年3月 >
23 24 25 26 27 28 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 31 1 2 3 4 5
点击右上角即可分享
微信分享提示