Enterprise Application Architecture Patterns-Things script

Source: Internet
Author: User

Overview: Using procedures to organize business logic, each process processes individual requests from the presentation layer

Characteristics
(1) Data-centric (input-to-query, processing-based output), typical process-oriented thinking)
(2) Simple code, easy to understand
(3) Suitable for the development of small projects
(4) The business logic and domain logic are mixed in the thing script, it is easy to produce redundant data

Organization mode
(1) A class dealing with a kind of thing script, we are very familiar with the programming way

1  Public classgateway{2     Private  Static FinalString findrecognitionsstatement = "Select amount from revenuerecognitions where contract =?" and Recognizedon <=? "3     Private  Static FinalString findcontractstatement = "SELECT * from Contracts c,product p where id =?" and c.product = P.id "; 4     Private  Static FinalString insertrecognitionstatement = "INSERT into revenuerecognitions values (?,?,?)";5     PrivateConnection _db;6      PublicResultSet Findrecognitionsfor (LongContractid,date asof)throwssqlexception{7PreparedStatement stmt =db.preparedstatement (findrecognitionsstatement);8Stmt.setlong (1, Contractid);9Stmt.setdate (2, asof);Ten             returnstmt.executequery (); One     } A      PublicResultSet Findcontract (LongContractid)throwssqlexception{ -PreparedStatement stmt =db.preparedstatement (findcontractstatement); -Stmt.setlong (1, Contractid); the             returnstmt.executequery (); -     } -      Public voidInsertrecognition (LongContractid,money amount,date asof)throwssqlexception{ -PreparedStatement stmt =_db.preparedstatement (insertrecognitionstatement); +Stmt.setlong (1, Contractid); -Stmt.setbigdecimal (2, Amount.amount ()); +Stmt.setdate (3, asof); A stmt.executeupdate (); at     } -}

Class Recognitionservice{private Gateway _db;public money recognizedrevenue (Long contractnumber,date asof) {Money result = Money.dollar (0); Try{resultset rs = _db.findrecognitionsfor (contractnumber,asof); while (Rs.next ()) {result = Result.add (Money.dollars (Rs.getbigdecimal ("Amount"));} return result;} catch (Exception ex) {throw new RuntimeException (ex);}} public void Calculaterevenuerecognitions (long contractnumber) {Try{resultset contracts = _db.findcontract ( Contractnumber); Contracts.next (); Money totalrevenue = Money.dollars (Contracts.getbigdecimal ("revenue"));D ate recognitiondate = new Date ( Contracts.getdate ("datasigned")); String type = contracts.getstring ("type"), if (Type.equals ("S")) {money[] allocation = totalrevenue.allocate (3); _ Db.insertrecognition (contractnumber,allocation[0],recognitiondate); _db.insertrecognition (ContractNumber, Allocation[1],recognitiondate.adddays) _db.insertrecognition (contract,allocation[2], Recognitiondate.adddays (90));} else if (type.equals ("W")) {_db.insertrecoGnition (contractnumber,totalrevenue,recognitiondate);} else if (type.equals ("D")) {money[] allocation = totalrevenue.allocate (3); _db.insertrecognition (Contractnumber, Allocation[0],recognitiondate); _db.insertrecognition (Contractnumber,allocation[1],recognitiondate.adddays (30)) ; _db.insertrecognition (Contract,allocation[2],recognitiondate.adddays (60));}}  catch (Exception ex) {throw new RuntimeException (ex);} }}


(2) A class handles a thing script, which can be organized in a command-mode manner. Command pattern

1     interface transactionscript{Object run ()}


  

1 classRecognizedrevenuetransationscriptImplementstransationscript{2         Private Long_contractnumber;3         PrivateDate _asof;4          PublicRecognizedrevenuetransationscript (Longcontractnumber,date asof) {5_contractnumber =Contractnumber;6_asof =asof;7         }8          PublicObject Run () {9Money result = Money.dollar (0);Ten             Try{ OneResultSet rs =_db.findrecognitionsfor (_contractnumber,_asof); A                  while(Rs.next ()) { -result = Result.add (Money.dollars (Rs.getbigdecimal ("Amount"))); -                 } the                 returnresult; -}Catch(Exception ex) { -                 Throw NewRuntimeException (ex); -             } +         } -      +}
1 classCalculaterevenuerecognitionstransationscriptImplementstransationscript{2         Private Long_contractnumber;3         4          PublicCalculaterevenuerecognitionstransationscript (LongContractnumber) {5_contractnumber =Contractnumber;6_asof =asof;7         }8          PublicObject Run () {9             Try{TenResultSet contracts =_db.findcontract (contractnumber); One Contracts.next (); AMoney totalrevenue = Money.dollars (Contracts.getbigdecimal ("revenue")); -Date recognitiondate =NewDate (Contracts.getdate ("datasigned")); -String type = contracts.getstring ("type"); the                 if(Type.equals ("S")){ -money[] allocation = totalrevenue.allocate (3); -_db.insertrecognition (contractnumber,allocation[0],recognitiondate); -_db.insertrecognition (Contractnumber,allocation[1],recognitiondate.adddays (60)); +_db.insertrecognition (Contract,allocation[2],recognitiondate.adddays (90)); -}Else if(Type.equals ("W")){ + _db.insertrecognition (contractnumber,totalrevenue,recognitiondate); A}Else if(Type.equals ("D")){ atmoney[] allocation = totalrevenue.allocate (3); -_db.insertrecognition (contractnumber,allocation[0],recognitiondate); -_db.insertrecognition (Contractnumber,allocation[1],recognitiondate.adddays (30)); -_db.insertrecognition (Contract,allocation[2],recognitiondate.adddays (60)); -                 } -}Catch(Exception ex) { in                 Throw NewRuntimeException (ex); -             } to             return NULL; +         } -      the}



Most companies are now developing software in this way, for the following reasons

(1) Essential business is not complicated
(2) Low learning costs (data-centric programming without considering abstractions, polymorphism, etc.)
(3) This type of development has been identified by most companies as a development standard
(4) The company does not value the development process, only see the final results

Summarize:
This type of development is typically a classic process-oriented approach that can be very well solved when the business is not complex.
Business issues.
Because a feature executes a script, there must be redundant code as the business continues to become complex.
For example, the transfer business and the withdrawal of the business, will appear the same query account and verification.
You can solve the problem by extracting a method, or by extracting a public class.
But increased complexity, contrary to the simple intention of the script of things.

Enterprise Application Architecture Patterns-Things script

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.