[Design mode]. 01. Ticket mode, design
- Using the volume Interface
1 // define an operation that should be performed by users 2 public interface IUser3 {4 bool Login (string userName, string psw); 5 bool Login (); 6 bool Exsit (); 7}
- Abstract class with category
1 // define an abstract class so that it can implement the IUser interface. 2 // specify the operations that need to be performed on each User class, for example, if the login information is logged to the log, the modifier is set to repeat virtual 3 public abstract class User: IUser 4 {5 public string UserName {get; set ;} 6 public string Psw {get; set;} 7 public User () 8 {} 9 public User (string userName, string psw) 10 {11 this. userName = userName; 12 this. psw = psw; 13} 14 public virtual bool Login (string userName, string psw) 15 {16 Console. writeLine ("operate Login, remember logs"); 17 return true; 18} 19 public virtual bool Login () 20 {21 Console. writeLine ("operate Login, remember logs"); 22 return true; 23} 24 public virtual bool Exsit () 25 {26 Console. writeLine ("Operation Exsit, remember logs"); 27 return true; 28} 29}
1 // inherit Admin type from User, Admin type has its own unique operations, re-commit operations in parent class, operation 2 public class Admin: User 3 {4 public Admin () 5: base () 6 {7 8} 9 public Admin (string userName, string psw) 10: base (userName, psw) 11 {12 13} 14 public override bool Login () 15 {16 if (base. login () 17 {18 Console. writeLine ("Operation Admin. login "); 19 return true; 20} 21 return false; 22} 23 public override bool Login (string userName, string psw) 24 {25 if (base. login (userName, psw) 26 {27 Console. writeLine ("Operation Admin. login "); 28 return true; 29} 30 return false; 31} 32 public override bool Exsit () 33 {34 if (base. exsit () 35 {36 Console. writeLine ("Operation Admin. exsit "); 37 return true; 38} 39 return false; 40} 41}
1 public class Visitor: User 2 {3 // identifies the Visitor type from the User. The Visitor type has its own unique operations, which repeat the operations in the parent class, operation 4 public Visitor () 5: base () 6 {7 8} 9 public Visitor (string userName, string psw) 10: base (userName, psw) 11 {12 13} 14 public override bool Login () 15 {16 if (base. login () 17 {18 Console. writeLine ("Operation Visitor. login "); 19 return true; 20} 21 return false; 22} 23 public override bool Login (string userName, string psw) 24 {25 if (base. login (userName, psw) 26 {27 Console. writeLine ("Operation Visitor. login "); 28 return true; 29} 30 return false; 31} 32 public override bool Exsit () 33 {34 if (base. exsit () 35 {36 Console. writeLine ("Operation Visitor. exsit "); 37 return true; 38} 39 return false; 40} 41}
- Implement the example of UserFactory using metadata
1 // The User of zookeeper is self-contained, and the root zookeeper information is input to generate the corresponding example, each example has the semantics defined in User and method 2 public static class UserFactory 3 {4 public enum UserType 5 {6 Admin, 7 Visitor 8} 9 public static User CreateUser (UserType ut) 10 {11 User user = null; 12 switch (ut) 13 {14 case UserType. admin: 15 user = new Admin (); 16 break; 17 case UserType. visitor: 18 user = new Visitor (); 19 break; 20} 21 return user; 22} 23 public static User CreateUser (UserType ut, string userName, string psw) 24 {25 User user = null; 26 switch (ut) 27 {28 case UserType. admin: 29 user = new Admin (userName, psw); 30 break; 31 case UserType. visitor: 32 user = new Visitor (userName, psw); 33 break; 34} 35 return user; 36} 37}
In the IUser interface, define a method that should be used, and then create an abstract class to implement this interface, define the actual method in the abstract class as a reusable method, and create two new class types to inherit the User, in this case, the two new types must use the parent class method and then use their own method.
When using real-life example UserFactory, the combined real-life object is returned when the real-life example uses the real-life example class.