创造型设计模式:单例、原型、三大工厂、建造者
在软件开辟中,设计模式是办理常见问题的经典办理方案。创造性设计模式主要关注对象的创建机制,确保系统在创建对象时具有灵活性和可扩展性。本文将介绍几种常见的创造型设计模式,包括单例模式、原型模式、三大工厂模式(简单工厂、工厂方法、抽象工厂)以及建造者模式。
1. 单例模式(Singleton Pattern)
单例模式确保一个类只有一个实例,并提供一个全局访问点。这在需要控制资源访问或共享资源时非常有效。
示例代码
- public class Singleton
- {
- private static Singleton _instance;
- // 私有构造函数,防止外部实例化
- private Singleton() { }
- public static Singleton Instance
- {
- get
- {
- if (_instance == null)
- {
- _instance = new Singleton();
- }
- return _instance;
- }
- }
- public void DoSomething()
- {
- Console.WriteLine("Singleton is doing something.");
- }
- }
- // 使用单例
- class Program
- {
- static void Main(string[] args)
- {
- Singleton singleton = Singleton.Instance;
- singleton.DoSomething();
- }
- }
复制代码 2. 原型模式(Prototype Pattern)
原型模式通过复制现有对象来创建新对象,而不是通过实例化类。这在创建成本较高的对象时非常有效。
示例代码
- public abstract class Prototype
- {
- public abstract Prototype Clone();
- }
- public class ConcretePrototype : Prototype
- {
- public int Id { get; set; }
- public override Prototype Clone()
- {
- return (Prototype)this.MemberwiseClone();
- }
- }
- // 使用原型
- class Program
- {
- static void Main(string[] args)
- {
- ConcretePrototype prototype = new ConcretePrototype { Id = 1 };
- ConcretePrototype clone = (ConcretePrototype)prototype.Clone();
- Console.WriteLine($"Original ID: {prototype.Id}");
- Console.WriteLine($"Clone ID: {clone.Id}");
- }
- }
复制代码 3. 三大工厂模式
3.1 简单工厂模式(Simple Factory Pattern)
简单工厂模式通过一个工厂类来创建对象,而不是直接在客户端代码中实例化对象。
示例代码
- public interface IProduct
- {
- void Use();
- }
- public class ConcreteProductA : IProduct
- {
- public void Use()
- {
- Console.WriteLine("Using Product A");
- }
- }
- public class ConcreteProductB : IProduct
- {
- public void Use()
- {
- Console.WriteLine("Using Product B");
- }
- }
- public class SimpleFactory
- {
- public static IProduct CreateProduct(string type)
- {
- switch (type)
- {
- case "A":
- return new ConcreteProductA();
- case "B":
- return new ConcreteProductB();
- default:
- throw new ArgumentException("Invalid product type");
- }
- }
- }
- // 使用简单工厂
- class Program
- {
- static void Main(string[] args)
- {
- IProduct productA = SimpleFactory.CreateProduct("A");
- productA.Use();
- IProduct productB = SimpleFactory.CreateProduct("B");
- productB.Use();
- }
- }
复制代码 3.2 工厂方法模式(Factory Method Pattern)
工厂方法模式定义了一个创建对象的接口,但由子类决定实例化哪个类。
示例代码
- public interface IProduct
- {
- void Use();
- }
- public class ConcreteProductA : IProduct
- {
- public void Use()
- {
- Console.WriteLine("Using Product A");
- }
- }
- public class ConcreteProductB : IProduct
- {
- public void Use()
- {
- Console.WriteLine("Using Product B");
- }
- }
- public abstract class Creator
- {
- public abstract IProduct FactoryMethod();
- }
- public class ConcreteCreatorA : Creator
- {
- public override IProduct FactoryMethod()
- {
- return new ConcreteProductA();
- }
- }
- public class ConcreteCreatorB : Creator
- {
- public override IProduct FactoryMethod()
- {
- return new ConcreteProductB();
- }
- }
- // 使用工厂方法
- class Program
- {
- static void Main(string[] args)
- {
- Creator creatorA = new ConcreteCreatorA();
- IProduct productA = creatorA.FactoryMethod();
- productA.Use();
- Creator creatorB = new ConcreteCreatorB();
- IProduct productB = creatorB.FactoryMethod();
- productB.Use();
- }
- }
复制代码 3.3 抽象工厂模式(Abstract Factory Pattern)
抽象工厂模式提供一个创建一系列相关或相互依赖对象的接口,而无需指定它们详细的类。
示例代码
- public interface IProductA
- {
- void Use();
- }
- public interface IProductB
- {
- void Use();
- }
- public class ConcreteProductA1 : IProductA
- {
- public void Use()
- {
- Console.WriteLine("Using Product A1");
- }
- }
- public class ConcreteProductA2 : IProductA
- {
- public void Use()
- {
- Console.WriteLine("Using Product A2");
- }
- }
- public class ConcreteProductB1 : IProductB
- {
- public void Use()
- {
- Console.WriteLine("Using Product B1");
- }
- }
- public class ConcreteProductB2 : IProductB
- {
- public void Use()
- {
- Console.WriteLine("Using Product B2");
- }
- }
- public interface IAbstractFactory
- {
- IProductA CreateProductA();
- IProductB CreateProductB();
- }
- public class ConcreteFactory1 : IAbstractFactory
- {
- public IProductA CreateProductA()
- {
- return new ConcreteProductA1();
- }
- public IProductB CreateProductB()
- {
- return new ConcreteProductB1();
- }
- }
- public class ConcreteFactory2 : IAbstractFactory
- {
- public IProductA CreateProductA()
- {
- return new ConcreteProductA2();
- }
- public IProductB CreateProductB()
- {
- return new ConcreteProductB2();
- }
- }
- // 使用抽象工厂
- class Program
- {
- static void Main(string[] args)
- {
- IAbstractFactory factory1 = new ConcreteFactory1();
- IProductA productA1 = factory1.CreateProductA();
- IProductB productB1 = factory1.CreateProductB();
- productA1.Use();
- productB1.Use();
- IAbstractFactory factory2 = new ConcreteFactory2();
- IProductA productA2 = factory2.CreateProductA();
- IProductB productB2 = factory2.CreateProductB();
- productA2.Use();
- productB2.Use();
- }
- }
复制代码 4. 建造者模式(Builder Pattern)
建造者模式将一个复杂对象的构建与其表示分离,使得同样的构建过程可以创建不同的表示。
示例代码
- public class Product
- {
- public string PartA { get; set; }
- public string PartB { get; set; }
- public string PartC { get; set; }
- public void Show()
- {
- Console.WriteLine($"PartA: {PartA}, PartB: {PartB}, PartC: {PartC}");
- }
- }
- public interface IBuilder
- {
- void BuildPartA();
- void BuildPartB();
- void BuildPartC();
- Product GetResult();
- }
- public class ConcreteBuilder : IBuilder
- {
- private Product _product = new Product();
- public void BuildPartA()
- {
- _product.PartA = "Part A";
- }
- public void BuildPartB()
- {
- _product.PartB = "Part B";
- }
- public void BuildPartC()
- {
- _product.PartC = "Part C";
- }
- public Product GetResult()
- {
- return _product;
- }
- }
- public class Director
- {
- private IBuilder _builder;
- public Director(IBuilder builder)
- {
- _builder = builder;
- }
- public void Construct()
- {
- _builder.BuildPartA();
- _builder.BuildPartB();
- _builder.BuildPartC();
- }
- }
- // 使用建造者模式
- class Program
- {
- static void Main(string[] args)
- {
- IBuilder builder = new ConcreteBuilder();
- Director director = new Director(builder);
- director.Construct();
- Product product = builder.GetResult();
- product.Show();
- }
- }
复制代码 总结
创造性设计模式提供了多种灵活的对象创建方式,资助我们在不同的场景下更好地管理对象的创建过程。单例模式确保全局唯一实例,原型模式通过复制对象来创建新对象,三大工厂模式(简单工厂、工厂方法、抽象工厂)提供了不同层次的抽象来创建对象,而建造者模式则将复杂对象的构建过程分离出来。通过合理使用这些设计模式,我们可以提高代码的可维护性和可扩展性。
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |