首页 > 其他分享 >.Net 几种常用设计模式【工厂、单例】

.Net 几种常用设计模式【工厂、单例】

时间:2023-12-28 12:00:22浏览次数:32  
标签:Operation class Client 单例 new Net 设计模式 public IProduct

抽象工厂模式
// 抽象产品 public interface IProduct { void Operation(); } // 具体产品A public class ProductA : IProduct { public void Operation() { Console.WriteLine("Product A Operation"); } } // 具体产品B public class ProductB : IProduct { public void Operation() { Console.WriteLine("Product B Operation"); } } // 抽象工厂 public interface IFactory { IProduct CreateProduct(); } // 具体工厂A public class FactoryA : IFactory { public IProduct CreateProduct() { return new ProductA(); } } // 具体工厂B public class FactoryB : IFactory { public IProduct CreateProduct() { return new ProductB(); } } // 客户端 public class Client { private IFactory factory; public Client(IFactory factory) { this.factory = factory; } public void Run() { IProduct product = factory.CreateProduct(); product.Operation(); } } // 使用示例 IFactory factoryA = new FactoryA(); Client clientA = new Client(factoryA); clientA.Run(); IFactory factoryB = new FactoryB(); Client clientB = new Client(factoryB); clientB.Run();
简单工厂模式
// 抽象产品
public interface IProduct
{
    void Operation();
}

// 具体产品A
public class ProductA : IProduct
{
    public void Operation()
    {
        Console.WriteLine("Product A Operation");
    }
}

// 具体产品B
public class ProductB : IProduct
{
    public void Operation()
    {
        Console.WriteLine("Product B Operation");
    }
}

// 工厂
public class SimpleFactory
{
    public IProduct CreateProduct(string type)
    {
        switch (type)
        {
            case "A":
                return new ProductA();
            case "B":
                return new ProductB();
            default:
                throw new ArgumentException("Invalid product type");
        }
    }
}

// 客户端
public class Client
{
    private SimpleFactory factory;

    public Client(SimpleFactory factory)
    {
        this.factory = factory;
    }

    public void Run()
    {
        IProduct product = factory.CreateProduct("A");
        product.Operation();
    }
}

// 使用示例
SimpleFactory simpleFactory = new SimpleFactory();
Client client = new Client(simpleFactory);
client.Run();
单例模式
public sealed class Singleton
{
    private static readonly Singleton instance = new Singleton();

    private Singleton()
    {
    }

    public static Singleton Instance
    {
        get
        {
            return instance;
        }
    }

    public void Operation()
    {
        Console.WriteLine("Singleton Operation");
    }
}

// 客户端
public class Client
{
    public void Run()
    {
        Singleton instance = Singleton.Instance;
        instance.Operation();
    }
}

// 使用示例
Client client = new Client();
client.Run();

转载自:https://blog.51cto.com/u_16175525/6778901

标签:Operation,class,Client,单例,new,Net,设计模式,public,IProduct
From: https://www.cnblogs.com/SmallChen/p/17932409.html

相关文章

  • .net 8中 System.Data.SqlClient打开数据库失败
    问题背景:项目升级到.Net8后,在使用System.Data.SqlClient连接SqlServer提示如下异常System.InvalidOperationException:“Internalconnectionfatalerror.” 开始解决问题:(1)排除了连接字符串以及代码编写的问题(2)System.Data.SqlClient升到最新版4.8.5,依旧报错 (3......
  • WPF 使用Log4Net记录日志和显示日志
    一、添加引用 二、添加Log4Net配置文件,设置文件属性如果较新则复制或者始终复制 <?xmlversion="1.0"encoding="utf-8"?><log4net><!--将日志以回滚文件的形式写到文件中--><!--按日期切分日志文件,并将日期作为日志文件的名字--><appendername="Lo......
  • 享元设计模式 和 享元设计模式在 FastDateFormat类中的应用
    1.概述享元设计模式(FlyweightPattern):通过尽量共享实例来避免new出实例。享元设计模式中有两个角色,一是要共享的实例,二是获取或创建这些共享实例的工厂。举一个例子:例如String常量池,大家创建的String常量,创建String的时候,先去常量池中看一下,有该String常量直接使用该常量,如果没......
  • Kubernetes之APIService资源
    一、前言在说自定义APIServer前,我们先来了解下Kubernetes原生的APIServer。    其实APIServer就是一个https服务器,我们可以使用kubectl工具通过https协议请求APIServer创建资源,删除资源,查看资源等等操作;每个请求都对应着RESTfulAPI中的请求方法,对应资源就是http协议中的url......
  • GOF23--23种设计模式(三)
    一.桥接模式Java中的桥接模式(BridgePattern)是一种结构性设计模式,它将抽象部分和实现部分分离,使它们可以独立变化,同时通过桥接对象将它们连接起来。这种模式将抽象与其实现解耦,使得抽象和实现可以独立变化。抽象和它的实现通过一个桥接类进行连接,使得它们可以各自独立地变化。......
  • 初中英语优秀范文100篇-040My View on the Internet-网络之我见
    初中英语优秀范文100篇-040MyViewontheInternet-网络之我见PDF格式公众号回复关键字:SHCZFW040记忆树1NowmanyofmyclassmatesliketosurftheInternetintheirfreetime.翻译现在很多同学喜欢在空闲时间上网简化记忆上网句子结构1manyofmyclassmate......
  • 单例模式
    5单例模式单例模式是保证实例唯一性的重要手段。单例模式首先通过将类的实例化方法私有化来防止程序通过其他方式创建该类的实例,然后通过提供一个全局唯一获取该类实例的方法帮助用户获取类的实例,用户只需也只能通过调用该方法获取类的实例。单例模式的常见写法有懒汉模式(线程......
  • Composite 组合模式简介与 C# 示例【结构型3】【设计模式来了_8】
    Composite组合模式简介与C#示例【结构型3】【设计模式来了_8】 阅读目录〇、简介1、什么是组合设计模式?2、优缺点和适用场景一、简单的代码示例二、根据示例代码看结构三、相关模式回到顶部〇、简介1、什么是组合设计模式?一句话解释:  针对树形结构......
  • Facade 外观模式简介与 C# 示例【结构型5】【设计模式来了_10】
    Facade外观模式简介与C#示例【结构型5】【设计模式来了_10】 阅读目录〇、简介1、什么是外观模式?2、外观模式的优缺点和适用场景一、外观模式的代码实现二、结构三、相关模式回到顶部〇、简介1、什么是外观模式?一句话解释:  将一系列需要一起进行的......
  • Builder 生成器模式简介与 C# 示例【创建型2】【设计模式来了_2】
    Builder生成器模式简介与C#示例【创建型2】【设计模式来了_2】 阅读目录〇、简介1、什么是生成器模式?2、优缺点和使用场景一、简单的示例代码二、生成器模式结构三、在.Net框架中的实际应用四、相关模式回到顶部〇、简介1、什么是生成器模式?一句话......