0

カスタム OPC クライアントを含むプロジェクトを行っています。Class Main は、WPF アプリケーションの MainWindow を表します。private フィールド_opcServerには、後で使用するオブジェクトが保持されます。_opcServer一度に許可されるオブジェクトは1 つだけです。私はこれを思いつきました(それはすべてサンプルコードであり、正常に動作します)

// "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 クライアントの新しいオブジェクトが作成され、サーバー オブジェクトがいずれかの方向に渡されます。このようなメソッド (読み取りタグなど) はもっとありますが、ユーザーはそれらを 1 日に 1 回か 2 回だけ使用する必要があるため、新しいオブジェクトを作成してそれらの間で何かを渡すことに問題はないと思います。

しかし、これらのもの (接続/切断/など) を常に使用する必要があると考えている本当に面白いユーザーがいる場合はどうなるでしょうか。それから私は多くのオブジェクトを作成することになります!

ふと思いついてこれにたどり着きました。

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 クライアントのシングルトンを作成し、それをメイン クラスに渡します。これでオブジェクトが 1 つだけ作成され、ユーザーは終日 [接続] または [切断] をクリックできます。

ここで進める最善の方法は何ですか?

  1. サーバー オブジェクトをメイン クラスに格納する
  2. クラス オブジェクトをメイン クラスに格納する
  3. 依存する
  4. どちらも悪い考えです (もしそうなら、なぜですか? 代わりに何ができますか?)
4

1 に答える 1

0

私は2番目のオプションを選択しています。シングルトン アプローチを選択することで、サーバー オブジェクトを 1 つだけにすることができます。これはとても重要です。

于 2012-09-22T07:47:35.573 に答える