没有公开公开属性设置器的实现接口?

问题描述 投票:0回答:1

下面的示例代码可以更好地解释我的问题,在注释中可以很好地解释它。 Browser包含BrowserModule的各种实现程序,这些模块引用所有者浏览器,因此它们可以在内部一起工作。每个“主”模块内部都有一个SpecificModule,这些“主”模块仅充当特定模块的代理,这些特定模块将实现目标浏览器自动化库,例如Selenium或Puppeteer等。

例如,对于BrowserMouse模块,它会将SpecificModule实例化为BrowserPuppeteerChromiumSpecificMouse,并将调用委派给该特定模块。

我不希望Browser类的用户访问每个自动化库的基础实现,所以我将其隐藏起来,但是例如,通过设置< [Browser.Mouse.PositionOnDocument = new Point(123,123),该功能我希望仅在内部(命名空间级别)可用,而对于库的最终用户不可用。

例如,应该允许另一个模块更改鼠标的位置,通常可以通过访问

Browser.Mouse.PositionOnDocument = new Point();

来更改它的位置] >>但是应该只允许来自相同的名称空间,不允许来自外部使用,外部使用应该是只读的。

using System; using System.Drawing; namespace Test { using InternalNamespace; class Program { public static void Main() { Browser browser = new Browser(); browser.Initialize(); Console.WriteLine(browser.Mouse.PositionOnDocument); // This should NOT be allowed because its being used from outside the Browser namespace browser.Mouse.PositionOnDocument = new Point(2, 2); Console.WriteLine(browser.Mouse.PositionOnDocument); } } } namespace InternalNamespace { public class Browser { public BrowserMouse Mouse; public void Initialize() { // Instantiate the main mouse with underlying specific puppeteer module // pass a null module to the specific module because it doesnt need // one underlying specific module Mouse = new BrowserMouse(this, new BrowserPuppeteerChromiumSpecificMouse(this, null)); // This is OK because its inside the namespace Mouse.PositionOnDocument = new Point(1, 1); } } public class BrowserModule { // ExternalBrowser that owns this module. protected Browser Browser { get; set; } // Underlying specific module => Puppeteer, Selenium or anything. protected BrowserModule SpecificModule { get; set; } public BrowserModule(Browser browser, BrowserModule specificModule) { Browser = browser; if (specificModule != null) SpecificModule = specificModule; } } interface ILocatable { Point PositionOnDocument { get; set; } } // This whole class just servers as a proxy for the underlying module. public class BrowserMouse : BrowserModule, ILocatable { public BrowserMouse(Browser browser, BrowserModule specificModule) : base(browser, specificModule) { } // This delegates the call to the specific module. public Point PositionOnDocument { get { return ((ILocatable)SpecificModule).PositionOnDocument; } set { ((ILocatable)SpecificModule).PositionOnDocument = value; } } } // The specific module implementation that is gonna be called from the main module. public class BrowserPuppeteerChromiumSpecificMouse : BrowserModule, ILocatable { public BrowserPuppeteerChromiumSpecificMouse(Browser browser, BrowserModule specificModule) : base(browser, specificModule) { } public Point PositionOnDocument { get; set; } } }

下面的示例代码可以更好地解释我的问题,在注释中可以很好地解释它。浏览器包含BrowserModule的各种实现程序,这些模块对所有者浏览器都有引用,因此它们...
c# module interface polymorphism public
1个回答
1
投票
此接口允许实现者公开PositionOnDocument
© www.soinside.com 2019 - 2024. All rights reserved.