2013-05-09 237 views
1

我有Person类,两个子类StaffStudent,接口IPerson。另外我有一个类Database和类GatewayDatabase类有如何执行方法

private string name = "username"; 

和方法

public void getName() {return id;} 

工作人员和学生具有的getName()方法。我需要通过网关创建请求getName()从学生和职员类到数据库。 Class Gateway必须检查方法getName()是否由Staffthen return id)或Studentthen return "Go away!")请求。 任何人都可以请帮助我。我正在考虑使用Gateway作为Database类的接口,但是因为我只是试图学习C#,所以我不知道该怎么做。或者,也许有这样做的更好的方法...请帮助 感谢

下面是一些代码:

public class Staff : Person 
    { 


    public Staff() {} 
    public Staff(string id): base(id) {} 
    public override string getName() 
    { 
     throw new NotImplementedException(); 
    } 
    public override void Update(object o) 
    { 
     Console.WriteLine(id + " notified that {1}", id, o.ToString()); 
    } 
    public override void UpdateMessage(object p) 
    { 
     Console.WriteLine(id + " notified about new message in chat: {1}", id, p.ToString()); 
    } 
    } 

public class Student : Person 
{ 
    public Student() {} 
    public Student(string id): base(id) {} 
    public override string getName() 
    { 
     throw new NotImplementedException(); 
    } 
    public override void Update(object o) 
    { 
    Console.WriteLine(id +" notified that {1}", id, o.ToString()); 
    } 
    public override void UpdateMessage(object p) 
    { 
    Console.WriteLine("Message for " + id + " {1}", id, p.ToString()); 
    } 
} 

public abstract class Person : IPerson 
{ 
    public string id; 
    public Person() { } 
    public abstract string getName(); 
    public Person(string i) { this.id = i; } 
    public abstract void Update(Object o); 
    public abstract void UpdateMessage(Object p); 
} 

public interface IPerson  
{ 
    void Update(Object o); 
    void UpdateMessage(Object p); 
    string getName(); 
} 

class database 
{ 
    public string username = "username"; 
    private string name = "user details"; 
    private string grade = "user grade"; 

    public string getName(Object o) 
    { 
     if (o is Staff) { return name; } 
     else { return "Go away!"; } 
    } 
    public string getgrade() { return grade; } 
} 


public class Gateway 
    { 
    public void DoSomethingWithPerson(IPerson person) 
    { 
     string iD = person.getName(); 
     if (person is Student) 
     { 
      return "go away!"; 
     } 
     else if (person is Staff) 
     { 
      return name; 
     } 
    } 
} 

回答

2

这是一个有点令人费解的问题。所以,首先,我想指出你的C#的一些样式问题。

  • database您的database类是小写,而其余的是一致的。有些方法不一致(例如,对某些方法使用惯用的PascalCase,对其他方法使用驼峰或小写)。
  • IPerson实际上在这里没有用处,因为您可以将StaffStudent的实例作为Person左右传递,并以与现在基本相同的方式使用所有内容。在大多数情况下,你会想要选择一个接口或抽象基类,而不是两者。
  • C#有一个“属性”的概念,这对于getter和setters来说基本上是一个方便的语法。这是公用字段的优先选择(如中的database类或public string id中的Person),因为它允许您将后台字段的实现保持为专用字段。如果你只是想要一个默认的实现,这个语法是public string username { get; set; }。您可以将其扩展到更复杂的事情。例如,也许你想确保修改用户名。 (1)
  • 使用小号挑选,但通常使用object,使用小写字母o。
  • 您实际上不必在字符串格式插值的对象上调用.ToString()。(2)

(1)

private string m_username; 
public string username { 
    get { return m_username; } 
    set { m_username = (value != null ? value.Trim() : value); } 
} 

(2)这些行是相等的。

Console.WriteLine(id + " notified that {1}", id, o.ToString()); 
Console.WriteLine("{0} notified that {1}", id, o); 

现在解决问题。对我来说,这听起来像你想要不同类别的不同行为。用它的措辞,这听起来像访问/权限问题。根据您的数据存储如何设置(在这种情况下,它看起来像在代码中的常量,但你可以很容易地做一些查询的),你可以不喜欢......

