Model-engineering implementation and expansion (Design Mode C #) coherent interface fluent interface-reference answer for "self-test"

Source: Internet
Author: User

Transferred from: Model-engineering implementation and expansion (Design Mode C)

Http://www.cnblogs.com/callwangxiang/

 

 

Bytes

 

 

Reference answer

 

Design Points:

    1. Use a coherent interface to design the table creation process
    2. Because table head involves a layer of nesting, and body involves two layers of nesting, to facilitate adjustment and modification, each node element type must be retained back to the reference of the parent node.

 

1. Design Abstract node types with FLUENT features

/// <Summary> /// set type with FLUENT features after modification /// </Summary> /// <typeparam name = "T"> set element type </ typeparam> // <typeparam name = "tparent"> parent node type </typeparam> class fluentcollection <telement, tparent> where telement: class where tparent: Class {protected list <telement> List = new list <telement> (); tparent parent; Public fluentcollection (tparent parent) {If (parent = NULL) throw new argumentnullexception ("parent"); this. parent = parent;} // <summary> // return to the parent node // </Summary> Public tparent parent {get {return parent ;}} /// <summary> /// how to obtain the delegate of a telement-type instance // </Summary> Public func <telement> getinstance {Get; set ;} /// <summary> /// append operation with FLUENT features /// </Summary> /// <Param name = "T"> </param> /// <returns> </returns> Public fluentcollection <telement, tparent> Add (telement t) {list. add (t); return this ;} /// <summary> /// vacant operation with FLUENT features /// </Summary> /// <returns> </returns> Public fluentcollection <telement, tparent> skip {get {list. add (getinstance (); return this ;}} /// <summary> /// execute the foreach action of LINQ /// </Summary> /// <Param name = "action"> </param> Public void foreach (action <telement> action) {list. foreach (Action) ;}//< summary> /// elements whose parent node is table /// </Summary> class withtableobject {table; // parent node public withtableobject (Table) {If (Table = NULL) throw new argumentnullexception ("table"); this. table = TABLE ;}/// <summary> /// point to the parent node -- table /// </Summary> Public table parent {get {return table ;}}}

 

2. Define each node type

Class notation {public notation () {DATA = string. empty;} public notation (string data) {DATA = data;} Public String data {Get; private set ;}} /// <summary> // nelement // </Summary> class item: Notation {public item (): Base () {} public item (string data ): base (data) {}/// <summary> // Col element // </Summary> class column: Notation {public column (): Base () {} public column (string data): Base (data) {}}/// <summary> /// line element /// </Summary> class line {fluentcollection <item, line> items; body; public line (body) {If (Body = NULL) throw new argumentnullexception ("body"); this. body = body; items = new fluentcollection <item, line> (this) {getinstance = () =>{ return new item ();}};} /// <summary> /// parent node /// </Summary> public body {get {return body;} public fluentcollection <item, line> items {get {return items;} public line newline {get {return body. newline ;}}/// <summary> // Body element /// </Summary> class body: withtableobject {list <line> lines = new list <line> (); public body (Table table): Base (table) {} public line newline {get {var line = new line (this); lines. add (line); return line ;}} public list <line> lines {get {return lines ;}}} /// <summary> /// head element /// </Summary> class head: withtableobject {fluentcollection <column, head> Columns; Public head (Table table ): base (table) {columns = new fluentcollection <column, head> (this) {getinstance = () => {return new column () ;};} public fluentcollection <column, head> Columns {get {return columns ;}} class table {string name; body; head; Public table () {body = new body (this ); head = new head (this);} public table name (string name) {If (string. isnullorempty (name) throw new argumentnullexception ("name"); this. name = Name; return this;} public override string tostring () {return name;} public body {get {return body ;}} public head {get {return head ;}}}

 

3. Define the Data Type of the generated workbook

Class notation {public notation () {DATA = string. empty;} public notation (string data) {DATA = data;} Public String data {Get; private set ;}} /// <summary> // nelement // </Summary> class item: Notation {public item (): Base () {} public item (string data ): base (data) {}/// <summary> // Col element // </Summary> class column: Notation {public column (): Base () {} public column (string data): Base (data) {}}/// <summary> /// line element /// </Summary> class line {fluentcollection <item, line> items; body; public line (body) {If (Body = NULL) throw new argumentnullexception ("body"); this. body = body; items = new fluentcollection <item, line> (this) {getinstance = () =>{ return new item ();}};} /// <summary> /// parent node /// </Summary> public body {get {return body;} public fluentcollection <item, line> items {get {return items;} public line newline {get {return body. newline ;}}/// <summary> // Body element /// </Summary> class body: withtableobject {list <line> lines = new list <line> (); public body (Table table): Base (table) {} public line newline {get {var line = new line (this); lines. add (line); return line ;}} public list <line> lines {get {return lines ;}}} /// <summary> /// head element /// </Summary> class head: withtableobject {fluentcollection <column, head> Columns; Public head (Table table ): base (table) {columns = new fluentcollection <column, head> (this) {getinstance = () => {return new column () ;};} public fluentcollection <column, head> Columns {get {return columns ;}} class table {string name; body; head; Public table () {body = new body (this ); head = new head (this);} public table name (string name) {If (string. isnullorempty (name) throw new argumentnullexception ("name"); this. name = Name; return this;} public override string tostring () {return name;} public body {get {return body ;}} public head {get {return head ;}}}

 

 

4. Unit Test

[Testclass] public class fluentinterfacefixture {tablewriter writer; [testinitialize] public void initialize () {writer = new tablewriter ();} [testmethod] public void testfullfilltable () {writer. output (new table (). name ("full fill "). head. columns. add (new column ("first ")). add (new column ("second ")). add (new column ("thrid ")). parent. parent. body. newline. items. add (new item ("11 ")). add (new item ("12 ")). add (new item ("13 ")). parent. newline. items. add (new item ("21 ")). add (new item ("22 ")). add (new item ("23 ")). parent. body. parent);} [testmethod] public void testskipcolumntable () {writer. output (new table (). name ("Skip columns "). head. columns. add (new column ("first ")). skip. add (new column ("thrid ")). parent. parent. body. newline. items. add (new item ("11 ")). add (new item ("12 ")). add (new item ("13 ")). parent. newline. items. add (new item ("21 ")). add (new item ("22 ")). add (new item ("23 ")). parent. body. parent);} [testmethod] public void testskiitemstable () {writer. output (new table (). name ("skip items "). head. columns. add (new column ("first ")). add (new column ("second ")). add (new column ("thrid ")). parent. parent. body. newline. items. add (new item ("11 ")). skip. add (new item ("13 ")). parent. newline. items. add (new item ("21 ")). add (new item ("22 ")). skip. parent. body. parent);} [testmethod] public void testskipcolumnsanditemstable () {writer. output (new table (). name ("Skip columns and items "). head. columns. add (new column ("first ")). skip. add (new column ("thrid ")). parent. parent. body. newline. items. add (new item ("11 ")). skip. add (new item ("13 ")). parent. newline. items. add (new item ("21 ")). add (new item ("22 ")). skip. parent. body. parent );}}

 

5. Test Results

------ Test started: Assembly: concept. tests. DLL ------ <Table> <Name> full fill </Name> 

 

Test and confirm the effectiveness of the fluent Interface

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.