簡體   English   中英

限制類方法訪問另一個類

[英]Restricting class method access to one other class

問候。

我有兩個班,'數據庫'和'組'。 我希望能夠在“數據庫”中創建“組”實例並在這些實例上調用方法,並能夠公開分發“組”實例引用。 但是,我不想提供對“組”中的構造函數或其他方法的公共訪問。

我原本以為我可以通過使'Group'成為'Database'的私有內部類來實現這種訪問限制,但我發現如果它是私有的,我不能公開分發對'Group'的引用。 此外,我將'Group'設為公共內部類的嘗試失敗了,因為如果它的方法都是私有的,'Database'無法訪問它們,如果它們是公共的,那么訪問可能超出'Database'。

我正在尋找解決或避免這個問題的最佳實踐技術。 也許我錯過了某個必要的關鍵字? 到目前為止,我在研究中發現的任何內容都表明C#允許這種控制粒度。 我找到了解決問題的混亂方法,正如我在下面的代碼中提供的那樣。 它的本質是這樣的:在“數據庫”中每次調用“組”中的方法之前,在“數據庫”中設置一個字段,可以公開讀取,但只能私下設置,“組”的方法都會檢查它們的創建實例'數據庫'在執行預期的操作之前。 在讀取字段時(通過“數據庫”中的公共方法),字段將被重置,防止對“組”進行任何進一步的方法調用,直到“數據庫”再次設置字段。

public class Database {

    // Field; true if Database has just authorized a method call to a %Group.
    private bool group_isNextCallAuthorized = false;

    // Invoked before every method call to a %Group.
    private void Group_AuthorizeNextCall() {
        group_isNextCallAuthorized = true;
 }

    // Method, ordinarily called from %Group, that checks its creating %Database
    //  that the %Database itself authorized the method call on the %Group. It
    //  automatically resets the authorization to false to prepare for the next,
    //  perhaps unauthorized, method call.
    public bool Group_IsNextCallAuthorized() {
        bool previousValue = group_isNextCallAuthorized;
        group_isNextCallAuthorized = false;
        return previousValue;
    }

    // Constructor; creates a demo %Group.
    public Database() {

        // Create a %Group, first authorizing the action.
        Group_AuthorizeNextCall();
        Group group = Group.Create(this);

        // Call some method on the group
        Group_AuthorizeNextCall();
        group.SomeGroupMethod();

    }

}

public class Group {

    // Field; refers to the %Database that created this %Group instance.
    private Database db;

    // Generates an instance of %Group, requiring the creating %Database as an
    //  argument. After checking that the %Database %db isn't null, it verifies
    //  that %db actually requests and authorized this %Group's creation via the
    //  %Group_IsNextCallAuthorized(.) method provided by %Database.
    public static Group Create(Database db) {

        // It will not create a dud instance of %Group; it will throw an exception
        //  instead.
        if ((db == null) || !db.Group_IsNextCallAuthorized())
            throw new System.Exception("Unauthorized access.");

        return new Group(db);
    }

    // This constructor is referenced internally by static %Create(.) as above.
    private Group(Database db) {
        this.db = db;
    }

    // This is an arbitrary method showing that it must check that the %Database
    //  that created this %Group authorized this method call before it will
    //  perform its intended function.
    public void SomeGroupMethod() {
        if (!db.Group_IsNextCallAuthorized())
            throw new System.Exception("Unauthorized access.");

        // intended functionality...
    }

}

一種選擇是將接口IGroup暴露給代碼的外部部分。 這個接口只有屬性上的getter,你想要訪問的方法等等。然后Database將在Group類上運行,擁有對所有屬性/方法的完全訪問權限,並返回IGroup

您可以使用嵌套類方法。 可能不是自從緊密耦合以來最有資格的人,但會做到這一點。

 public class DataBase 
 {
      private class Group 
      {
            private Group() {} 

      }

      private Group group = null;

      public DataBase() 
      {
         this.group = new Group();
      }

      public Group 
      {
         get 
         {
            return this.group;

         }   
 }

要使用C#訪問規則表達此可見性,您需要反轉類嵌套:使數據庫成為Group中的嵌套類,以便它可以訪問Group的私有位。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM