Connect to "Database Synchronization System of Integrated instance analysis in design mode (2)".
6. Rule Mode
There are three ways to synchronize table data: Incremental synchronization, insert after delete, and temporary table. Therefore, you can define a synchronization policy interface datasynstrategy, three implementation classes are provided: incsynstrategy, delandinssynstrategy, and temptablesynstrategy. Class 8:
Figure 8 rule mode instance type Diagram
In figure 8, the Oracle table synchronization object class oracletabledbsynchronizer acts as the Environment class, datasynstrategy acts as the abstract policy class, and its subclasses incsynstrategy, delandinssynstrategy, and temptablesynstrategy act as the specific policy class.
Datasynstrategy is used as the local variable of the synsingletable () method in oracletabledbsynchronizer. Therefore, oracletabledbsynchronizer and datasynstrategy are dependent. If they are global variables, they are associated.
7.Combination Mode, command mode, and responsibility chain mode
When using temporary tables to synchronize tables, you can define a series of command objects. These commands encapsulate operations on the database, because some operations modify the database structure, therefore, the traditional JDBC transaction control does not play a role, and you need to implement the rollback logic after the operation fails. In this case, you can use the command mode for design. In addition, you can also provide the macro command macrocommand to combine some commands that execute database operations to form a composite command. As shown in Figure 9 (because the original class chart is complex, taking into account readability, figure 9 is simplified, but it is complicated after being simplified ,):
Figure 9 combined mode, command mode, and responsibility chain mode instance class diagram
(This figure is a bit complex because it involves the combination of multiple modes ,)
In Figure 9, temptablesyncommand acts as the abstract command, macrocommand acts as the macro command class, renametablecommand, syntabledatacommand, and renametableconstraintcommand act as the specific command, temptablesynstrategy acts as the request caller, and datasynhelper acts, datasynhelper defines some methods to assist in synchronizing temporary tables. These methods are called in the Command class. The common execute () method is declared in temptablesyncommand, And the rollback method undo () is provided. Its subclass implements the specific execution and recovery operations. Datasynhelper interface declares the methods for database operations, and implements these methods in its subclass datasynhelperimpl.
The temptablesyncommand also defines two sub-types of variables previuscommand and nextcommnadare used to save the previous command and the next command. nextcommnadis used after the business logic of the current command is executed, then, execute the business logic of the next command. previuscommand is used to call the Undo () method of the previous command to restore an exception. Worker uses the duty chain model. nextcommnad.exe cute () implements a forward responsibility chain, while previouscommand. Undo () and Java's Exception Handling Mechanism implement a reverse responsibility chain.
Macrocommand is a macro command. Its code snippets are as follows:
Public class macrocommand extends temptablesyncommand {temptablesyncommand lastcommand = This; Public void add (temptablesyncommand) {temptablesyncommand. setpreviuscommand (lastcommand); lastcommand = temptablesyncommand; // create a command chain} protected void execute () throws exception {......} Protected void undo () throws exception {......}}
In the request caller's temptablesynstrategy class, use the following code snippet to call the execute () method of the macro command object:
Public class temptablesynstrategy extends datasynstrategy {Public String processsyn () {// other code omitting string temptablename = generatetemptablename (); string backuptablename = "Bak _" + temptablename; datasynhelper = new datasynhelperimpl (); macrocommand Marcocommand = new macrocommand (); Marcocommand. add (New renametableconstraintcommand (datasynhelper, tablename, destdb); Marcocommand. add (New syntabledatacommand (datasynhelper, tablename, temptablename, srcdb, destdb); Marcocommand. add (New renametablecommand (datasynhelper, tablename, backuptablename, destdb); Marcocommand. add (New renametablecommand (datasynhelper, temptablename, tablename, destdb1_try1_marcocommand.exe cute (); try {// other Code omitted} catch (exception e) {e. printstacktrace () ;}} catch (exception e) {e. printstacktrace ();} // other Code omitted }}
[The analysis of this instance has ended. I hope it will help you ,!]
[Author: Liu Wei http://blog.csdn.net/lovelion]