设计模式

一、单例模式

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

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);
    }
}

五、观察者模式

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

相关推荐
yixun_gdas15 分钟前
政企网站错敏巡查:错敏都藏在哪些位置?
开发语言·前端·javascript·内容运营
小羊没烦恼!18 分钟前
.NET 11 Runtime Async 详解
java·运维·开发语言·javascript·python
Дерек的学习记录19 分钟前
Unreal Engine 5:开关门实现和流程控制
开发语言·学习·ue5
哇嘎呀21 分钟前
RHCSA题目
java·服务器·前端
SL_staff34 分钟前
企业知识平台细粒度权限设计实践:从成员组、文档库到操作级的三层防线
java·开源·全栈
周厉41 分钟前
ERP系统全景解析:核心模块与业务逻辑框架
java·服务器·前端
开开心心就好1 小时前
课堂随机点名工具,绿色版免安装双击即用
java·服务器·前端·ffmpeg·音视频·perl·symfony
code2cat1 小时前
Java高级篇之Socket:从建立连接到完整收发一条消息
java·开发语言
凯瑟琳.奥古斯特2 小时前
OCR文字检测算法对比解析
开发语言·python·职场和发展
PD我是你的真爱粉2 小时前
成熟Agent系统设计深度研究报告
java·大数据·人工智能