Template Method
This mode places a general algorithm in the base class, and implements the general algorithm by inheriting in different specific classes, this mode cost is that the derived class must be bound to the base class.
In the iPass project, there is a feature imported from the file. First, open the file, follow the CSV format progressive, for unqualified filtering, the qualified saved, finally displayed on the interface, and then determine whether the import function is executed.
Public Abstract Class BaseImporter {
List list = new arraylist ();
Public void OpenFile (String filename) throws exception {
BufferedReader Br = New BufferedReader (New InputStreamReader (NEW
New fileInputstream (filename))))
String newline;
While (NEWLINE = Br.Readline ())! = NULL) {
Object o = valid (newline);
IF (o! = null) {
List.Add (o);
}
}
}
Public Object [] getimportinfo () {
Return list.toArray ();
}
Public Abstract Object Valid (String L ine);
}
Public Class Userimporter Extends BaseImporter {
Public Object Valid (String line) {
String [] info = cvsparser.parse (line);
// invalid
IF (Info.Length! = 2) {
Return NULL;
}
Return New UserImportInfo (INFO);
}
}
Each import page generates your own importer, call the OpenFile method to get the right content.
STRATEGY
This mode uses delegate to implement the reuse of the algorithm. Place the general algorithm in a specific class, the abstract method to be called by the general algorithm is defined in the interface, and the derived class implements this interface to implement the specific business.
In the figure below, it is realized that the batch import function, the above example is imported to the page display, this example is imported to the background. Due to too many numbers of imported elements, a full import cannot be implemented, one is too wasteful, and now the plan is to import 300. The batch import algorithm is implemented in the specific class of Batchimporter, which calls the ImportHandler's ImportOperation () abstraction method to implement specific import services. UserImportPage and GroupImportPage are imported from page and import groups, which implements the ImportHandler interface, and implements its own import operations in the Importoperty method. At the same time, they also reserve the BatchImporter instance, when a specific event is activated (such as pressing the import button), performing the BatchImporter.doImport () for import operations. Batchimporter should be delegated to the derived class of ImportHandler.
Public class batchimporter {
Int SendTimes; // How Many Times Needs
Int Total; // Total Number of Objects To Import
INT SKIP = 300; // Import 300 Objects Each Time
Importhandler Hander;
Public BatchImporter (int Total) {this.hander = handler;
THIS. TOTAL = Total;
Sendtimes = Total / Skip;
IF (Total% Skip> 0) {
SendTimes ;
}
}
Public void doimport () {
For (int i = 0; i // Import SendNumber Objects this Time INT sendnumber = SKIP; IF (i == sendtimes - 1) { Skip = Total - I * SKIP; } // Import from the position Skip, And Import SendNumber Objects Hander.Importoperation (i * skip, sendnumber); } } } Strategy allows each specific implementation to be manipulated by different general algorithms, which has a greater degree of freedom than Template Method, which reduces the coupling between common algorithms and specific details. But more running time and data space overhead is required.