usingSystem;usingSystem.Collections.Generic;usingSystem.Text;namespacesingleresponsibilityprinciple{//Single-accusation principle (Responsibility Principle) SRP//There should never is more than one reason for a class-to-change. //There is only one reason for the class to change. classProgram {//Usage Scenarios Static voidMain (string[] args) { //A single function applies to interfaces, classes, methods. //let's start by looking at how interfaces represent a single function. //we all know that since the iphone uses the Usb_type_c interface, many manufacturers are also really afraid to join the use of the interface. //the Usb_type_c interface of the new MacBook is capable of transmitting data, charging it, and can be used as a video output port to link external display devices. //It is clear that the principle of single duty is violated! interface contains three functions at all irrelevant, if the user to charge at the same time, to transfer data, to connect the monitor, how to do? Apple has weighed against the MacBook's thickness and weight. //This is the product design on the trade-offs, you can not say wrong, each has pros and cons, for software development, is the same. //For example, the analog user uses the Usb_type_c interface to charge the phone. Iphone6 iphone =NewIphone6 (); XiaoMiM5 Xiaomi=NewXiaoMiM5 (); Charger_c (iphone); Charger_c (Xiaomi); //now Flash charging technology is not what news, we assume that the iphone can't wait to upgrade the charging function to flash charge, or users do not buy AH. //think about what's going to happen? Usb_type_c has 3 reasons (charging, data transmission, display) will cause the class changes, because the iphone implements the Usb_type_c interface, want to upgrade, it is necessary to Usb_type_c interface declaration Flash charging function. //this time millet and other use of usb_type_c related to the charging of the manufacturers do not do ah, Usb_type_c upgrade flash charge, we have to change AH (subclass must implement the interface declaration method, because only the internal charging method changes, the interface has not changed, So the transmission and display related equipment manufacturers do not have to change, charging equipment manufacturers need to adapt to the new method), increased costs and risks, the price is not a fever ah, users do not buy AH. //What's that? You said create a new Flash_usb_type_c interface? Let Apple play it by itself? The transfer and display functions have not changed. Just because of the flash charge and add a new interface Flash_usb_type_c, the previous Apple related to the Usb_type_c interface code is not all to adapt to the new Flash_usb_type_c interface? //and you have to call other iphone auxiliary equipment manufacturers to adapt to the new interface (including transmission and display related equipment manufacturers, because the interface has changed, but in fact, the internal transmission and display method has not changed, resulting in different interfaces, declaring the actual same function, become complex), Apple is also out of work (although the interface of the previous Apple product is maverick ...). )。 //tear Force War from now on, who told you to choose to follow the use of Usb_type_c interface, the responsibility of a single? Meizu and Huawei say there is no pressure, because it implements different interfaces for each function. Meizu mz =NewMeizu (); Charger_n (MZ); HuaWei HW=NewHuaWei (); Charger_n (HW); //Meizu want to upgrade on the upgrade, Iflashcharge Flash charger is a separate charging function module, and the industry has matured, Meizu replaced the charging interface can be, Huawei certainly unaffected, still use the previous charging technology icharger~Flashmeizu FMZ =NewFlashmeizu (); Flashcharger (FMZ); //transmission, display technology replacement, the principle is the same, if not meet the responsibility of a single, we toss together. Therefore, the responsibility of the interface is single, mainly reflected in the change. //A single, well understood approach, such as a scenario where a user dials a phoneIphone6 IP6 =NewIphone6 (); //Please press F12 to enter function to see detailsIP6. Callpeople ("1307779****"); //In line with the single method of responsibility, you might think that the Srpcallpeople method itself seems to be not a single function, so many ways? This needs to be considered at different levels. //Srpcallpeople is already a business logic handler method, and it is not a function anymore, it will definitely call other methods to implement some kind of business. The functions of the method are mainly embodied in the function. IP6. Srpcallpeople ("1307779****"); //Finally, the responsibility for a class is a singular one, largely similar to a method. //For example, a variety of library, each class library to implement its own related functions (see Project-Reference), if not according to the responsibility of a single, without namespace distinction, they can exist in a class file completely Ah, is simply the Code Chaos World! //so the more responsibilities a class (large to module, small to method) takes, the less likely it is to be reused, and the more responsibilities a class assumes, the equivalent of coupling those responsibilities together, which may affect the operation of other responsibilities when one of the responsibilities changes.//so to separate these responsibilities, encapsulate different responsibilities in different classes, encapsulate different causes of change in different classes, and encapsulate them in the same class if multiple responsibilities are always changed at the same time. //a single responsibility, that is, let us consider high cohesion, low coupling } //PS. Interface-oriented programming, focusing only on the code of conduct, who is the object class. Low coupling//I'm a regular charger. Static voidcharger_n (Icharge device) {device. Charge (); } //I am a support Usb_type_c charger ~ (Flash_usb_type_c This interface charger does not exist) Static voidcharger_c (Iusb_type_c device) {device. Charge (); } //charge 5 minutes, call 2 hours (Flash charger has matured, many manufacturers have started production) Static voidFlashcharger (Iflashcharge device) {device. Flashcharge (); } } Public classIphone6:iusb_type_c {//to call someone Public voidCallpeople (stringNumber ) { //have you found the problem wood? The method of making a call all in this function implementation, a good responsibility for a single? //Think about the length of the code, it looks like it's not very faint, you probably don't know what these different codes are for, and it's easy to introduce bugs. //and, check the phone, make a call, show the operators, these features are not only implemented here. //so it is best to extract, do the "method" of a single, not only good understanding, other places can also be called, reduce redundancy, reuse code, improve efficiency ~Console.WriteLine ("Check if the phone number is legal"); if(number. Length = = One) {Console.WriteLine ("Make a phone call"); } if(number. Substring (0,3). Contains (" the") {Console.WriteLine ("Show phone number operator, China Unicom"); } } //meet the responsibility of a single call method yo Public voidSrpcallpeople (stringNumber ) {checknumber (number); Call (number); Showoperator (number); } Private BOOLCheckNumber (stringNumber ) {Console.WriteLine ("single Check phone number is legal"); if(number. Length > One) { return true; } Else { return false; } } Private voidCall (stringNumber ) {Console.WriteLine ("Single phone call"); } Private voidShowoperator (stringNumber ) { if(number. Substring (0,3). Contains (" the") {Console.WriteLine ("single display phone number operator, China Unicom"); } } Public voidHangup () {Console.WriteLine ("End of Call"); } Public voidTrandata () {Throw Newnotimplementedexception (); } Public voidCharge () {Console.WriteLine ("Iphone6 charging ing"); } Public voidDisplay () {Throw Newnotimplementedexception (); } } Public classXiaomim5:iusb_type_c { Public voidTrandata () {Throw Newnotimplementedexception (); } Public voidCharge () {Console.WriteLine ("Xiaomi M5 charge ing"); } Public voidDisplay () {Throw Newnotimplementedexception (); } } Public classMeizu:itrandata, Icharge, IDisplay { Public voidTrandata () {Throw Newnotimplementedexception (); } Public voidCharge () {Console.WriteLine ("Meizu charge ing"); } Public voidDisplay () {Throw Newnotimplementedexception (); } } Public classHuawei:itrandata, Icharge, IDisplay { Public voidTrandata () {Throw Newnotimplementedexception (); } Public voidCharge () {Console.WriteLine ("Huawei charge ing"); } Public voidDisplay () {Throw Newnotimplementedexception (); } } Public classFlashmeizu:iflashcharge, Itrandata, IDisplay { Public voidTrandata () {Throw Newnotimplementedexception (); } Public voidDisplay () {Throw Newnotimplementedexception (); } Public voidFlashcharge () {Console.WriteLine ("Xiaomi Flash charge, charge 5 minutes, call 2 hours! ~"); } } Public InterfaceIusb_type_c {voidTrandata (); voidCharge (); voidDisplay (); } Public InterfaceItrandata {voidTrandata (); } Public InterfaceIcharge {voidCharge (); } Public InterfaceIflashcharge {voidFlashcharge (); } Public InterfaceIDisplay {voidDisplay (); }}
Single accusation principle