SOLID Design Principles In C # code implementation

Source: Internet
Author: User

[S] Single Responsibility Principle (Single Responsibility Principle) considers that an object should only have one Single Responsibility to copy the code namespace SingleResponsibilityPrinciple {class DataAccess {void InsertData () {Console. writeLine ("data inserted successfully");} // Incorrect Design, does not comply with the single responsibility principle // void WriteLog () // {// Console. writeLine ("log inserted successfully"); // You should assign different responsibilities to different objects for processing class Logger {void WriteLog () {Console. writeLine ("log inserted successfully") ;}} copy the Code [o] Open Close Principle (Open and closed Principle). The software should be Open to expansion, and the modification should be closed. Closed replication code namespace OpenClosePrinciple {abstract class DataProvider {public abstract void OpenConnection (); public abstract void CloseConnection (); public abstract void ExecuteCommand ();} // ms SQL class SqlDataProvider: dataProvider {public override void OpenConnection () {Console. writeLine ("enable SQL data connection");} public override void CloseConnection () {Console. writeLine ("Close SQL data connection");} public override v Oid ExecuteCommand () {Console. writeLine ("run SQL data command") ;}// ORACLE class OracleDataProvider: DataProvider {public override void OpenConnection () {Console. writeLine ("Open Oracle Data Connection");} public override void CloseConnection () {Console. writeLine ("Close Oracle Data Connection");} public override void ExecuteCommand () {Console. writeLine ("execute Oracle Data command");} class Start {void Invoke () {DataProvider dataProvider = New SqlDataProvider (); dataProvider. openConnection (); dataProvider. executeCommand (); dataProvider. closeConnection (); dataProvider = new OracleDataProvider (); dataProvider. openConnection (); dataProvider. executeCommand (); dataProvider. closeConnection () ;}} copy the Code [L] Liskov Substitution Principle (RYS replacement Principle) the object in the program should be the replication code namespace LiskovSubstitutionPrinciple {class Rectangle {that can be replaced by its subclass without changing the program's correctness {Protected int width = 0; protected int height = 0; public virtual void SetWidth (int width) {this. width = width;} public virtual void SetHeight (int height) {this. height = height;} public virtual int GetArea () {return this. width * this. height ;}} class Square: Rectangle {public override void SetHeight (int height) {this. height = height; this. width = height;} public override void SetWidth (in T width) {this. height = width; this. width = width ;}} class Start {public static Rectangle CreateInstance (int condition = 1) {if (condition = 1) {return new Rectangle ();} else {return new Square () ;}} public static void Invoke () {Rectangle rectangleObject = CreateInstance (); rectangleObject. setHeight (10); rectangleObject. setWidth (20); rectangleObject. getArea () ;}} copy the Code [I] Interface Seg Regation Principle (interface separation Principle) considers that multiple specific client interfaces are better than one interface for a broad purpose. It also has a single responsibility to copy the code namespace InterfaceSegregationPrinciple {interface IDataProvider {void OpenConnection (); void CloseConnection ();} interface response: IDataProvider {void ExecuteSqlCommand ();} interface response: IDataProvider {void ExecuteOracleCommand ();} class SqlDataProvider: Response {public void ExecuteSqlCom Mand () {Console. writeLine ("run SQL data command");} public void OpenConnection () {Console. writeLine ("enable SQL data connection");} public void CloseConnection () {Console. writeLine ("Close SQL data connection") ;}} class OracleDataProvider: IOracleDataProvider {public void ExecuteOracleCommand () {Console. writeLine ("execute Oracle Data command");} public void OpenConnection () {Console. writeLine ("Open Oracle Data Connection");} public void CloseConnection () {C Onsole. writeLine ("Close Oracle Data Connection") ;}} class Start {public void Invoke () {ISqlDataProvider sqlDataProvider = new SqlDataProvider (); sqlDataProvider. openConnection (); sqlDataProvider. executeSqlCommand (); sqlDataProvider. closeConnection (); IOracleDataProvider oracleDataprovider = new OracleDataProvider (); oracleDataprovider. openConnection (); oracleDataprovider. executeOracleCommand (); oracleDataprov Ider. closeConnection () ;}} copies the Code [D] Dependency Inversion Principle (Dependency Inversion Principle). A method should be dependent on abstraction rather than an instance and control Inversion, dependency injection is an implementation of this principle to copy the code namespace DependencyInversionPrinciple {interface IBankAccount {long BankNumber {get; set ;}// card number decimal Balance {get; set ;} // balance} // transferor interface ITransferSource: IBankAccount {void CutPayment (decimal value);} // recipient interface ITransferDestination: IBankAccount {v Oid AddMoney (decimal value);} class BankAccout: IBankAccount, ITransferSource, ITransferDestination {public long BankNumber {get; set;} public decimal Balance {get; set ;} public void CutPayment (decimal value) {Balance-= value;} public void AddMoney (decimal value) {Balance + = value ;}} class TransferAmount {public decimal Amount {get; set;} public void Transfer (ITransferSource source, ITransferDestination dest) {source. cutPayment (Amount); dest. addMoney (Amount) ;}} class Start {void Invoke () {ITransferSource source = new BankAccout {Balance = 10000, BankNumber = 111}; ITransferDestination dest = new BankAccout {Balance = 1, bankNumber = 222}; TransferAmount transfer = new TransferAmount (); transfer. amount = 9999; // The Amount of money transferred to transfer. transfer (source, dest); // start Transfer // This is based on The reverse thinking enables bank transfers so that bank accounts can rely on ITransferSource and ITransferDestination // instead of the original direct BankAccout account = new BankAccout (); this greatly increases the flexibility of the core object of the account. // This example basically contains the SOLID principle, easy to understand class bank account {public int card number {get; set;} public decimal balance {get; set;} public void deposit (decimal value) {balance + = value ;} public void expenditure (decimal value) {balance-= value ;}} class bank transfer operation // ATM {public bank account transferor {get; Set;} public bank account payee {get; set;} public decimal amount {get; set;} public void transfer () {transferer. expenditure (amount); payer. deposit (amount) ;}} class Start1 {public void Main () {bank account Zhang San = new bank account {card number = 111, balance = 10000 }; bank account Li Si = new bank account {card number = 222, balance = 5}; bank transfer operation transfer = new bank transfer operation (); Transfer. amount = 1000; transfer. transferor = James; transfer. recipient = Li Si; transfer money. transferring money () ;}} copying code SOLID design principles is a common topic. I didn't find a good code implementation in the blog garden, therefore, I wrote such a blog by referring to the online materials. This blog is not highly technical and can be viewed by new users, including me and People think it takes some time to master these five design principles and be proficient in their systems. My point is: business-driven design principles. If you are not familiar with the business field, you will not be able to take advantage of these advantages. You will think it is troublesome to do so, but pay high respect for the design principles and design patterns, repeat the examples in the online books twice and three times... You only need to write various principles and code templates of the pattern. So, a good way to learn the design model is to first understand the logic of a set of business fields, and then use the thought of the design model you have learned to pick a thorn. The design of the domain-driven model has inspired me a lot. I first respect the business field, then the software design, and finally the code. Of course, we should always look at things with reverence.

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.