• 穿着马甲的编码方式链式


    我们要做实干家,不做意淫者。哈~   

    上代码:

      1 using Microsoft.VisualStudio.TestTools.UnitTesting;
      2 using System.Diagnostics;
      3 using System;
      4 
      5 namespace Demo.TestProject
      6 {
      7     /// <summary>
      8     /// 链式编码Demo
      9     /// </summary>
     10     [TestClass]
     11     public class UnitTest
     12     {
     13 
     14         [TestMethod]
     15         public void TestMethod1()
     16         {
     17             string hello = "Hello ";
     18             string world = "World";
     19 
     20             try
     21             {  
     22                 //例1: 有返回值(返回本身)的例子
     23                 new TestClasslmp { UName = "HLS" }
     24                     .StrStart()
     25                     .StrAppend(hello, world)
     26                     .StrEnd();
     27                 //例2: 无返回值的例子
     28                 new TestClasslmp2 { UName = "HLSTwo" }
     29                     .Do<TestClasslmp2>(p => p.StrStart())
     30                     .Do(p => p.StrAppend(hello, world))
     31                     .Do(p => p.StrEnd());
     32 
     33             }
     34             catch (Exception e)
     35             {
     36                 throw new Exception("Error:" + e.Message);
     37             }
     38 
     39 
     40         }
     41     }
     42 
     43     /*****例1: 有返回值(返回本身)的例子*****/
     44     public interface ITestClass
     45     {
     46         ITestClass StrStart();
     47         ITestClass StrAppend(string hello, string world);
     48         ITestClass StrEnd();
     49     }
     50 
     51     public class TestClasslmp : ITestClass
     52     {
     53         public string UName { getset; }
     54         #region ITestClass 成员
     55 
     56         public ITestClass StrStart()
     57         {
     58             Trace.WriteLine("开始输出...");
     59             return this;
     60         }
     61 
     62         public ITestClass StrAppend(string hello, string world)
     63         {
     64             Trace.WriteLine("输出: " + this.UName + " Say: " + hello + " " + world + ".");
     65             return this;
     66         }
     67 
     68         public ITestClass StrEnd()
     69         {
     70             Trace.WriteLine("完成!");
     71             return this;
     72         }
     73 
     74         #endregion
     75     }
     76 
     77 
     78     /*****例2: 无返回值(返回本身)的例子*****/
     79     public interface ITestClass2
     80     {
     81         void StrStart();
     82         void StrAppend(string hello, string world);
     83         void StrEnd();
     84     }
     85 
     86     public class TestClasslmp2 : ITestClass2
     87     {
     88         public string UName { getset; }
     89         #region ITestClass 成员
     90 
     91         public void StrStart()
     92         {
     93             Trace.WriteLine("开始输出...");
     94         }
     95 
     96         public void StrAppend(string hello, string world)
     97         {
     98             Trace.WriteLine("输出: " + this.UName + " Say: " + hello + " " + world + ".");
     99         }
    100 
    101         public void StrEnd()
    102         {
    103             Trace.WriteLine("完成!");
    104         }
    105 
    106         #endregion
    107     }
    108 
    109    /*****定义一个处理了链式void的方法*****/
    110     public static class MyClass
    111     {
    112         public static T Do<T>(this T t, Action<T> action)
    113         {
    114             action(t);
    115             return t;
    116         }
    117     }
    118 }
  • 相关阅读:
    浅析 Java 中的 final 关键字
    谷歌Java编程风格指南
    分布式事务之两阶段提交协议(2PC)and 使用事件和消息队列实现分布式事务
    零和博弈与木桶定律
    Executors类创建四种常见线程池
    软件设计的原则&101个设计模式-2011年04月25日 陈皓
    编程中的命名设计那点事-陈皓
    从面向对象的设计模式看软件设计- 2013年02月01日 陈皓
    SQL语句
    分布式事务
  • 原文地址:https://www.cnblogs.com/haoliansheng/p/1762448.html
Copyright © 2020-2023  润新知