What should be the class design

If i have WinForm,corresponding to that WinForm ,there is a Business Logic class say Class Server.

class Server does some computation based on the values entered in form controls and needs to pass that information to another class say class Client.

The solution i did is ,I used Mediator Pattern.

class Server
{
  Mediator m = new Mediator();
  m.IValue=txtValue.text;
  Client c =new Client(m);

}

    class Mediator
    {
      private int iValue
      public int IValue{get,set};
    }

    class Client
    {
      Mediator m;
      private int val;
      Client(Mediator m)
      { 
         this.m=m
      }
      val = m.IValue;
    }

Is there any other way to do this?


I think it would be better if just simply decouple the entities.

Suppose, class Information is what Business logic would be working upon and then passing to client.

Proceed as following.

Interface IInformation
{
}

class Info: IInformation
{
}

class Server
{
    ProcessInfo(IInformation infoUserInput)
    {
     //business logic
     .
     .
     //pass to client
     client c = new client(infoUserInput);
    }
}


class client
{
   IInformation revcieveInfo;
   client(IInformation rec)
   {
       revcieveInfo = rec;
   }
}

Whenever you need to return more than a defined type ( int , string , IEnumberable<T> , or whatever) you need to create your own class that defines those fields you need to return to the caller.

If you, for instance, need to return an all of the information about an address to a caller, it you would create an Address class. The caller would invoke the method and, in return, an Address object should be returned.

public class Address
{
    public string Street { get; set; }
    public string Unit { get; set; }
    public string City { get; set; }
    public string Region { get; set; }
    public string PostalCode { get; set; }
}

public Address GetSomeAddress ()
{
    // Perform the logic to either retrieve or create the address
    var result = new Address ();

    // Perform some action on it
    result.Street = "123 No Place";

    // and return it
    return result;
}

Because you have not stated the domain in which you are working, there is not much help anybody can provide outside of that. If you can post more information about your domain (what it is you're attempting to accomplish) then you can get a more detailed answer.

Without more information, too, I cannot see why you are using/worried about the mediator pattern.


Sounds like you might be interested in reading about the Presentation Model design pattern. I think of MVC, PM and MVVM as forms of a mediator pattern and they are all approaches for getting the business logic out of a view.

Also if you're just starting C#, you might consider looking into learning WPF. The MVVM pattern is sort of baked in and there are many good tutorials and examples. I wrote up my expereience on learning WPF and MVVM in a blog entry and it has links you might find helpful. It's a big exaggeration to say I "learned" this in a weekend since it's six months later and I'm still learning it but I do think if you watch the various screencasts you can get a good understanding of the basics in a weekend.

链接地址: http://www.djcxy.com/p/51040.html

上一篇: 最好的OOP方式代表棋子

下一篇: 班级设计应该是什么