做一个公司网站一般需要多少钱,如何开公司注册需要多少钱,住房和城乡建设网站方案,汽车工厂网站建设前言
在工作了一年多之后#xff0c;我发现静态方法的耦合问题实在是头疼。如果可以尽量不要使用静态方法存储数据#xff0c;如果要存储全局数据就把数据放在最顶层的主函数里面。
静态方法问题
耦合问题#xff0c;不要用静态方法存储数据
我这里有两个静态方法#…前言
在工作了一年多之后我发现静态方法的耦合问题实在是头疼。如果可以尽量不要使用静态方法存储数据如果要存储全局数据就把数据放在最顶层的主函数里面。
静态方法问题
耦合问题不要用静态方法存储数据
我这里有两个静态方法A和B。A和B都各种存储A_Data和B_Data。如果两个静态方法一直互相调用就会导致耦合过高无法控制数据流向的问题。 #mermaid-svg-oxIK6VskzTexiwlh {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-oxIK6VskzTexiwlh .error-icon{fill:#552222;}#mermaid-svg-oxIK6VskzTexiwlh .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-oxIK6VskzTexiwlh .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-oxIK6VskzTexiwlh .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-oxIK6VskzTexiwlh .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-oxIK6VskzTexiwlh .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-oxIK6VskzTexiwlh .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-oxIK6VskzTexiwlh .marker{fill:#333333;stroke:#333333;}#mermaid-svg-oxIK6VskzTexiwlh .marker.cross{stroke:#333333;}#mermaid-svg-oxIK6VskzTexiwlh svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-oxIK6VskzTexiwlh .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-oxIK6VskzTexiwlh .cluster-label text{fill:#333;}#mermaid-svg-oxIK6VskzTexiwlh .cluster-label span{color:#333;}#mermaid-svg-oxIK6VskzTexiwlh .label text,#mermaid-svg-oxIK6VskzTexiwlh span{fill:#333;color:#333;}#mermaid-svg-oxIK6VskzTexiwlh .node rect,#mermaid-svg-oxIK6VskzTexiwlh .node circle,#mermaid-svg-oxIK6VskzTexiwlh .node ellipse,#mermaid-svg-oxIK6VskzTexiwlh .node polygon,#mermaid-svg-oxIK6VskzTexiwlh .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-oxIK6VskzTexiwlh .node .label{text-align:center;}#mermaid-svg-oxIK6VskzTexiwlh .node.clickable{cursor:pointer;}#mermaid-svg-oxIK6VskzTexiwlh .arrowheadPath{fill:#333333;}#mermaid-svg-oxIK6VskzTexiwlh .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-oxIK6VskzTexiwlh .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-oxIK6VskzTexiwlh .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-oxIK6VskzTexiwlh .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-oxIK6VskzTexiwlh .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-oxIK6VskzTexiwlh .cluster text{fill:#333;}#mermaid-svg-oxIK6VskzTexiwlh .cluster span{color:#333;}#mermaid-svg-oxIK6VskzTexiwlh div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-oxIK6VskzTexiwlh :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} A Method_A_1 Data_B Method_A_2 Method_A_3 Data_A B Method_B_1 Method_B_2 Method_B_3 可以看到这个已经乱成一锅粥了
Mermaid源码
graph LRA-- Method_A_1--Data_B
A-- Method_A_2--Data_B
A-- Method_A_3--Data_A
A---Data_AB-- Method_B_1--Data_A
B-- Method_B_2--Data_A
B-- Method_B_3--Data_B
B---Data_B所以静态方法尽量只存放方法不要存放变量。比如字符串切割数组拼接敏感字去除。
扩展方法
这里推荐使用扩展方法来解决扩展方法很好的限制了输入和输出不会出现耦合数据的情况 微软扩展方法文档
如何改变面向过程的思维
喜欢用静态方法其实还是面向过程的思维就是在写方法的时候就已经知道我可以直接去拿Static_A的静态参数。静态方法最容易出现的就是配置文件的读取。
面向过程直接去静态函数里面拿参数 #mermaid-svg-HgYoTj9dmhmzcMIG {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-HgYoTj9dmhmzcMIG .error-icon{fill:#552222;}#mermaid-svg-HgYoTj9dmhmzcMIG .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-HgYoTj9dmhmzcMIG .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-HgYoTj9dmhmzcMIG .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-HgYoTj9dmhmzcMIG .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-HgYoTj9dmhmzcMIG .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-HgYoTj9dmhmzcMIG .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-HgYoTj9dmhmzcMIG .marker{fill:#333333;stroke:#333333;}#mermaid-svg-HgYoTj9dmhmzcMIG .marker.cross{stroke:#333333;}#mermaid-svg-HgYoTj9dmhmzcMIG svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-HgYoTj9dmhmzcMIG .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-HgYoTj9dmhmzcMIG .cluster-label text{fill:#333;}#mermaid-svg-HgYoTj9dmhmzcMIG .cluster-label span{color:#333;}#mermaid-svg-HgYoTj9dmhmzcMIG .label text,#mermaid-svg-HgYoTj9dmhmzcMIG span{fill:#333;color:#333;}#mermaid-svg-HgYoTj9dmhmzcMIG .node rect,#mermaid-svg-HgYoTj9dmhmzcMIG .node circle,#mermaid-svg-HgYoTj9dmhmzcMIG .node ellipse,#mermaid-svg-HgYoTj9dmhmzcMIG .node polygon,#mermaid-svg-HgYoTj9dmhmzcMIG .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-HgYoTj9dmhmzcMIG .node .label{text-align:center;}#mermaid-svg-HgYoTj9dmhmzcMIG .node.clickable{cursor:pointer;}#mermaid-svg-HgYoTj9dmhmzcMIG .arrowheadPath{fill:#333333;}#mermaid-svg-HgYoTj9dmhmzcMIG .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-HgYoTj9dmhmzcMIG .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-HgYoTj9dmhmzcMIG .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-HgYoTj9dmhmzcMIG .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-HgYoTj9dmhmzcMIG .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-HgYoTj9dmhmzcMIG .cluster text{fill:#333;}#mermaid-svg-HgYoTj9dmhmzcMIG .cluster span{color:#333;}#mermaid-svg-HgYoTj9dmhmzcMIG div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-HgYoTj9dmhmzcMIG :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 直接调用 直接调用 直接调用 Static_A Data A Method_A B Method_B C Method_C 这样是面向过程的思想,ABC可以直接去拿Static_A的参数 如果参数A被污染就会导致参数不可控,非常的危险 面向对象在构造函数里面将参数注入 #mermaid-svg-ZvejxbCEho065Re8 {font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;fill:#333;}#mermaid-svg-ZvejxbCEho065Re8 .error-icon{fill:#552222;}#mermaid-svg-ZvejxbCEho065Re8 .error-text{fill:#552222;stroke:#552222;}#mermaid-svg-ZvejxbCEho065Re8 .edge-thickness-normal{stroke-width:2px;}#mermaid-svg-ZvejxbCEho065Re8 .edge-thickness-thick{stroke-width:3.5px;}#mermaid-svg-ZvejxbCEho065Re8 .edge-pattern-solid{stroke-dasharray:0;}#mermaid-svg-ZvejxbCEho065Re8 .edge-pattern-dashed{stroke-dasharray:3;}#mermaid-svg-ZvejxbCEho065Re8 .edge-pattern-dotted{stroke-dasharray:2;}#mermaid-svg-ZvejxbCEho065Re8 .marker{fill:#333333;stroke:#333333;}#mermaid-svg-ZvejxbCEho065Re8 .marker.cross{stroke:#333333;}#mermaid-svg-ZvejxbCEho065Re8 svg{font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:16px;}#mermaid-svg-ZvejxbCEho065Re8 .label{font-family:"trebuchet ms",verdana,arial,sans-serif;color:#333;}#mermaid-svg-ZvejxbCEho065Re8 .cluster-label text{fill:#333;}#mermaid-svg-ZvejxbCEho065Re8 .cluster-label span{color:#333;}#mermaid-svg-ZvejxbCEho065Re8 .label text,#mermaid-svg-ZvejxbCEho065Re8 span{fill:#333;color:#333;}#mermaid-svg-ZvejxbCEho065Re8 .node rect,#mermaid-svg-ZvejxbCEho065Re8 .node circle,#mermaid-svg-ZvejxbCEho065Re8 .node ellipse,#mermaid-svg-ZvejxbCEho065Re8 .node polygon,#mermaid-svg-ZvejxbCEho065Re8 .node path{fill:#ECECFF;stroke:#9370DB;stroke-width:1px;}#mermaid-svg-ZvejxbCEho065Re8 .node .label{text-align:center;}#mermaid-svg-ZvejxbCEho065Re8 .node.clickable{cursor:pointer;}#mermaid-svg-ZvejxbCEho065Re8 .arrowheadPath{fill:#333333;}#mermaid-svg-ZvejxbCEho065Re8 .edgePath .path{stroke:#333333;stroke-width:2.0px;}#mermaid-svg-ZvejxbCEho065Re8 .flowchart-link{stroke:#333333;fill:none;}#mermaid-svg-ZvejxbCEho065Re8 .edgeLabel{background-color:#e8e8e8;text-align:center;}#mermaid-svg-ZvejxbCEho065Re8 .edgeLabel rect{opacity:0.5;background-color:#e8e8e8;fill:#e8e8e8;}#mermaid-svg-ZvejxbCEho065Re8 .cluster rect{fill:#ffffde;stroke:#aaaa33;stroke-width:1px;}#mermaid-svg-ZvejxbCEho065Re8 .cluster text{fill:#333;}#mermaid-svg-ZvejxbCEho065Re8 .cluster span{color:#333;}#mermaid-svg-ZvejxbCEho065Re8 div.mermaidTooltip{position:absolute;text-align:center;max-width:200px;padding:2px;font-family:"trebuchet ms",verdana,arial,sans-serif;font-size:12px;background:hsl(80, 100%, 96.2745098039%);border:1px solid #aaaa33;border-radius:2px;pointer-events:none;z-index:100;}#mermaid-svg-ZvejxbCEho065Re8 :root{--mermaid-font-family:"trebuchet ms",verdana,arial,sans-serif;} 构造函数注入 Data A_Data_ Method_A调用Data 这个才是面向对象的思想。
如果参数过多怎么办
方法1将参数写对象 method(string a,int b,int [] c)//改成method(Data a)class Data{public string a {get;set;}public int b {get;set;}public int[] c {get;set;}
}
方法2通过委托实现回调
这里不展开说
方法3依赖注入和IOC控制反转
这里不展开说