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方法
  • 相关阅读:
    PHP里文件的查找方式及写法
    上传文件
    用ajax对数据进行查看人员信息
    ajax实现分页
    jquery做个日期选择适用于手机端
    PHP用ajia代码写三级联动下拉
    JSON数据的定义
    jquery做一些小的特效
    对jquery操作复选框
    使用 LUT 模拟光照
  • 原文地址:https://www.cnblogs.com/zhengshiqiang47/p/5712959.html
Copyright © 2011-2022 走看看