1. theoretical definition
The template method mode is pre-defined to implement some basic attributes and methods. The part that needs to be re-computed is overwritten by subclass or a new method is added.
2. Application Example
Requirement: ASP. NET custom controls have many common attributes and events.
The system. Web. UI. webcontrols. webcontrol class can implement custom controls.
Webcontrol has the basic methods and events of controls. When defining controls, we can stand on the shoulders of giants,
Avoid repeated wheel creation. Webcontrol is equivalent to a template. You can change the attributes of a template or add something to the template. The displayed content is different.
In the example of password strength detection, the strength of the password is controlled by modifying the strength attribute.
3. Specific Encoding
1. Password strength Enumeration
Using System; Using System. Collections. Generic; Using System. LINQ; Using System. text; Namespace Com. Design. gof. template { /// <Summary> /// Password strength enumeration attributes /// </Summary> Public Enum Strengthoption {verylow = 1 , // Poor Normer = 2 , // Average Good =3 , // Good Perfect = 4 // Very good, very strong, very good }}
2. Custom Controls for password strength
Using System; Using System. Collections. Generic; Using System. componentmodel; Using System. LINQ; Using System. text; Using System. Web; Using System. Web. UI; Using System. Web. UI. webcontrols; [Assembly: tagprefix ( " Com. Design. gof. Template " , " ASP " )] Namespace Com. Design. gof. template {[defaultproperty ( " Text " )] [Toolboxdata ( " <{0}: passwdstrength runat = Server> </{0}: passwdstrength> " )] Public Class Passwdstrength: webcontrol { /// <Summary> /// Current password strength /// </Summary> [Bindable ( True )] [Category ( " Appearance " )] [Defaultvalue (strengthoption. verylow)] [localizable ( True )] Public Strengthoption strength { Get { Object Bag = viewstate [ " Strengthoption " ]; If (BAG = Null ){ Return Strengthoption. verylow ;} Return (Strengthoption) viewstate [ " Strengthoption " ];} Set {Viewstate [ " Strengthoption " ] = Value ;}} Protected Override Void Rendercontents (htmltextwriter output ){ String CSS = "" ; Switch (Strength ){ Case Strengthoption. verylow: CSS = " Bg1 " ; Break ; Case Strengthoption. normer: CSS = " Bg2 " ; Break ; Case Strengthoption. Good: CSS = " Bg3 " ; Break ; Case Strengthoption. Perfect: CSS = " Bg4 " ; Break ; Default : Break ;} Output. Write ( " <Div class =' " + CSS + " '> </Div> " );}}}
3. ASPX page call controls
<% @ Page Language = " C # " Autoeventwireup = " True " Codebehind =" Template_passwdstrength.aspx.cs " Inherits = " Com. Design. gof. Test. Web. template_passwdstrength " %> <% @ Register Assembly = " Com. Design. gof " Namespace = " Com. Design. gof. Template " Tagprefix = " ASP " %> <! Doctype HTML public " -// W3C // dtd xhtml 1.0 transitional // en " " Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd " > <HTML xmlns = " Http://www.w3.org/1999/xhtml " > <Head> <title> </title> <style type = " Text/CSS " >Div {width: 180px; Height: 7px; Border -Left: 1px solid RGB ( 255 , 117 , 6 ); Margin-left: 5px; margin- Top: 10px} Div. Bg1 {Background: URL ( " /Images/pwd.png " ) No-repeat scroll 100 % 0 % Transparent;} Div. bg2 {Background: URL ( " /Images/pwd.png " ) No-repeat scroll 100 % 32 % Transparent;} Div. bg3 {Background: URL ( " /Images/pwd.png " ) No-repeat scroll 100 % 65 % Transparent;} Div. bg4 {Background: URL ( " /Images/pwd.png " ) No-repeat scroll100 % 100 % Transparent ;} </Style> " Passwdstrength1 " Runat = " Server " /> <P> General </P> <asp: passwdstrength strength = normer id = " Passwdstreng2 " Runat = " Server " /> <P> good </P> <asp: passwdstrength strength = good id = " Passwdstrength3 " Runat = " Server " /> <P> strong </P> <asp: passwdstrength strength = perfect id = " Passwdstrength4 " Runat =" Server " /> </Body>
4. Running result
5. Summary
Custom Controls
The attachment containsProgramSource code. It also includes testing of other projects, including the console and web.
This mode is tested using COM. Design. gof. Test. Web.
Download)