首页 > 其他分享 >流式接口(fluent interface)

流式接口(fluent interface)

时间:2022-10-01 10:12:38浏览次数:76  
标签:Customer set string FirstName 流式 fluent context interface public

流式接口(fluent interface)是软件工程面向对象API的一种实现方式,以提供更为可读的源代码。最早由Eric EvansMartin Fowler于2005年提出。

通常采取方法瀑布调用 (具体说是方法链式调用)来转发一系列对象方法调用的上下文 [1]。这个上下文(context)通常是指:

  • 通过被调方法的返回值定义
  • 自引用,新的上下文等于老的上下文。
  •  只有一条规则:该方法必须返回非void值。

流式接口可用于一系列方法,他们运行在同一对象上。linq大量使用了流式接口

 

// Defines the data context
class Context
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public string Sex { get; set; }
    public string Address { get; set; }
}

class Customer
{
    private Context _context = new Context(); // Initializes the context

    // set the value for properties
    public Customer FirstName(string firstName)
    {
        _context.FirstName = firstName;
        return this;
    }

    public Customer LastName(string lastName)
    {
        _context.LastName = lastName;
        return this;
    }

    public Customer Sex(string sex)
    {
        _context.Sex = sex;
        return this;
    }

    public Customer Address(string address)
    {
        _context.Address = address;
        return this;
    }

    // Prints the data to console
    public void Print()
    {
        Console.WriteLine("First name: {0} \nLast name: {1} \nSex: {2} \nAddress: {3}", _context.FirstName, _context.LastName, _context.Sex, _context.Address);
    }
}

class Program
{
    static void Main(string[] args)
    {
        // Object creation
        Customer c1 = new Customer();
        // Using the method chaining to assign & print data with a single line
        c1.FirstName("vinod").LastName("srivastav").Sex("male").Address("bangalore").Print();
    }
}

 

标签:Customer,set,string,FirstName,流式,fluent,context,interface,public
From: https://www.cnblogs.com/cdaniu/p/16746837.html

相关文章

  • TDengine 3.0 流式计算引擎语法规则介绍
    小T导读:TDengine3.0引入了全新的流式计算引擎,既支持时间驱动的流式计算,也支持事件驱动的流式计算。本文将对新的流式计算引擎的语法规则进行详细介绍,方便开发者及企业使......
  • TDengine3.0 流式计算引擎语法规则介绍
    小T导读:TDengine 3.0引入了全新的流式计算引擎,既支持时间驱动的流式计算,也支持事件驱动的流式计算。本文将对新的流式计算引擎的语法规则进行详细介绍,方便开发者及企业......
  • 学习收-在 .NET 中使用 FluentValidation 进行参数验证
    安装FluentValidation新建了一个很简单的.NETCore的WebAPI程序,只有一个接口是用户注册,入参是一个User类,然后在Nuget中安装 FluentValidation。创建第一个验证对......
  • 代码优先Fluent API 配置
    EFCore除了用数据注释DataAnnotation对实体类进行配置之外,还提供了FluentAPI的方式对实体类进行配置。FluentAPI优势:1.能够更好的进行职责分离。实体类只负责......
  • golang map 和 interface 的一些记录
    golang的map读取是不需要判断key是否存在的,不存在的key会返回默认值。如果map的value是interface,那么interface是需要先进行类型转换的,非要求类型的转换,得到结果是nil。......
  • 流式配置中转换类型的固定块和业务块
    固定块:按BOS界面业务块:金蝶流式布局  ......
  • 让SpringBoot也用上Fluent Validator
    前言在使用SpringBoot的时候经常需要对客户端传入的参数进行合法性的校验,校验的方法基本上都是使用SpringBoot提供的注解,有时候遇上注解不能满足需求的时候还需要在业务......
  • on the IPv6 loopback interface: 'Cannot assign requested address'
    .netcore+docker  需要修改两个地方Dockerfile添加 ENVASPNETCORE_URLS=http://+:44303  FROMmcr.microsoft.com/dotnet/aspnet:6.0ASbaseENVASPNETCORE......
  • Java8 提供的流式操作
    目录参考资料流式操作1.java.util.stream.Stream接口1.1Stream提供的方法2.Collection的流式操作参考资料https://docs.oracle.com/javase/8/docs/api/流式操作......
  • 使用 NVIDIA CloudXR 从 Google Cloud 流式传输 VR 和 AR 内容
    过去,与VR交互需要专用的高端工作站,以及(取决于头显)、壁挂式传感器和专用物理空间。VR中的复杂任务可能会突破传感器范围、电缆长度和空间边界的限制,使艺术家陷入困境并限......