C# 语言 设计模式的简洁实现

C#阿木 发布于 2025-06-13 8 次阅读


C 设计模式简洁实现指南

设计模式是软件工程中解决常见问题的通用解决方案。在C编程语言中,设计模式可以帮助开发者编写更加模块化、可重用和易于维护的代码。本文将围绕C语言,对几种常见的设计模式进行简洁实现,旨在帮助开发者快速理解和应用这些模式。

单例模式(Singleton)

单例模式确保一个类只有一个实例,并提供一个全局访问点。

csharp
public class Singleton
{
private static Singleton instance;
private static readonly object lockObject = new object();

private Singleton() { }

public static Singleton Instance
{
get
{
if (instance == null)
{
lock (lockObject)
{
if (instance == null)
{
instance = new Singleton();
}
}
}
return instance;
}
}
}

工厂模式(Factory Method)

工厂模式定义一个用于创建对象的接口,让子类决定实例化哪一个类。

csharp
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 Creator
{
public IProduct FactoryMethod()
{
// 根据需要返回具体产品
return new ConcreteProductA();
}
}

抽象工厂模式(Abstract Factory)

抽象工厂模式提供一个接口,用于创建相关或依赖对象的家族,而不需要明确指定具体类。

csharp
public interface IProductA
{
void Use();
}

public interface IProductB
{
void Use();
}

public class ConcreteProductA : IProductA
{
public void Use()
{
Console.WriteLine("Using Product A");
}
}

public class ConcreteProductB : IProductB
{
public void Use()
{
Console.WriteLine("Using Product B");
}
}

public interface IFactory
{
IProductA CreateProductA();
IProductB CreateProductB();
}

public class ConcreteFactory : IFactory
{
public IProductA CreateProductA()
{
return new ConcreteProductA();
}

public IProductB CreateProductB()
{
return new ConcreteProductB();
}
}

命令模式(Command)

命令模式将请求封装为一个对象,从而允许用户使用不同的请求、队列或日志请求,以及支持可撤销的操作。

csharp
public interface ICommand
{
void Execute();
}

public class ConcreteCommand : ICommand
{
private Receiver _receiver;

public ConcreteCommand(Receiver receiver)
{
_receiver = receiver;
}

public void Execute()
{
_receiver.DoSomething();
}
}

public class Receiver
{
public void DoSomething()
{
Console.WriteLine("Receiver does something.");
}
}

public class Invoker
{
private ICommand _command;

public void SetCommand(ICommand command)
{
_command = command;
}

public void ExecuteCommand()
{
_command.Execute();
}
}

装饰者模式(Decorator)

装饰者模式动态地给一个对象添加一些额外的职责,而不改变其接口。

csharp
public interface IComponent
{
void Operation();
}

public class ConcreteComponent : IComponent
{
public void Operation()
{
Console.WriteLine("ConcreteComponent Operation");
}
}

public class Decorator : IComponent
{
protected IComponent _component;

public Decorator(IComponent component)
{
_component = component;
}

public void Operation()
{
_component.Operation();
}
}

public class ConcreteDecoratorA : Decorator
{
public ConcreteDecoratorA(IComponent component)
: base(component)
{
}

public override void Operation()
{
base.Operation();
AddBehavior();
}

private void AddBehavior()
{
Console.WriteLine("ConcreteDecoratorA Added Behavior");
}
}

总结

本文通过简洁的代码示例,展示了C语言中几种常见设计模式的实现。这些模式可以帮助开发者编写更加高效、可维护和可扩展的代码。在实际项目中,开发者可以根据具体需求选择合适的设计模式,以提高代码质量。