全局“类对象”或全局“服务器对象”

时间:2012-09-21 13:58:50

标签: c#

我正在做一个包含自定义OPC客户端的项目。 Class Main表示WPF应用程序中的MainWindow。 私有字段_opcServer将保留一个对象以供进一步使用。 任何时候只允许一个_opcServer个对象。 我想出了这个(这是所有示例代码并且工作正常)

// "Main" Class --> it's a WPF Window
public class Main
{
// the "global" server object
private OpcServer _opcServer = new OpcServer();

public Main() {}

private void connectOpcServer()
{
    if(this._opcServer == null)
    {
        // the "global" server object
        this._opcServer = this.opcClientFactory().connectOpcServer("someOpcServer");

        if(this._opcServer != null)
        {
            // we made the connection
        }
        else
        {
            // connection failed
        }           
    }
}

private void disconnectOpcServer()
{
    if(this._opcServer != null)
    {           
        if(this.opcClientFactory().disconnectOpcServer(this._opcServer))
        {
            // disconnected
            this._opcServer = null;
        }
        else
        {
            // something went wrong
        }
    }
}

private OpcClient ocpClientFactory()
{
    OpcClient opcClient = new opcClient();
    return opcClient;
}
   }

// Client Class
public class OpcClient     
{
// the server object
private OpcServer _opcServer = new OpcServer(); 

public OpcClient() {}

public OpcServer connectOpcServer(string progID)
{
    bool madeConnection = this._opcServer.Connect(progID);

    if(madeConnection)
    {
        return this._opcServer;
    }
    else
    {
        return null;
    }
}

public bool disconnectOpcServer(OpcServer opcServer)
{
    this._opcServer = opcServer;

    if(this._opcServer.disconnect())
    {
        this._opcServer = null;
        return true;
    }       
    return false;
}       
  }

代码中的评论不多,但我认为你明白了。 每次通过用户操作触发连接或断开连接时,都会创建OPC客户端的新对象,并在一个或另一个方向上传递服务器对象。 会有更多的方法(比如读取标签等),但由于用户每天只应使用一次或两次,我认为创建新对象并在它们之间传递内容没有问题。

但是,如果有一个真正有趣的用户认为他必须一直使用这些东西(连接/断开连接等),该怎么办呢?然后我将最终创建许多对象!

我考虑了一下,想出了这个。

public class Main
{
// the client object
private OpcClient _opcClient = OpcClient.Instance;

public Main(){}

private void connectOpcServer()
{
    if(this._opcClient.connectOpcServer("someOpcServer"))
    {
        // we made the connection and can now use 
        // this._opcClient.opcServer
    }
    else
    {
        // connection failed
    }
}

private void disconnectOpcServer()
{
    if(this._opcClient.disconnect())
    {
        // disconnected
    }
    else
    {
        // something went wrong
    }
}
 }

public class OpcClient
{
private static OpcClient _instance;

public static OpcClient Instance
{
    get
    {
        if(instance == null)
        {
            _instance = new OpcClient();
        }           
        return _instance;
    }
}

private OpcClient()
{
    this.opcServer = new OpcServer();
}

public OpcServer opcServer
{
    get;
    private set;
}

public bool connectOpcServer(string progID)
{   
    return this.opcServer.Connect(progID);
}

public bool disconnectOpcServer()
{
    return this.opcServer.disconnect();
}

 }

现在我创建了一个OPC客户端的singelton并将其传递给主类。现在只创建一个对象,用户可以整天点击连接/断开连接。

这里最好的方法是什么?

  1. 将服务器对象存储在主类
  2. 将类对象存储在主类
  3. 取决于
  4. 两者都是坏主意(如果是,为什么?我能做什么呢?)

1 个答案:

答案 0 :(得分:0)

我选择第二个选项。 通过选择单例方法,我可以确保只有一个服务器对象。 这非常重要。

相关问题