是否打破了Facade设计模式
在 HomeCADEngine 外观类中,我有一个方法“addRoom(room:Room)”,它将将此房间添加到 ArrayList 中。但是,当我们在 Facade 类外部创建一个房间,然后将其传递给 addRom() 方法时,是否会破坏 Facade 模式?
非常感谢
In HomeCADEngine facade class I have a method "addRoom(room:Room)" and it will add this room to a ArrayList. But is it break facade pattern when we create a room outside facade class and then pass it into addRom() method??
Many thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这取决于情况,这些模式只是改进代码的指南。在这种情况下,如果您在 Facade 内部创建 Room 会更准确,并且您无法从除 Facade 类之外的任何其他类访问 Room 类,即使是创建 Room 的新实例也是如此。
重要的是,外观模式的目的是封装对其他类的所有访问,并且对它们的每次访问都必须通过外观类。
It depends, the patterns are only guidelines to improve your code. In this case, it would be more accurate if you create the Room inside the facade and you cannot access to the Room class from any other class except from the facade class, even for creating a new instance of Room.
The important thing is that the purpose of the facade pattern is to encapsulate all the acceses to other/s classes and every access to them must be through the facade class.
设计模式仅被视为在某些情况下如何改进设计的示例。您永远不应该按原样使用设计模式。你必须始终思考,在你的具体情况下什么对你真正有帮助。
像门面这样的设计模式之所以如此常见,是因为在实践中观察到,门面确实有用的情况发生得相对频繁。因此,将其作为一种常见模式进行说明是有意义的,以便每个人在定义自己的设计时都能获得某种灵感。
这意味着,让你的设计变得优秀的并不是外观。门面并不是官方给出的好的解决方案。什么是好的设计仅取决于您的情况。您可以选择非常接近立面的设计。然后你甚至可以说它是门面,因为这是每个人都理解的,或者你说它“像门面”或者你对它的命名完全不同。
让我们看看我的意思:
例如,假设您有一个 CadObject 类。你想,你想要为它建立一个外观。你认为,外观是官方的东西,你应该以官方的方式实现。因此,您将外观称为 CadObjectFacade。不幸的是,你走错了路。因为 CadObjectFacade 这个名称并不合适。立面是建筑物的正面。为什么你将班级命名为建筑物的正面?它使用外观设计模式的事实只是一个实现细节。您应该根据类提供的功能来命名您的类,而不是根据实现细节。例如,如果您的外观可以轻松地处理 CadObject 的几何形状,那么您的外观最好命名为 CadObjectGeometry。或者,如果您的外观是处理 CadObject 材质或价格的舒适方式,那么您的外观最好命名为 CadObjectMaterials 或 CadOjectPricing。
你可能会说这些名称并不能帮助代码的读者理解设计。读者更应该看到它是一个外观。这种担心是错误的。如果外观是一个合适的解决方案,那么使用外观也必须是直观的(不直观的东西就不可能合适)。但如果外观直观且自然,则不必明确将其命名为外观。您也不要将您的类命名为 CadObjectClass、PositionClass、RectangleClass。就像必须直观地清楚知道 CadObject、Position、Rectangle 是类一样,使用外观或其他设计模式也必须直观地了解。而如果在门面之外做一些事情是直观的,并且耦合性和内聚性都可以,那么这样的设计也是不错的。
你看,当使用立面设计时,没有对错之分。这取决于你的情况。如果您问是否可以在立面之外创建一个房间,那么您还应该问自己为什么这会成为一个问题?对于外观,您通常打算简化一些事情。如果您认为想要简化房间的创建,请将其包含到立面中。如果没有那就不是。但这个问题取决于你认为什么使使用变得简单以及你认为什么应该是抽象的。您还可以使用另一个用于创建房间的立面、包装器或策略。
The design patterns are considered only as examples how in certain situations the design can be improved. You should never use the design patterns as they are. You always have to think, what exactly helps you in your specific situation.
The reason why the design patterns like facade are so common is that it is observered in practice that the situation in which exactly a facade is helpful, occurs relatively often. So it makes sense to to illustrate it as a common pattern, so that everybody has some sort of inspiration when defining an own design.
That means, it is not the facade that makes your design good. The facade is not an official good solution. What a good design is depends only on your situation. You may pick a design that is very close to a facade. Then you could even say it is facade, because that is what everybody understands, or you say it is "like a facade" or you name it completely different.
Let us see what I mean:
Consider for example you have a class CadObject. You think, you want to have a facade for it. And you think, a facade is something official that you should implement in an official way. So you call your facade CadObjectFacade. Unfortunately then you are on a wrong path. Because the name CadObjectFacade is not appropriate. A facade is the front of a building. Why do you name your class like the front of a building? The fact that it uses a facade design pattern is just an implementation detail. You should name your classes according to the functions they are providing but not according to implementation details. If your facade for example is a comfortable way of working with the geometry of a CadObject, then your facade should better be named CadObjectGeometry. Or if your facade is a comfortable way of working with the materials of a CadObject or the price, your facade should better be named CadObjectMaterials or CadOjectPricing.
You may say that these names do not help the reader of the code to understand the design. The reader should rather see that it is a facade. That concern is wrong. If a facade is an appropriate solution, then it must be also intuitive to use a facade (something not intuitive cannot be appropriate). But if a facade is intuitive and natural, you don't have to explicitly name it a facade. You does also do not name your classes CadObjectClass, PositionClass, RectangleClass. The way how it must be intuitively clear that CadObject, Position, Rectangle are classes, it must also be intuitive that you use a facade or another design pattern. And if it is intuitive to do something outside the facade and the coupling and the cohesion are ok, then such design is also good.
You see, when using a facade design, there is no right and wrong. It depends on your situation. If you ask if you can create a room outside your facade, then you should also ask yourself why this should be a problem? With a facade you typically intend to simplify some things. If you think you want to simplify the room creation, then include it to the facade. If not then not. But this question depends on what you think makes the usage easy and what you think should be abstract. You can also have another facade for the room creation, or a wrapper or a strategy.