当前位置: 首页 > article >正文

设计模式

一、单例模式

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

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

二、工厂模式

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

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

三、策略模式

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

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

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


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

五、观察者模式

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


http://www.kler.cn/a/539408.html

相关文章:

  • mysql 主从配置
  • 【漫话机器学习系列】087.常见的神经网络最优化算法(Common Optimizers Of Neural Nets)
  • Elasticsearch 生产集群部署终极方案
  • 如何搭建DeepSeek R1的训推环境?
  • 基于Java的在线购物系统的设计与实现
  • pytest-xdist 进行多进程并发测试
  • 所以尼!什么是边缘计算?这和云计算有什么关系?
  • 移动电视盒MGV2000刷安卓及Armbian笔记
  • vue 中 props 的使用,保姆教程
  • 火语言RPA--网址/图片地址获取
  • 生成式聊天机器人 -- 基于Pytorch + Global Attention + 双向 GRU 实现的SeqToSeq模型 -- 上
  • CodeReview-checkList-Java版
  • GOland的context的使用
  • MYSQL 创建索引
  • 集成学习(二):从理论到实战(附代码)
  • CSGHub高效管理|解锁DeepSeek R1蒸馏模型 :高效推理的新选择
  • 【stm32学习】STM32F103实操primary2(FlyMCU)
  • 【图像处理】- 基本图像操作
  • Linux网络之http协议
  • Docker安装pypiserver私服
  • Jupyter Notebook 6/7 设置代码补全
  • Windows图形界面(GUI)-QT-C/C++ - QT 文本编辑控件详解
  • 旋转位置编码(RoPE)讲解和代码实现
  • < OS 有关 > Ubuntu 版本升级 实践 24.04 -> 24.10, 安装 .NET
  • Ranger 2.1.0 Admin安装
  • 处理数据及其选择关键列进行一次聚类