设计模式

一、单例模式

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

cs 复制代码
public class Singleton
{
    private static Singleton _singleton;
    private static readonly object _lock = new object();

    private Singleton() { }

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

二、工厂模式

定义一个接口用于创建对象,但让子类决定实例化哪一个类

cs 复制代码
public interface IProduct
{
    void Use();
}

public class ProductA : IProduct
{
    public void Use()
    {
        Console.WriteLine("使用了ProductA");
    }
}

public class ProductB : IProduct
{
    public void Use()
    {
        Console.WriteLine("使用了ProductB");
    }
}

public class ProductFactory
{
    public IProduct UserWhichProduct(string ProductType)
    {
        if(ProductType == "A")
        {
            return new ProductA();
        }else if(ProductType == "B")
        {
            return new ProductB();
        }
        throw new ArgumentException("无可用Product");
    }
}

三、策略模式

定义一系列算法,把它们封装起来,并且使它们可相互替换。比如支付方式

cs 复制代码
public interface IPay
{
    void Pay(decimal money);
}

public class WeChatPay : IPay
{
    public void Pay(decimal money)
    {
        Console.WriteLine($"微信支付{money}");
    }
}

public class AliPay : IPay
{
    public void Pay(decimal money)
    {
        Console.WriteLine($"支付宝支付{money}");
    }
}

public class PayType
{
    private readonly IPay _pay;
    public PayType(IPay pay)
    {
        _pay = pay;
    }

    public void ExecutePay(decimal money)
    {
        _pay.Pay(money);
    }
}


//用法
PayType pay = new PayType(new WeChatPay());
pay.ExecutePay(20);

四、仓储模式Repository

用于抽象访问数据层的逻辑,将数据访问逻辑与业务逻辑分离

cs 复制代码
public interface IRepository<T>
{
    IEnumerable<T> GetAll();
    T GetById(int id);
    void Add(T t);
    void Update(T t);
    void Delete(int id);
}
 
public class CustomerRepository : IRepository<Customer>
{
    private readonly AppDbContext _context;
 
    public CustomerRepository(AppDbContext context)
    {
        _context = context;
    }
 
    public IEnumerable<Customer> GetAll() => _context.Customers.ToList();
    public Customer GetById(int id) => _context.Customers.Find(id);
    public void Add(Customer customer) => _context.Customers.Add(customer);
    public void Update(Customer customer) => _context.Customers.Update(customer);
    public void Delete(int id)
    {
        var customer = _context.Customers.Find(id);
        if (customer != null) _context.Customers.Remove(customer);
    }
}

五、观察者模式

定义对象间的一对多依赖关系,使得当一个对象状态发生改变时,其依赖者都会收到通知并自动更新。

相关推荐
九章云极AladdinEdu5 分钟前
深度学习优化器进化史:从SGD到AdamW的原理与选择
linux·服务器·开发语言·网络·人工智能·深度学习·gpu算力
axban7 分钟前
QT M/V架构开发实战:QStandardItemModel介绍
开发语言·数据库·qt
Yeats_Liao10 分钟前
Spring缓存(二):解决缓存雪崩、击穿、穿透问题
java·spring·缓存
猿究院-赵晨鹤22 分钟前
String、StringBuffer 和 StringBuilder 的区别
java·开发语言
I'm a winner27 分钟前
第五章:Python 数据结构:列表、元组与字典(一)
开发语言·数据结构·python
葵野寺31 分钟前
【RelayMQ】基于 Java 实现轻量级消息队列(九)
java·开发语言·rabbitmq·java-rabbitmq
代码不停1 小时前
MySQL联合查询
java·数据库·mysql
nightunderblackcat1 小时前
新手向:C语言、Java、Python 的选择与未来指南
java·c语言·python
纯真时光1 小时前
Maven高级
java
大白同学4211 小时前
【C++】C++11介绍(Ⅱ)
开发语言·c++