zoukankan      html  css  js  c++  java
  • Fluent Interface(流式接口)

    我最初接触这个概念是读自<<模式-工程化实现及扩展>>,另外有Martin fowler大师 所写http://martinfowler.com/bliki/FluentInterface.html

    Fluent Interface实例

    Java 类Country

    1. package com.jue.fluentinterface;  
    2.   
    3.   
    4. public class Country {  
    5.     private String name;  
    6.     private int code;  
    7.     private boolean isDevelopedCountry;  
    8.     private int area;  
    9.   
    10.   
    11.     Country addName(String name) {  
    12.         this.name = name;  
    13.         return this;  
    14.     }  
    15.   
    16.   
    17.     Country addCountyCode(int code) {  
    18.         this.code = code;  
    19.         return this;  
    20.     }  
    21.   
    22.   
    23.     Country setDeveloped(boolean isdeveloped) {  
    24.         this.isDevelopedCountry = isdeveloped;  
    25.         return this;  
    26.     }  
    27.   
    28.   
    29.     Country setAread(int area) {  
    30.         this.area = area;  
    31.         return this;  
    32.     }  
    33. }  


    调用类

    1. /** 
    2.  * @param args 
    3.  */  
    4. public static void main(String[] args) {  
    5.     // TODO Auto-generated method stub  
    6.   
    7.     Country china = new Country();  
    8.     china.addName("The People's Republic of China")  
    9.             .addCountyCode(1001)  
    10.             .setDeveloped(false)  
    11.             .setAread(960);  
    12. }  

    主要特征:

    Country 的方法返回本身country,使调用者有了继续调用country方法的能力.

    优势

    1.有时候我们需要根据传入的参数数目不同定义不同的构造器。使用 FluentInterface就可以随意传递想要的数据,并保持他们的连贯。


    java中的应用 

    StringBuffer append方法
  • 相关阅读:
    852. Peak Index in a Mountain Array
    841. Keys and Rooms
    832. Flipping an Image
    821. Shortest Distance to a Character
    824. Goat Latin
    如何生成git的公钥和私钥
    学习笔记
    加快JavaScript加载和执行效率
    PO BO VO DTO POJO DAO概念及其作用
    jvm 垃圾回收区的形象说明
  • 原文地址:https://www.cnblogs.com/zhengshiqiang47/p/5712959.html
Copyright © 2011-2022 走看看