Object Oriented Programming , OOP
This article describes how to use object-oriented thinking Programming
The following order is used as an example.Web, Fill in the contact information, the service you have purchased (just a fewCheckboxTo buy a few services, select a fewCheckbox, The price is calculated based on the selected service), online payment; after clicking the "Submit" button, the following operations will be performed: save to the database, pay by credit card, generate an orderPDFAnd send an email to notify the customer.
AverageCodeFor example:
Public Class Ordercontroller { OrderdalDal =New Orderdal(); Public GuidPlaceorder (OrderinfoOrderinfo) { // Verify the function, omitted GuidOrderid =Guid. Empty; BoolSaveok =False; Using(TransactionscopeTS =New Transactionscope()) { Orderid = Dal. insertorder (orderinfo ); If(! Orderid. Equals (Guid. Empty )) If(Paymentservice. Pay (orderinfo. Price )) Saveok =True; If(Saveok) TS. Complete (); } If(Saveok) { Generateneworderpdf (orderinfo ); Sendemail2client (orderinfo ); } ReturnOrderid; } Private VoidSendemail2client (OrderinfoOrderinfo) { Throw New Notimplementedexception(); } Private VoidGenerateneworderpdf (OrderinfoOrderinfo) { Throw New Notimplementedexception(); } } |
|
The above code isMartin folwerSaid"Transaction Script Mode"That is, a business method is written in a function, and a large part is written from start to end.
What should I do if I want to write in object-oriented mode? The answer is (I personally think...Haha): Draw a sequence diagram and encode it.
This is because the use of sequence diagrams can help you find objects and communication methods between objects, as shown below:
This figure seems complicated. Therefore, only when the system is complicated (business logic) Can we choose to split objects in an object-oriented manner. The benefits include:
1.Each object is actually a separation of concerns, so it is easy to maintain
A.For example, to verify it, the modification is very targeted and it is easy to locate the class to be modified.
B.The calculation price is the same, and it is easy to locate the location where the change is needed.
2.In unit testing, you can test each class separately without havingPlaceorderWrite countless test functions (divide and conquer)
A.For "transaction Script Type ",PlaceorderWrite unit test&&Code coverage is required& Gt; = 70%That requires a lot of verification.
B. OOP the code can be used to write unit tests for each class , code coverage = 70% easier to achieve