# Smart Contract Design Pattern for Processing Logically Coherent Transaction Types
## 3 The Pattern Design and Implementation
### Concrete Layer
```
packageconcreteSC; importadapT.AbstractTransaction; publicabstractsealedclassTransactionextendsAbstractTransactionpermitsTransactionIn,TransactionCross,TransactionGrid{ privatedoublequantity; privatedoublesourceSurplus; privatedoubletargetNeed; privatedoubletargetProduction; privatedoubletargetEnergySurplus; privateintsourceID; privateinttargetID; publicTransaction(doublequantity,doublesSurplus,doubleNeed,doubletargetProduction,doubletargetEnergySurplus,intsID,inttID){ this.quantity=quantity; this.sourceSurplus=sSurplus; this.targetNeed=tNeed; this.targetProduction=targetProduction; this.targetBatterEnergySurplus=targetBatterEnergySurplus; this.sourceID=sID; this.targetID=tID; } publicdoublegetQuantity(){returnquantity;} publicdoublegetSourceSurplus(){ returnsourceSurplus;} publicdoublegetTargetNeed(){ returntargetNeed;} publicintgetSourceID(){ returnsourceID; publicintgetTargetID(){ returntargetID;} publicdoublegetTargetProduction(){ returntargetProduction;} publicdoublegetTargetBatterEnergySurplus(){ returntargetBatterEnergySurplus;}} }
```
**Listing 5**. The source code of the _Transaction_ class. The _Transaction_ class is marked as sealed. The possibility of inheriting from the class is permitted only for transaction classes that should be handled by this smart contract. The inheritance tree was closed by marking descendant classes as final ones. Both features are visible in the source code of the _TransactionCross_ class shown in Listing 6. ```
packageconcreteSC; publicfinalclassTransactionCrossextendsTransaction{ privateintsourceCommunityID; privateinttargetCommunityID; publicTransactionCross(doublequantity,doublesSurplus,doubletNeed, doubletargetProduction,doubletargetEnergySurplus,intsID,inttID,intSCID,inttCID){ super(quantity,sSurplus,tNeed,targetProduction, targetBatteryEnergySurplus,sID,tID); this.sourceCommunityID=sID; this.targetCommunityID=tCID; } publicintgetSourceCommunityID(){ returnsourceCommunityID;} publicintgetTargetCommunityID(){ returntargetCommunityID;} }
```
**Listing 6**. The source code of the _Transaction_Cross class. The _TransactionIn_ and _TransactionGrid_ classes were implemented in the same way as the class for cross-community transaction type. Both classes inherit from the _Transaction_ class and terminate inheritance by employing the final keyword. The use of the _Transaction_ class, in _rulesList_ variable, also allows the processing of a list of rules on each of the specific types of transactions by the same method. The source code of the _AdapT_ v2.0 design pattern is available in the publicly accessible GitHub repository [43]. ## 4 Reuse and Redundancy Analysis

The effectiveness of the use of the software source code affects its maintenance. Raising the level of its reuse facilitates modifications and reduces the scope of testing. The author has introduced a measure \(U^{sc}\) as the percentage of reused verification rules under a smart contract. The \(U^{sc}\) is expressed by Equation (1). \[U^{sc}=\frac{\sum_{i=1}^{C}(\frac{u_{i}^{L}}{u_{i}})}{C}\times 100 \tag{1}\]

where:

\(C\)--the number of configurations in the smart contract;

\(u_{i}^{L}\)--the number of reused verification rules in \(i\)-th configuration in the smart contract;

\(u_{i}\)--the number of verification rules in \(i\)-th configuration in the smart contract. The value of the source code efficiency reuse \(U^{sc}\) for the smart contract considered in the article was calculated: \(U^{sc}=\frac{(0+1+\frac{3}{4})}{3}\times 100=\frac{7}{12}\times 100=58.3\%\). Configurations were taken for calculations in order from the most numerous to the least numerous. A score above 50% indicates a high level of source code reuse of validation rules. It should be remembered that if the design pattern was not applied, such would be the level of redundancy of verification rules. At run-time, it is also worth determining the level of efficiency of using the set of verification rule objects in configurations.