在软件开发领域,设计模式是一种经过时间检验的解决方案,它可以帮助开发者解决常见的问题,提高代码的可读性、可维护性和可扩展性。C作为一门成熟的编程语言,拥有丰富的设计模式库。本文将揭秘C中的五大实用设计...
在软件开发领域,设计模式是一种经过时间检验的解决方案,它可以帮助开发者解决常见的问题,提高代码的可读性、可维护性和可扩展性。C#作为一门成熟的编程语言,拥有丰富的设计模式库。本文将揭秘C#中的五大实用设计模式,并探讨它们在具体场景中的应用。
单例模式确保一个类只有一个实例,并提供一个全局访问点。这种模式在需要控制实例数量、共享资源或避免重复创建对象时非常有用。
public class DatabaseConnection
{ private static DatabaseConnection instance; private static readonly object lockObject = new object(); private DatabaseConnection() { } public static DatabaseConnection GetInstance() { if (instance == null) { lock (lockObject) { if (instance == null) { instance = new DatabaseConnection(); } } } return instance; }
}工厂模式定义一个接口用于创建对象,但让子类决定实例化哪一个类。这种模式让类的实例化过程延迟到子类中进行。
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 Factory
{ public static IProduct CreateProduct(string type) { if (type == "A") { return new ConcreteProductA(); } else if (type == "B") { return new ConcreteProductB(); } return null; }
}观察者模式定义对象间的一种一对多的依赖关系,当一个对象的状态发生变化时,所有依赖于它的对象都会得到通知并自动更新。
public interface IObserver
{ void Update();
}
public interface ISubject
{ void RegisterObserver(IObserver observer); void RemoveObserver(IObserver observer); void NotifyObservers();
}
public class ConcreteSubject : ISubject
{ private List observers = new List(); public void RegisterObserver(IObserver observer) { observers.Add(observer); } public void RemoveObserver(IObserver observer) { observers.Remove(observer); } public void NotifyObservers() { foreach (IObserver observer in observers) { observer.Update(); } }
}
public class ConcreteObserver : IObserver
{ public void Update() { Console.WriteLine("Observer got notified!"); }
} 装饰者模式动态地给一个对象添加一些额外的职责,而不改变其接口。这种模式在需要在不修改对象的情况下增加其功能时非常有用。
public interface IComponent
{ void Operation();
}
public class ConcreteComponent : IComponent
{ public void Operation() { Console.WriteLine("ConcreteComponent operation"); }
}
public abstract class Decorator : IComponent
{ protected IComponent component; public Decorator(IComponent component) { this.component = component; } public void Operation() { component.Operation(); }
}
public class ConcreteDecoratorA : Decorator
{ public ConcreteDecoratorA(IComponent component) : base(component) { } public override void Operation() { base.Operation(); Console.WriteLine("ConcreteDecoratorA added functionality"); }
}适配器模式将一个类的接口转换成客户期望的另一个接口,使得原本接口不兼容的类可以一起工作。
public interface ITarget
{ void Request();
}
public class Target : ITarget
{ public void Request() { Console.WriteLine("Target.Request"); }
}
public interface IAdaptee
{ void SpecificRequest();
}
public class Adaptee : IAdaptee
{ public void SpecificRequest() { Console.WriteLine("Adaptee.SpecificRequest"); }
}
public class Adapter : ITarget
{ private IAdaptee adaptee; public Adapter(IAdaptee adaptee) { this.adaptee = adaptee; } public void Request() { adaptee.SpecificRequest(); }
}通过以上五种设计模式,开发者可以更好地解决常见问题,提高代码质量。在实际开发中,根据具体需求选择合适的设计模式,是提升软件开发效率的关键。