The code is merely illustrative, and the actual varying process in the project development process: defines a query form to use the DataGrid display list After you open the edit page of the specified record, the form class is formsearchenTiThe Private void Grid_doubleClick (Object sender, system.eventargs e) {string entityID = Double-click the recorded ID field value; // This has a fixed method formentity = new formentity (entityID); ........ FRMENTITY. Show ();} where the Formentity is the editing interface of the business entity, which is incorporated into the constructor, and then loads the relevant data of the record, which is not a key explanation here.
Next, you have to add a button "Go" on the query interface, the execution action and the Grid double-click, that is, the record is selected in the GRID, click on the Open entity's operation interface.
Thus, using the Extract Method reconstruction techniques: private void Grid_DoubleClick (object sender, System.EventArgs e) {string entityID ID field value recorded double-click =; OpenEntityForm (entityID);} private void btnGo_Click (object sender, System. Eventargs e) {String EntityID = Double-click the ID field value; OpenEntityform (entityID); }
What is the use of this now? Did you call the Grid DoubleClick directly in the Go's Click time? In fact, Extract Method is not only to prevent repetition, but also improves the reusability of code, and the role will be seen below.
Now, it is necessary to do the same operation, then define a form, change the above code, but there is a duplication code, which is a bad taste. Then such as: the method to OpenEntityForm Virtual, while declared Protected, which removed the code protected void OpenEntityForm (string entityID) {} FormSearchEntityBase renamed to form a re-write FormSearchEntityA class to inherit FormSearchEntityBase, override the parent class OpenEntityForm method protected override void OpenEntityForm (string entityID) {FormEntityA frmEntityA = new FormEntityA (entityID); ........ frmEntityA.Show ();} query interface entity B inherits from the same FormSearchEntityB FormSearchEntityBase, override the superclass the method of OpenEntityForm protected override void OpenEntityForm (string entityID) {FormEntityB frmEntityB = new FormEntityB (entityID); ........ frmEntityB.Show ();} Thus, if the needs and there are the same, it is inherited from FormSearchEntityBase A class, Override parent class OpenEntityForm method can now take a look at the TemplateMethod mode: Define the skeleton of an algorithm in an operation, and delay some steps into the subclass. Applicability: Distribute a constant portion of an algorithm in one time, and leave a variable behavior to subclasses. Public behavior in each subclass should be extracted and concentrated in a public parent class to avoid code repeat control subclasses
Example code: namespace templateMethod_designpattern {using system;
Class Algorithm {Public Void Doalgorithm () {Console.Writeline ("in doaalgorithm"); // do some part of the algorithm here // Step1 Goes here concele.writeline ("in algorithm - doalgostep1); //..
// Step 2 Goes here console.writeline ("in algorithm - doalgostep2"); //.
// Now call configurable / replacable part doalgostep3 ();
// Step 4 Goes here console.writeline ("in algorithm - doalgostep4"); //.
// Now call next configurable part doalgostep5 ();
Virtual public void doalgostep3 () {Console.Writeline ("in algorithm - doalgostep3);}
Virtual public void doalgostep5 () {console.writeline ("in algorithm - doalgostep5);}}
Class Customalgorithm: Algorithm {Public Override Void Doalgostep3 () {Console.WriteLine ("In Customalgorithm - DoalGostep3);}
Public override void doalgostep5 () {Console.writeline ("in customAlgorithm - doalgostep5");}}
///
/// ??? Summary description for client. ///
Public Class Client
{
Public static int main (String [] ARGS)
{
Customalgorithm C =
New Customalgorithm ();
C. Doalgorithm ();
Return 0;}}}}}}}
Then compare the code of the code and the example of the templateMethod, which is changed to the form class, this is a templateMethod mode.
One point of view is that the design mode often leads to transition design, and current agile methods and reconstruction gradually advocates code evolution and reconstruction to meet design patterns. I also found that this is already a pattern after writing the code.