简体   繁体   English

门面设计模式用法

[英]Facade design pattern usage

In our application (C#), the facade is used as the core's API - the facade will be used by the App itself to do stuff with the core. 在我们的应用程序(C#)中,外观被用作核心的API - 应用程序本身将使用外观来处理核心内容。 Knowing that, here are my questions: 知道了,这是我的问题:

  1. Assuming one of my core objects, which the facade wraps, has a recursive bit in it? 假设我的一个核心对象(正面包裹)中有一个递归位? For example, the facade provides a 'GetX' from a tree, and each node needs to GetX from its subtree. 例如,Facade从树中提供“GetX”,每个节点都需要从其子树中获取GetX。 Should this node use the facade's 'GetX'? 这个节点应该使用Facade的'GetX'吗?
  2. Should the facade expose core objects to the application? 外观应该将核心对象暴露给应用程序吗? Eg, the user wants to build a tree, add nodes, print the tree, calculate things on it etc. Should the application use the tree object or should it ask the facade to create it, save it, pring it etc.? 例如,用户想要构建一个树,添加节点,打印树,计算它上面的东西等。如果应用程序使用树对象或应该要求立面创建它,保存它,pring it等?

Thanks. 谢谢。

A facade is an object that provides a simplified interface to a larger body of code. 外观是一个对象,它为更大的代码体提供了简化的界面。

Therefore, keep it simple. 因此,保持简单。

  1. No. Encapsulate the Facade within itself. 否。将Facade封装在自身内部。 I assume you have a private implementation for retrieving the tree, use this internally, exposing only the public method which returns the populated object (see point 2). 我假设您有一个私有实现来检索树,在内部使用它,只暴露返回填充对象的公共方法(参见第2点)。

  2. No. The facade should do everything, otherwise there is little point creating it. 不,外立面应该做所有事情,否则创造它就没什么意义了。 You might want to create a DTO that can be used within the facade methods, but you should not expose the core objects. 您可能希望创建可在facade方法中使用的DTO,但不应公开核心对象。

Concerning question 1: As others have said; 关于问题1:正如其他人所说; No. Instead, consider using a separate interface for this operation (the interface segregation principle may be applicable here?), and access the operations on that interface via the Facade first. 相反,请考虑为此操作使用单独的接口( 接口隔离原则可能适用于此处?),并首先通过Facade访问该接口上的操作。 Upon recursion, the inner interface can be used again directly. 在递归时,可以直接再次使用内部接口。 This might provide some separation of concerns, while also making it easier to change the implementation later if necessary. 这可能会提供一些关注点分离,同时还可以在必要时更轻松地更改实现。

It sounds to me as if the recursion itself is an implementation-detail, and not something the facade needs to know anything about. 听起来好像递归本身就是一个实现细节,而不是外观需要了解的东西。 Similarly, the facade is something that the implementing algorithm does not need to know about (ie, dont recur via the facade). 类似地,外观是实现算法不需要知道的东西(即,不通过外观重复)。

Concerning question 2: How about defining interfaces for this as well? 关于问题2:如何为此定义接口? Eg ITree , ITreeNode , etc, and include operations on the Facade to work with these. 例如ITreeITreeNode等,并在Facade上包含操作以使用这些操作。 Now make the implementation implement these interfaces, thus providing the objects needed outside the facade without exposing the core objects. 现在让实现实现这些接口,从而提供外观所需的对象而不暴露核心对象。

A facade usually wraps your core and give access to a specific set of function. 外观通常包装您的核心并提供对特定功能集的访问。

  1. you might want to do that for consistency but if there is another easier way that preserve the abstraction provided by the facade it would be better. 您可能希望这样做是为了保持一致性,但如果有另一种更简单的方法来保留外观提供的抽象,那就更好了。

  2. No none of your core code should be exposed ( that would defy the purpose of the facade there) If you want to add some functionality, then add a function to your facade that will call the core functions inside your facade. 不会暴露你的核心代码(这会违反那里的外观的目的)如果你想添加一些功能,那么在你的外观中添加一个函数来调用你的外观中的核心功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM