Design Mode (Java)-factory mode + policy mode + facade Mode

Source: Internet
Author: User

Scenario: design a sub-module in a transaction system ------ fee deduction sub-module

The fee deduction submodule consists of two parts:

1. IC card and transaction information

The IC card includes two types of amounts: fixed amount and free amount. The transaction information Class records each transaction.

2. Deduction strategies

There are two types of deduction policies:

A. Fixed Amount of IC Card = existing fixed amount of IC card-transaction amount/2

Free amount of IC Card = Free amount of IC card-transaction amount/2

B. All expenses are deducted from the free amount of the IC card.

Class chart implementation:

It mainly involves the following roles:

1. IC card and transaction

2. Debit Policy Interface

3. Deduction policy encapsulation class

4. Policy Enumeration class

5. Policy Factory

6. deduction module Encapsulation

7. Scenario

CodeImplementation

Card class:

 

Package Org. apache. java. designpatter. factoryandstrategy; public class card {private string no; private int steadymoney; // Private int freemoney of the fixed transaction amount in the card; // Public String getno () {return no ;} public void setno (string no) {No = no;} public int getsteadymoney () {return steadymoney;} public void setsteadymoney (INT steadymoney) {This. steadymoney = steadymoney;} public int getfreemoney () {return freemoney;} public void setfreemoney (INT freemoney) {This. freemoney = freemoney ;}}

Trade:

 

 

 
Public class trade {private string tradeno; private int Mount; // transaction amount Public String gettradeno () {return tradeno;} public void settradeno (string tradeno) {This. tradeno = tradeno;} public int getmount () {return Mount;} public void setmount (INT mount) {This. mount = Mount ;}}

Deduction Policy Interface

 

 

 
Public interface ideduction {public Boolean exec (card, trade );}

Deduction policies

 

 

 
Public class steadydeduction implements ideduction {public Boolean exec (card, trade Trade) {int halfmoney = (INT) math. RINT (trade. getmount ()/2.0); card. setsteadymoney (card. getsteadymoney ()-halfmoney); card. setfreemoney (card. getfreemoney ()-halfmoney); Return true ;}} public class freededuction implements ideduction {public Boolean exec (card, trade Trade) {card. setfreemoney (card. getfreemoney ()-trade. getmount (); Return true ;}}

Deduction policy Encapsulation

 

 

Public class deductioncontext {private ideduction deduction; Public deductioncontext (ideduction deduction) {This. deduction = deduction;} public Boolean exec (card, trade Trade) {return this.deduction.exe C (card, trade );}}

Rule Enumeration

 

 

 
Public Enum strategymanage {steadydeduction ("org. apache. java. designpatter. factoryandstrategy. steadydeduction "), freededucation (" org. apache. java. designpatter. factoryandstrategy. freededuction "); string value =" "; private strategymanage (string value) {This. value = value ;}public string getvalue () {return value ;}}

Policy Factory

 

 

Public class strategyfactory {public static ideduction getdeduction (strategymanage Strategy) {ideduction deduction = NULL; try {deduction = (ideduction) class. forname (strategy. getvalue ()). newinstance ();} catch (exception e) {e. printstacktrace ();} return deduction ;}}

Deduction module Encapsulation

 

 

 
Public class deductionfacade {public static card deduct (card, trade Trade) {strategymanage Reg = getdeducationtype (trade); // obtain the consumption policy ideduction deduction = strategyfactory. getdeduction (REG); // initialize a consumption policy object deductioncontext context = new deductioncontext (deduction); // execute the encapsulation context.exe C (card, trade); // execute the deduction return card ;} private Static strategymanage getdeducationtype (trade Trade) {If (trade. gettradeno (). contains ("ABC") {return strategymanage. freededucation;} else {return strategymanage. steadydeduction ;}}}

Scenario

 

 

 
Public class client {Private Static card initcard () {card = new card (); card. setno (""); card. setfreemoney (0); card. setsteadymoney (0); Return card;} Private Static trade inittrade () {Trade trade Trade = new trade (); trade. setmount (0); trade. settradeno (""); return trade;} public static void main (string [] ARGs) {card = initcard (); Boolean flag = true; while (FLAG) {Trade trade = inittrade (); deductionfacade. deduct (card, trade );}}}

 

 

The following design patterns are used in this scenario:

Strategy Mode: The load encapsulates the deduction policy to ensure that the two policies can be switched freely.

Factory mode: the Policy mode must expose the problem of a specific policy. The factory mode directly generates a specific policy object, and other modules do not need to rely on specific policies.

Facade mode: It is responsible for sealing and Transferring Complex fee deduction systems. The result of the transfer is to prevent high-level modules from going deep into the subsystem and provide high cohesion and low coupling characteristics of the system.

 

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.