设计模式

一、单例模式

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

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

五、观察者模式

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

相关推荐
错把套路当深情10 分钟前
Java 全方向开发技术栈指南
java·开发语言
前端郭德纲10 分钟前
JavaScript Object.freeze() 详解
开发语言·javascript·ecmascript
han_hanker19 分钟前
springboot 一个请求的顺序解释
java·spring boot·后端
MaCa .BaKa20 分钟前
44-校园二手交易系统(小程序)
java·spring boot·mysql·小程序·maven·intellij-idea·mybatis
希望永不加班37 分钟前
SpringBoot 静态资源访问(图片/JS/CSS)配置详解
java·javascript·css·spring boot·后端
ada0_ada11 小时前
qt模块学习记录
开发语言·qt·学习
oh LAN1 小时前
RuoYi-Vue-master:Spring Boot 4.x (JDK 17+) (环境搭建)
java·vue.js·spring boot
liulilittle1 小时前
C++ 无锁编程:单停多发送场景高性能方案
服务器·开发语言·c++·高性能·无锁·原子
ch.ju1 小时前
Java程序设计(第3版)第二章——java的数据类型:小数
java
飞Link1 小时前
大模型时代的“语言编程”:Prompt Engineering (提示词工程) 深度解析与实战指南
开发语言·python·prompt