Simple Factory:
Public class sendfactory {public sender produce (string type) {If ("mail ". equals (type) {return New mailsender ();} else if ("SMS ". equals (type) {return New smssender ();} else {system. out. println ("enter the correct type! "); Return NULL ;}}}
public class SendFactory { public Sender produceMail(){ return new MailSender(); } public Sender produceSms(){ return new SmsSender(); } }
public class SendFactory { public static Sender produceMail(){ return new MailSender(); } public static Sender produceSms(){ return new SmsSender(); } }
Factory method:
Public Enum type {SMS, mail} public abstract class sender {public static string [] typearray = new string [] {"SMS", "mail"}; Public type T; public String sendername;} class mail: sender {public mail () {This. sendername = "Zhang Fei"; this. T = type. mail ;}}// abstract class sendonemessage {abstract protected sender createsender (type T); Public void send (type T) {sender s = createsender (t); If (S! = NULL) {console. write (S. sendername + "send a message" + sender. typearray [(INT) T] + "message");} else {console. write ("wrong") ;}}/// <summary> // factory method (implemented by inheritance) /// </Summary> class sendonemail: sendonemessage {public sendonemail () {} protected override sender createsender (type T) {If (t = type. mail) {return New Mail () ;}else {return NULL ;}}}
static int Main()//string[] args { SendOneMessage a = new SendOneMessage(); a.send(type.Mail); Console.ReadLine(); return 0; }
Abstract Factory:
Public abstract class weapon {public string name; Public void description () {console. write ("I am a" + this. name + "\ n") ;}} class sword1: Weapon {public sword1 () {This. name = "wave blade" ;}} class axe1: Weapon {public axe1 () {This. name = "hand axe" ;}} class sword2: Weapon {public sword2 () {This. name = "Aurora sword" ;}} class axe2: Weapon {public axe2 () {This. name = "Crystal Meteor" ;}}// Abstract Factory interface factory {weapon createsword (); weapon createaxe () ;}class weapon1: factory {public weapon createsword () {return New sword1 ();} public weapon createaxe () {return New axe1 () ;}} class weapon2: factory {public weapon createsword () {return New sword2 ();} public weapon createaxe () {return New axe2 ();}}
static int Main()//string[] args { weapon1 w1 = new weapon1(); weapon2 w2 = new weapon2(); w1.CreateSword().Description(); w1.CreateAxe().Description(); w2.CreateSword().Description(); w2.CreateAxe().Description(); Console.ReadLine(); return 0; }
Abstract Factory + factory Method
Public abstract class equip {public string name; Public void description () {console. write ("I Am A/piece" + this. name + "\ n") ;}} class sword1: equip {public sword1 () {This. name = "wave blade" ;}} class axe1: equip {public axe1 () {This. name = "hand axe" ;}} class sword2: equip {public sword2 () {This. name = "Aurora sword" ;}} class axe2: equip {public axe2 () {This. name = "Crystal Meteor" ;}} class armour1: equip {public armour1 () {This. name = "pi" ;}} class armour2: equip {public armour2 () {This. name = "" ;}} class gaiter1: equip {public gaiter1 () {This. name = "skin leg guard" ;}} class gaiter2: equip {public gaiter2 () {This. name = "platinum leg guard" ;}}// Abstract Factory interface factory {equip createsword (string type); Equip createarmour (string type);} class equip1: factory {public equip createsword (string type) {If (type = "sword") {return New sword1 () ;}else {return New axe1 ();}} public equip createarmour (string type) {If (type = "armour") {return New armour1 () ;}else {return New gaiter1 () ;}} class equip2: factory {public equip createsword (string type) {If (type = "sword") {return New sword2 () ;}else {return New axe2 ();}} public equip createarmour (string type) {If (type = "armour") {return New armour2 () ;}else {return New gaiter2 ();}}} static int main () // string [] ARGs {equip1 e1 = new equip1 (); equip2 e2 = new equip2 (); e1.createsword ("sword "). description (); e1.createsword ("Axe "). description (); e1.createarmour ("armour "). description (); e1.createarmour ("gaiter "). description (); e2.createsword ("sword "). description (); e2.createsword ("Axe "). description (); e2.createarmour ("armour "). description (); e2.createarmour ("gaiter "). description (); console. readline (); Return 0 ;}
1 factory method and 2 Abstract Factory)