设计模式

一、单例模式

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

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

五、观察者模式

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

相关推荐
小江的记录本5 小时前
【Java基础】核心关键字:final、static、volatile、synchronized、transient(附《思维导图》+《面试高频考点清单》)
java·前端·数据结构·后端·ai·面试·ai编程
tongluowan0075 小时前
Java 内存模型(JMM)- 内存屏障
java·内存模型·内存屏障
月落归舟5 小时前
并发编程之volatile深度解析(二)
java·开发语言·volatile
me8325 小时前
【AI】踩坑LangChain4j集成千问模型:版本适配问题完整解决历程
java·spring·阿里云·ai
来恩10036 小时前
Java Web三大作用域对象
java·开发语言·前端
ゆづき6 小时前
Java 初学者入门指南:常见问题 + 核心知识点 + 进阶 20 道练习题
java·开发语言·学习·算法·水题
_Evan_Yao6 小时前
限流的艺术:令牌桶与滑动窗口的博弈,以及我为何在 AI 项目中选择了后者
java·后端·架构
TheRouter6 小时前
OpenClaw 上下文瘦身:3 个实验
开发语言·python·ai
LIUAWEIO6 小时前
接口 data 满屏反斜杠,怎么展开?
java·开发语言·数据库·json在线解析·data是字符串·json转义·二次json