CSharp: Adapter 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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/// <summary>
   /// 适配器模式 Adapter PatternA
   /// geovindu, Geovin Du edit
   /// </summary>
   interface IRectangle
   {
       void AboutMe();
   }
 
   /// <summary>
   ///
   /// </summary>
   class Rectangle : IRectangle
   {
       public void AboutMe()
       {
           Console.WriteLine("Actually, I am a Rectangle(三角形).");
       }
   }
   /// <summary>
   ///
   /// </summary>
   interface ITriangle
   {
       void AboutTriangle();
   }
 
   /// <summary>
   ///
   /// </summary>
   class Triangle : ITriangle
   {
       public void AboutTriangle()
       {
           Console.WriteLine("Actually, I am a Triangle(矩形).");
       }
   }
 
   /*
    * RectangleAdapter is implementing IRectangle.
    * So, it needs to implement all the methods
    * defined in the target interface.
    */
 
   /// <summary>
   ///
   /// </summary>
   class RectangleAdapter : Triangle, IRectangle
   {
       public void AboutMe()
       {
           //Invoking the adaptee method
           //For Q&A
           //Console.WriteLine("You are using an adapter now.");
           AboutTriangle();
       }
   }

  

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
/// <summary>
   /// 适配器模式 Adapter PatternA
   /// geovindu, Geovin Du edit
   /// </summary>
   public interface ICommand
   {
       void Execute();
   }
   /// <summary>
   ///
   /// </summary>
   public class SaveCommand : ICommand
   {
       public void Execute()
       {
           Console.WriteLine("Saving current file");
       }
   }
   /// <summary>
   ///
   /// </summary>
   public class OpenCommand : ICommand
   {
       public void Execute()
       {
           Console.WriteLine("Opening a file");
       }
   }
   /// <summary>
   ///
   /// </summary>
   public class Button
   {
       private ICommand command;
       private string name;
 
       public Button(ICommand command, string name)
       {
           this.command = command;
           this.name = name;
       }
 
       public void Click()
       {
           command.Execute();
       }
 
       public void PrintMe()
       {
           Console.WriteLine($"I am a button called {name}");
       }
   }
   /// <summary>
   ///
   /// </summary>
   public class Editor
   {
       public IEnumerable<Button> Buttons { get; }
 
       public Editor(IEnumerable<Button> buttons)
       {
           Buttons = buttons;
       }
 
       public void ClickAll()
       {
           foreach (var btn in Buttons)
           {
               btn.Click();
           }
       }
   }

  

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
   /// <summary>
     /// 适配器模式 Adapter PatternA
    /// geovindu, Geovin Du edit
  /// </summary>
  public interface IInteger
{
    int Value { get; }
}
 /// <summary>
 ///
 /// </summary>
public static class Dimensions
{
    public class Two : IInteger
    {
        public int Value => 2;
    }
 