[Flags] 
public enum Permission { 
    None = 0, 
    GetName = 1 
} 

public abstract class Person { 
    /* ... */ 
    public abstract Permission Permissions { get; } 
} 

public class Staff : Person { 
    /* ... */ 
    public override Permission Permissions { 
     get { return Permission.GetName; } 
    } 
} 

public class Student : Person { 
    /* ... */ 
    public override Permission Permissions { 
     get { return Permission.None; } 
    } 
} 

public class Database { 
    /* ... */ 
    private Dictionary<string, string> NamesDatabase { get; set; } 
    public string getName(string id) { 
     // As a consequence of being managed by Gateway, assume that the caller has access 
     return NamesDatabase[id]; 
    } 
} 

public class Gateway { 
    public string DoSomethingWithPerson(Person person, string desiredNamePersonId) { 
     if (person.Permissions.HasFlag(Permission.GetName)) { 
      Database db = new Database(); 
      return db.getName(desiredNamePersonId); 
     } 
     return "go away!"; 
    } 
} 

假如我们具有用于Database一个构造为这样:

public Database() { 
     NamesDatabase = new Dictionary<string, string>(2); 
     NamesDatabase["id1"] = "Student Amy"; 
     NamesDatabase["id2"] = "Staff Mary"; 
    } 

而一个Main这样:

static void Main() { 
     Gateway gate = new Gateway(); 
     Console.WriteLine("Student id1 looks up Staff id2: {0}", gate.DoSomethingWithPerson(new Student("id1"), "id2")); 
     Console.WriteLine("Staff id2 looks up Student id1: {0}", gate.DoSomethingWithPerson(new Staff("id2"), "id1")); 

     Console.ReadLine(); 
    } 

的输出是:

Student id1 looks up Staff id2: go away! 
Staff id2 looks up Student id1: Student Amy 

如果有任何部分不清楚,或者我的评估方式偏离标准,请随时澄清问题。

0

我不确定这是否是您需要的。

static void Main(string[] args) 
    { 
     var gateway = new Gateway(); 
     Console.WriteLine(gateway.DoSomethingWithPerson(new Staff(1))); 
     Console.WriteLine(gateway.DoSomethingWithPerson(new Student(1))); 
    } 

    public class Staff : Person 
    { 
     public Staff() { } 
     public Staff(int id) : base(id) { } 
     public override void Update(object o) 
     { 
      Console.WriteLine(ID + " notified that {1}", ID, o); 
     } 
     public override void UpdateMessage(object p) 
     { 
      Console.WriteLine(ID + " notified about new message in chat: {1}", ID, p); 
     } 
     public override string GetName() 
     { 
      return DataBase.GetName(ID); 
     } 
    } 

    public class Student : Person 
    { 
     public Student() { } 
     public Student(int id) : base(id) { } 
     public override void Update(object o) 
     { 
      Console.WriteLine(ID + " notified that {1}", ID, o); 
     } 
     public override void UpdateMessage(object p) 
     { 
      Console.WriteLine("Message for " + ID + " {1}", ID, p); 
     } 

     public override string GetName() 
     { 
      return "Go Away!"; 
     } 
    } 

    public abstract class Person : IPerson 
    { 
     public int ID; 
     protected Person() { DataBase = new DataBase(); } 

     public abstract string GetName(); 

     protected Person(int i) { ID = i; DataBase = new DataBase(); } 
     public abstract void Update(Object o); 
     public abstract void UpdateMessage(Object p); 

     public DataBase DataBase { get; set; } 
    } 

    public interface IPerson 
    { 
     void Update(Object o); 
     void UpdateMessage(Object p); 
     string GetName(); 
    } 

    public class DataBase 
    { 
     public string USERNAME = "username"; 
     private const string Name = "user details"; 
     private const string Grade = "user grade"; 

     public string GetName(int id) 
     { 
      // you should perform get something. 
      return Name; 
     } 

     public string GetGrade() { return Grade; } 
    } 

    //maybe call it facade 
    public class Gateway 
    { 
     public string DoSomethingWithPerson(IPerson person) 
     { 
      return person.GetName(); 
     } 
    }