创造型设计模式

打印 上一主题 下一主题

主题 993|帖子 993|积分 2983

创造型设计模式:单例、原型、三大工厂、建造者

在软件开辟中,设计模式是办理常见问题的经典办理方案。创造性设计模式主要关注对象的创建机制,确保系统在创建对象时具有灵活性和可扩展性。本文将介绍几种常见的创造型设计模式,包括单例模式、原型模式、三大工厂模式(简单工厂、工厂方法、抽象工厂)以及建造者模式
1. 单例模式(Singleton Pattern)

单例模式确保一个类只有一个实例,并提供一个全局访问点。这在需要控制资源访问或共享资源时非常有效。
示例代码

  1. public class Singleton
  2. {
  3.     private static Singleton _instance;
  4.     // 私有构造函数,防止外部实例化
  5.     private Singleton() { }
  6.     public static Singleton Instance
  7.     {
  8.         get
  9.         {
  10.             if (_instance == null)
  11.             {
  12.                 _instance = new Singleton();
  13.             }
  14.             return _instance;
  15.         }
  16.     }
  17.     public void DoSomething()
  18.     {
  19.         Console.WriteLine("Singleton is doing something.");
  20.     }
  21. }
  22. // 使用单例
  23. class Program
  24. {
  25.     static void Main(string[] args)
  26.     {
  27.         Singleton singleton = Singleton.Instance;
  28.         singleton.DoSomething();
  29.     }
  30. }
复制代码
2. 原型模式(Prototype Pattern)

原型模式通过复制现有对象来创建新对象,而不是通过实例化类。这在创建成本较高的对象时非常有效。
示例代码

  1. public abstract class Prototype
  2. {
  3.     public abstract Prototype Clone();
  4. }
  5. public class ConcretePrototype : Prototype
  6. {
  7.     public int Id { get; set; }
  8.     public override Prototype Clone()
  9.     {
  10.         return (Prototype)this.MemberwiseClone();
  11.     }
  12. }
  13. // 使用原型
  14. class Program
  15. {
  16.     static void Main(string[] args)
  17.     {
  18.         ConcretePrototype prototype = new ConcretePrototype { Id = 1 };
  19.         ConcretePrototype clone = (ConcretePrototype)prototype.Clone();
  20.         Console.WriteLine($"Original ID: {prototype.Id}");
  21.         Console.WriteLine($"Clone ID: {clone.Id}");
  22.     }
  23. }
复制代码
3. 三大工厂模式

3.1 简单工厂模式(Simple Factory Pattern)

简单工厂模式通过一个工厂类来创建对象,而不是直接在客户端代码中实例化对象。
示例代码

  1. public interface IProduct
  2. {
  3.     void Use();
  4. }
  5. public class ConcreteProductA : IProduct
  6. {
  7.     public void Use()
  8.     {
  9.         Console.WriteLine("Using Product A");
  10.     }
  11. }
  12. public class ConcreteProductB : IProduct
  13. {
  14.     public void Use()
  15.     {
  16.         Console.WriteLine("Using Product B");
  17.     }
  18. }
  19. public class SimpleFactory
  20. {
  21.     public static IProduct CreateProduct(string type)
  22.     {
  23.         switch (type)
  24.         {
  25.             case "A":
  26.                 return new ConcreteProductA();
  27.             case "B":
  28.                 return new ConcreteProductB();
  29.             default:
  30.                 throw new ArgumentException("Invalid product type");
  31.         }
  32.     }
  33. }
  34. // 使用简单工厂
  35. class Program
  36. {
  37.     static void Main(string[] args)
  38.     {
  39.         IProduct productA = SimpleFactory.CreateProduct("A");
  40.         productA.Use();
  41.         IProduct productB = SimpleFactory.CreateProduct("B");
  42.         productB.Use();
  43.     }
  44. }
复制代码
3.2 工厂方法模式(Factory Method Pattern)

工厂方法模式定义了一个创建对象的接口,但由子类决定实例化哪个类。
示例代码

  1. public interface IProduct
  2. {
  3.     void Use();
  4. }
  5. public class ConcreteProductA : IProduct
  6. {
  7.     public void Use()
  8.     {
  9.         Console.WriteLine("Using Product A");
  10.     }
  11. }
  12. public class ConcreteProductB : IProduct
  13. {
  14.     public void Use()
  15.     {
  16.         Console.WriteLine("Using Product B");
  17.     }
  18. }
  19. public abstract class Creator
  20. {
  21.     public abstract IProduct FactoryMethod();
  22. }
  23. public class ConcreteCreatorA : Creator
  24. {
  25.     public override IProduct FactoryMethod()
  26.     {
  27.         return new ConcreteProductA();
  28.     }
  29. }
  30. public class ConcreteCreatorB : Creator
  31. {
  32.     public override IProduct FactoryMethod()
  33.     {
  34.         return new ConcreteProductB();
  35.     }
  36. }
  37. // 使用工厂方法
  38. class Program
  39. {
  40.     static void Main(string[] args)
  41.     {
  42.         Creator creatorA = new ConcreteCreatorA();
  43.         IProduct productA = creatorA.FactoryMethod();
  44.         productA.Use();
  45.         Creator creatorB = new ConcreteCreatorB();
  46.         IProduct productB = creatorB.FactoryMethod();
  47.         productB.Use();
  48.     }
  49. }