    public class Three : IInteger
    {
        public int Value => 3;
    }
}
/// <summary>
///
/// </summary>
/// <typeparam name="TSelf"></typeparam>
/// <typeparam name="T"></typeparam>
/// <typeparam name="D"></typeparam>
public abstract class Vector<TSelf, T, D>
  where D : IInteger, new()
  where TSelf : Vector<TSelf, T, D>, new()
{
    protected T[] data;
 
    public Vector()
    {
        data = new T[new D().Value];
    }
 
    public Vector(params T[] values)
    {
        var requiredSize = new D().Value;
        data = new T[requiredSize];
 
        var providedSize = values.Length;
 
        for (int i = 0; i < Math.Min(requiredSize, providedSize); ++i)
            data[i] = values[i];
    }
 
    public static TSelf Create(params T[] values)
    {
        var result = new TSelf();
        var requiredSize = new D().Value;
        result.data = new T[requiredSize];
 
        var providedSize = values.Length;
 
        for (int i = 0; i < Math.Min(requiredSize, providedSize); ++i)
            result.data[i] = values[i];
 
        return result;
    }
 
    public T this[int index]
    {
        get => data[index];
        set => data[index] = value;
    }
 
    public T X
    {
        get => data[0];
        set => data[0] = value;
    }
}
/// <summary>
///
/// </summary>
/// <typeparam name="TSelf"></typeparam>
/// <typeparam name="D"></typeparam>
public class VectorOfFloat<TSelf, D>
  : Vector<TSelf, float, D>
  where D : IInteger, new()
  where TSelf : Vector<TSelf, float, D>, new()
{
}
/// <summary>
///
/// </summary>
/// <typeparam name="D"></typeparam>
public class VectorOfInt<D> : Vector<VectorOfInt<D>, int, D>
  where D : IInteger, new()
{
    public VectorOfInt()
    {
    }
 
    public VectorOfInt(params int[] values) : base(values)
    {
    }
 
    public static VectorOfInt<D> operator +
      (VectorOfInt<D> lhs, VectorOfInt<D> rhs)
    {
        var result = new VectorOfInt<D>();
        var dim = new D().Value;
        for (int i = 0; i < dim; i++)
        {
            result[i] = lhs[i] + rhs[i];
        }
 
        return result;
    }
}
/// <summary>
///
/// </summary>
public class Vector2i : VectorOfInt<Dimensions.Two>
{
    public Vector2i(params int[] values) : base(values)
    {
    }
}
/// <summary>
///
/// </summary>
public class Vector3f
  : VectorOfFloat<Vector3f, Dimensions.Three>
{
    public override string ToString()
    {
        return $"{string.Join(",", data)}";
    }
}

  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/// <summary>
   ///
   /// </summary>
   public class CountryStats
   {
       [XmlIgnore]
       public Dictionary<string, string> Capitals { get; set; }
         = new Dictionary<string, string>();
 
       public (string, string)[] CapitalsSerializable
       {
           get
           {
               return Capitals.Keys.Select(country =>
                 (country, Capitals[country])).ToArray();
           }
           set
           {
               Capitals = value.ToDictionary(x => x.Item1, x => x.Item2);
           }
       }
   }

 调用: 

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
//适配器模式 Adapter Pattern
           Console.WriteLine("***适配器模式 Adapter Pattern Alternative Implementation Technique Demo.***\n");
           IRectangle rectangle = new Rectangle();
           Console.WriteLine("For initial verification purposes, printing the details from of both shapes.");
           Console.WriteLine("The rectangle.AboutMe() says:");
           rectangle.AboutMe();
           ITriangle triangle = new Triangle();
           Console.WriteLine("The triangle.AboutTriangle() says:");
           triangle.AboutTriangle();
 
           Console.WriteLine("\nNow using the adapter.");
           IRectangle adapter = new RectangleAdapter();
           Console.Write("True fact : ");
           adapter.AboutMe();
           Console.WriteLine("*****************************");
 
           //
           // for each ICommand, a Button is created to wrap it, and all
           // are passed to the editor
           var b = new ContainerBuilder();
           b.RegisterType<OpenCommand>()
             .As<ICommand>()
             .WithMetadata("Name", "Open");
           b.RegisterType<SaveCommand>()
             .As<ICommand>()
             .WithMetadata("Name", "Save");
           //b.RegisterType<Button>();
           //b.RegisterAdapter<ICommand, Button>(cmd => new Button(cmd, ""));
           b.RegisterAdapter<Meta<ICommand>, Button>(cmd =>
             new Button(cmd.Value, (string)cmd.Metadata["Name"]));
           b.RegisterType<Editor>();
 
           using var c = b.Build();
           var editor = c.Resolve<Editor>();
           editor.ClickAll();
 
           // problem: only one button
 
           foreach (var btn in editor.Buttons)
               btn.PrintMe();
 
           //
           var v = new Vector2i(1, 2);
           v[0] = 0;
           var vv = new Vector2i(3, 2);
           var result = v + vv;
           var u = Vector3f.Create(3.5f, 2.2f, 1);
           Console.WriteLine(u.ToString());
 
           //
           var stats = new CountryStats();
           stats.Capitals.Add("France", "Paris");
           var xs = new XmlSerializer(typeof(CountryStats));
           var stringBuilder = new StringBuilder();
           var stringWriter = new StringWriter(stringBuilder);
           xs.Serialize(stringWriter, stats);
           //Clipboard.SetText(stringBuilder.ToString());
           Console.WriteLine(stringBuilder.ToString());
           var newStats = (CountryStats)xs.Deserialize(
             new StringReader(stringWriter.ToString()));
           Console.WriteLine(newStats.Capitals["France"]);

  

输出:

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
***适配器模式 Adapter Pattern Alternative Implementation Technique Demo.***
 
For initial verification purposes, printing the details from of both shapes.
The rectangle.AboutMe() says:
Actually, I am a Rectangle(三角形).
The triangle.AboutTriangle() says:
Actually, I am a Triangle(矩形).
 
Now using the adapter.
True fact : Actually, I am a Triangle(矩形).
*****************************
Opening a file
Saving current file
I am a button called Open
I am a button called Save
3.5,2.2,1
<?xml version="1.0" encoding="utf-16"?>
<CountryStats xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <CapitalsSerializable>
    <ValueTupleOfStringString>
      <Item1>France</Item1>
      <Item2>Paris</Item2>
    </ValueTupleOfStringString>
  </CapitalsSerializable>
</CountryStats>
Paris

  

posted @   ®Geovin Du Dream Park™  阅读(25)  评论(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
点击右上角即可分享
微信分享提示