CSharp: Abstract Factory 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
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
/// <summary>
   /// Abstract Factory抽像工厂
   /// geovindu,Geovin Du  eidt
   /// </summary>
   public interface IShape
   {
       void Draw();
   }
   /// <summary>
   ///
   /// </summary>
   public class Square : IShape
   {
       public void Draw() => Console.WriteLine("Basic square");
   }
   /// <summary>
   ///
   /// </summary>
   public class Rectangle : IShape
   {
       public void Draw() => Console.WriteLine("Basic rectangle");
   }
   /// <summary>
   ///
   /// </summary>
   public class RoundedSquare : IShape
   {
       public void Draw() => Console.WriteLine("Rounded square");
   }
   /// <summary>
   ///
   /// </summary>
   public class RoundedRectangle : IShape
   {
       public void Draw() => Console.WriteLine("Rounded rectangle");
   }
   /// <summary>
   ///
   /// </summary>
   public enum Shape
   {
       Square,
       Rectangle
   }
   /// <summary>
   ///
   /// </summary>
   public abstract class ShapeFactory
   {
       public abstract IShape Create(Shape shape);
   }
   /// <summary>
   ///
   /// </summary>
   public class BasicShapeFactory : ShapeFactory
   {
       public override IShape Create(Shape shape)
       {
           switch (shape)
           {
               case Shape.Square:
                   return new Square();
               case Shape.Rectangle:
                   return new Rectangle();
               default:
                   throw new ArgumentOutOfRangeException(
                     nameof(shape), shape, null);
           }
       }
   }
   /// <summary>
   ///
   /// </summary>
   public class RoundedShapeFactory : ShapeFactory
   {
       public override IShape Create(Shape shape)
       {
           switch (shape)
           {
               case Shape.Square:
                   return new RoundedSquare();
               case Shape.Rectangle:
                   return new RoundedRectangle();
               default:
                   throw new ArgumentOutOfRangeException(nameof(shape), shape, null);
           }
       }
   }
   /// <summary>
   ///
   /// </summary>
   public class Demo
   {
       public static ShapeFactory GetFactory(bool rounded)
       {
           if (rounded)
               return new RoundedShapeFactory();
           else
               return new BasicShapeFactory();
       }
 
   }

  

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
/// <summary>
  /// Abstract Factory抽像工厂
  /// geovindu,Geovin Du  eidt
  /// </summary>
  public interface IAnimalFactory
  {
      IDog GetDog();
      ITiger GetTiger();
  }
 
  /// <summary>
  /// Abstract Product-1
  /// </summary>
  public interface ITiger
  {
      void AboutMe();
  }
  /// <summary>
  /// Abstract Product-2
  /// </summary>
  public interface IDog
  {
      void AboutMe();
  }
 
  /// <summary>
  /// Concrete product-A1(WildTiger)
  /// </summary>
  class WildTiger : ITiger
  {
      public void AboutMe()
      {
          Console.WriteLine("涂氏 tiger says: I prefer hunting in jungles.Halum.");
      }
  }
  /// <summary>
  ///  Concrete product-B1(WildDog)
  /// </summary>
  class WildDog : IDog
  {
      public void AboutMe()
      {
          Console.WriteLine("涂氏 dog says: I prefer to roam freely in jungles.Bow-Wow.");
      }
  }
 
  /// <summary>
  /// Concrete product-A2(PetTiger)
  /// </summary>
  class PetTiger : ITiger
  {
 
      /// <summary>
      ///
      /// </summary>
      public void AboutMe()
      {
          Console.WriteLine("李氏 tiger says: Halum.I play in an animal circus.");
      }
  }
 
  /// <summary>
  /// Concrete product-B2(PetDog)
  /// </summary>
  class PetDog : IDog
  {
      public void AboutMe()
      {
          Console.WriteLine("李氏 dog says: Bow-Wow.I prefer to stay at home.");
      }
  }
  /// <summary>
  /// Concrete Factory 1-Wild Animal Factory
  /// </summary>
  public class WildAnimalFactory : IAnimalFactory
  {
 
      public ITiger GetTiger()
      {
          return new WildTiger();
      }
      public IDog GetDog()
      {
          return new WildDog();
      }
  }
  /// <summary>
  /// Concrete Factory 2-Pet Animal Factory
  /// </summary>
  public class PetAnimalFactory : IAnimalFactory
  {
      public IDog GetDog()
      {
          return new PetDog();
      }
 
      public ITiger GetTiger()
      {
          return new PetTiger();
      }
  }
  /// <summary>
  /// Factory provider
  /// </summary>
  class FactoryProvider
  {
      /// <summary>
      ///
      /// </summary>
      /// <param name="factoryType"></param>
      /// <returns></returns>
      /// <exception cref="ArgumentException"></exception>
      public static IAnimalFactory GetAnimalFactory(string factoryType)
      {
          if (factoryType.Contains("涂氏"))
          {
              // Returning a WildAnimalFactory
              return new WildAnimalFactory();
          }
          else       
         if (factoryType.Contains("李氏"))
          {
              // Returning a PetAnimalFactory
              return new PetAnimalFactory();
          }
          else
          {
              throw new ArgumentException("You need to pass either wild or pet as argument.");
          }
      }
  }

  

调用:

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
//Abstract Factory抽像工厂
           Console.WriteLine("***Abstract Factory Pattern Demo.***\n");
 
           //Making a wild dog and wild tiger through WildAnimalFactory
           IAnimalFactory animalFactory = FactoryProvider.GetAnimalFactory("涂氏");
           IDog dog = animalFactory.GetDog();
           ITiger tiger = animalFactory.GetTiger();
           dog.AboutMe();
           tiger.AboutMe();
 
           Console.WriteLine("******************");
 
           //Making a pet dog and pet tiger through PetAnimalFactory now.
           animalFactory = FactoryProvider.GetAnimalFactory("李氏");
           dog = animalFactory.GetDog();
           tiger = animalFactory.GetTiger();
           dog.AboutMe();
           tiger.AboutMe();
 
           //
           var basic = Demo.GetFactory(false);
           var basicRectangle = basic.Create(Shape.Rectangle);
           basicRectangle.Draw();
 
           var roundedSquare = Demo.GetFactory(true).Create(Shape.Square);
           roundedSquare.Draw();
 
           Console.ReadLine();

  

输出:

1
2
3
4
5
6
7
8
9
10
***Abstract Factory Pattern Demo.***
 
涂氏 dog says: I prefer to roam freely in jungles.Bow-Wow.
涂氏 tiger says: I prefer hunting in jungles.Halum.
******************
李氏 dog says: Bow-Wow.I prefer to stay at home.
李氏 tiger says: Halum.I play in an animal circus.
Basic rectangle
Rounded square
***Abstract Factory Pattern Demo.***

  

 

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