复制代码
3.3 抽象工厂模式(Abstract Factory Pattern)

抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们详细的类。
示例代码

  1. public interface IProductA
  2. {
  3.     void Use();
  4. }
  5. public interface IProductB
  6. {
  7.     void Use();
  8. }
  9. public class ConcreteProductA1 : IProductA
  10. {
  11.     public void Use()
  12.     {
  13.         Console.WriteLine("Using Product A1");
  14.     }
  15. }
  16. public class ConcreteProductA2 : IProductA
  17. {
  18.     public void Use()
  19.     {
  20.         Console.WriteLine("Using Product A2");
  21.     }
  22. }
  23. public class ConcreteProductB1 : IProductB
  24. {
  25.     public void Use()
  26.     {
  27.         Console.WriteLine("Using Product B1");
  28.     }
  29. }
  30. public class ConcreteProductB2 : IProductB
  31. {
  32.     public void Use()
  33.     {
  34.         Console.WriteLine("Using Product B2");
  35.     }
  36. }
  37. public interface IAbstractFactory
  38. {
  39.     IProductA CreateProductA();
  40.     IProductB CreateProductB();
  41. }
  42. public class ConcreteFactory1 : IAbstractFactory
  43. {
  44.     public IProductA CreateProductA()
  45.     {
  46.         return new ConcreteProductA1();
  47.     }
  48.     public IProductB CreateProductB()
  49.     {
  50.         return new ConcreteProductB1();
  51.     }
  52. }
  53. public class ConcreteFactory2 : IAbstractFactory
  54. {
  55.     public IProductA CreateProductA()
  56.     {
  57.         return new ConcreteProductA2();
  58.     }
  59.     public IProductB CreateProductB()
  60.     {
  61.         return new ConcreteProductB2();
  62.     }
  63. }
  64. // 使用抽象工厂
  65. class Program
  66. {
  67.     static void Main(string[] args)
  68.     {
  69.         IAbstractFactory factory1 = new ConcreteFactory1();
  70.         IProductA productA1 = factory1.CreateProductA();
  71.         IProductB productB1 = factory1.CreateProductB();
  72.         productA1.Use();
  73.         productB1.Use();
  74.         IAbstractFactory factory2 = new ConcreteFactory2();
  75.         IProductA productA2 = factory2.CreateProductA();
  76.         IProductB productB2 = factory2.CreateProductB();
  77.         productA2.Use();
  78.         productB2.Use();
  79.     }
  80. }
复制代码
4. 建造者模式(Builder Pattern)

建造者模式将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。
示例代码

  1. public class Product
  2. {
  3.     public string PartA { get; set; }
  4.     public string PartB { get; set; }
  5.     public string PartC { get; set; }
  6.     public void Show()
  7.     {
  8.         Console.WriteLine($"PartA: {PartA}, PartB: {PartB}, PartC: {PartC}");
  9.     }
  10. }
  11. public interface IBuilder
  12. {
  13.     void BuildPartA();
  14.     void BuildPartB();
  15.     void BuildPartC();
  16.     Product GetResult();
  17. }
  18. public class ConcreteBuilder : IBuilder
  19. {
  20.     private Product _product = new Product();
  21.     public void BuildPartA()
  22.     {
  23.         _product.PartA = "Part A";
  24.     }
  25.     public void BuildPartB()
  26.     {
  27.         _product.PartB = "Part B";
  28.     }
  29.     public void BuildPartC()
  30.     {
  31.         _product.PartC = "Part C";
  32.     }
  33.     public Product GetResult()
  34.     {
  35.         return _product;
  36.     }
  37. }
  38. public class Director
  39. {
  40.     private IBuilder _builder;
  41.     public Director(IBuilder builder)
  42.     {
  43.         _builder = builder;
  44.     }
  45.     public void Construct()
  46.     {
  47.         _builder.BuildPartA();
  48.         _builder.BuildPartB();
  49.         _builder.BuildPartC();
  50.     }
  51. }
  52. // 使用建造者模式
  53. class Program
  54. {
  55.     static void Main(string[] args)
  56.     {
  57.         IBuilder builder = new ConcreteBuilder();
  58.         Director director = new Director(builder);
  59.         director.Construct();
  60.         Product product = builder.GetResult();
  61.         product.Show();
  62.     }
  63. }
复制代码
总结

创造性设计模式提供了多种灵活的对象创建方式,资助我们在不同的场景下更好地管理对象的创建过程。单例模式确保全局唯一实例,原型模式通过复制对象来创建新对象,三大工厂模式(简单工厂、工厂方法、抽象工厂)提供了不同层次的抽象来创建对象,而建造者模式则将复杂对象的构建过程分离出来。通过合理使用这些设计模式,我们可以提高代码的可维护性和可扩展性。

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

刘俊凯

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表