C # design pattern learning notes - simple factory model

zhaozj2021-02-16  100

---------- Sample

------------------------------

Using

System;

Namespace

Factory

{/ ** ////

/// Name Abstract Description. /// public class name // base class {protected string fname, lname; public string getFname () {return this.fname;} public string getlname () {return this.lname;}}}

---------------------------

Using

System;

Namespace

Factory

{/ ** ////

/// first Summary Description. /// public class first: Namer // Inherited a method of separating the name {public first (String Name) {INT i = Name.trim (). Indexof (""); if (i > 0) {FNAME = Name.SubString (0, i) .trim (); lname = name.substring (i 1) .trim ();} else {lname = name; fname = "";}}}}}}}}}

------------------------------

Using

System;

Namespace

Factory

{/ ** ////

/// second Abstract Description. /// public class second: Namer // Inherited a comma separate name {pUBLIC Second (String name) {Int i = name.indexof (","); if (i> 0 ) {LNAME = Name.Substring (0, i); fname = name.substring (i 1) .trim ();} else {lname = name; fname = ";}}}}

-------------------------------

System;

Namespace

Factory

{/ ** ////

/// namefactory summary description. /// Public class namefactory // factory class {public name getname (string name) {= name.indexof (","); if (i> 0) {return new second (name);} Else {Return New First (Name);}}}}

--------------------------------

/ ** /

/// Simple Factory Mode Example //

Using

System;

Using

System.drawing;

Using

System.collections;

Using

System.componentmodel;

Using

System.windows.forms;

Using

System.data;

Namespace

Factory

{/ ** ////

/// Form1 Summary Description. /// public class Form1: System.Windows.Forms.Form {private System.Windows.Forms.Label label1; private System.Windows.Forms.TextBox txname; private System.Windows.Forms.Button btComputer; private System.Windows.Forms.Label label2; private System.Windows.Forms.Label label3; private System.Windows.Forms.TextBox txlname; private System.Windows.Forms.TextBox txfname; private System.Windows.Forms.Label label4; / ** //// // The required designer variable. /// private system.componentmodel.container Components = NULL; Public Form1 () {// // Windows Form Designer Support To Support // InitializationComponent (); // // Todo: Call in InitializationComponent Add any constructor code //} / ** //// /// clean all the resources being used. /// Protected Override Void Dispose (Bool Disposing) {if (disponents! = Null) {Components.dispose ();}} Base.Dispose (DISPOSISING);} Windows Form Designer The generated code #REGION Windows Form Designer Generate / ** //// /// Designer Supports the required method - Do not use the code editor to modify the // / this method.

/// private vid initializecomponent () {this.label1 = new system.windows.forms.label (); this.txname = new system.windows.Forms.TextBox (); this.btcomputer = new system. Windows.Forms.Button (); this.label2 = new system.windows.forms.label (); this.label3 = new system.windows.forms.label (); this.txlname = new system.windows.Forms.TextBox (); This.txfname = new system.windows.Forms.TextBox (); this.label4 = new system.windows.forms.label (); this.suspendlayout (); // // label1 // this.label1. Location = new system.drawing.point (32, 112); this.label1.name = "label1"; this.label1.tabindex = 0; this.label1.text = "enter name"; // // txname // THIS.TXNAME.LOCATION = New System.drawing.point (168, 112); this.txname.name = "TXNAME"; this.txname.tabindex = 1; this.txname.text = ""; // BTComputer // this.btcomputer.location = new system.drawing.point (120, 248); this.btcomputer.name = "btcomputer"; this.btcomputer.tabindex = 2 "This.btcomputer.text =" computer "; this.btcomputer.click = new system.EventHandler (this.btcomputer_click; // // label2 // this.label2.location = new system.drawing.point (32, 160); this.label2.name = "label2"; this.label2.tabindex = 3; this.label2.text = "first name"; // // label3 // this.label3.Location = new system.drawing. Point (32, 200); this.label3.name = "label3"; this.label3.tabindex = 4; this.label3.text = "last name"; ////////////////////////////// // TXLName.Location =

New system.drawing.point (168, 200); this.txlname.name = "txlname"; this.txlname.tabindex = 5; this.txlname.text = ""; /// TXFNAME // this.txfname. Location = new system.drawing.point (168, 152); this.txfname.name = "TXFNAME"; this.txfname.tabindex = 6; this.txfname.text = ""; /// Label4 // this. Label4.font = new system.drawing.font ("Song", 20F, System.drawing.FontStyle.Regular, System.drawing.graphicsUnit.Point, ((System.byte) (134))); this.Label4.Location = New system.drawing.point (80, 32); this.label4.name = "label4"; this.label4.size = new system.drawing.size (208, 40); this.label4.tabindex = 7; this .label4.text = "Simple factory example"; // // Form1 // this.autoscalebasesize = new system.drawing.size (6, 14); this.clientsize = new system.drawing.size (368, 307); This.Controls.add (this.label4); this.controls.add (this.txfname); this.controls.add (this.txlname); this.controls.add (this.label3); this.controls.add ( This.label2); this.Controls. Add (THIS.BTComputer); this.controls.add (this.txname); this.controls.add (this.label1); this.name = "form1"; this.text = "form1"; this.Load = New system.eventhandler (this.form1_load); this.ResumeLayout (false);} #ENDREGION / ** ////

// Application main entry point.

/// [STAThread] static void Main () {Application.Run (new Form1 ());} private void Form1_Load (object sender, System.EventArgs e) {} private void btComputer_Click (object sender, System. Eventargs e) {namefactory nf = new namefactory (); NAMER NM = nf.getname (this.txname.text); this.txfname.text = nm.getfname (); this.txlname.text = nm.getlname (); }}} -------------------------- The last is the application of simple factory model. Simple factory model has more than three classes. One is a base class, more than one inheritance class and a factory class, and the last is of course an application class. The simple factory model is implemented with multiple inheritance, it can be said that it is applied to a number of solutions.

转载请注明原文地址:https://www.9cbs.com/read-11028.html

New Post(0)