简体   繁体   English

在这种情况下如何应用oo设计模式?

[英]How can I apply oo design patterns in this situation?

Situation: Suppose we're designing the UI of Windows 9 using Java API. 情况:假设我们正在使用Java API设计Windows 9的UI。 We need to build up 3 classes main , BuildInWindow and ApplicationWindow . 我们需要建立3个main类, BuildInWindowApplicationWindow

main - the window for rendering the system UI (ie the start botton & wallpaper page) main -窗口渲染系统UI(即启动钮与壁纸页)

BuildInWindow - windows for rendering buildt-in apps (eg IE) BuildInWindow用于渲染内置应用程序(例如IE)的窗口

ApplicationWindow - windows for rendering apps from third party (eg eclipse) ApplicationWindow用于从第三方渲染应用ApplicationWindow窗口(例如eclipse)

all of them have to implement 3 Java API interfaces, WindowFocusListener , WindowListener and WindowStateListener and have the methods onExit() and onCrushing() . 它们都必须实现3个Java API接口,即WindowFocusListenerWindowListenerWindowStateListener并具有方法onExit()onCrushing()

onExit() performs when the system/built-in app/ third-party app is shut down normally onExit()在系统/内置应用程序/第三方应用程序正常关闭时执行

onCrushing() captures any system/application crush and send system state back to server onCrushing()捕获任何系统/应用程序onCrushing()并将系统状态发送回服务器

This is the original design: 这是原始设计:

http://i.stack.imgur.com/JAJiY.png http://i.stack.imgur.com/JAJiY.png

I have some ideas of how to design it in a OO manner, but I am not sure if that's the right way. 我对如何以面向对象的方式设计它有一些想法,但是我不确定这是否是正确的方法。 Here's my thoughts: 这是我的想法:

  1. Create an abstract class with method onExit() and onCrushing() . 使用onExit()onCrushing()方法创建一个abstract class Since the code of onExit() would vary from 3 classes, it should be an abstract method & onCrushing() would be same fo all classes, so it would be an concrete method 由于onExit()的代码将与3个类不同,因此它应该是一个abstract method ,而onCrushing()所有类都是相同的,因此它将是一个具体方法
  2. tHE MAIN WINdow should use singleton design to ensure user only create one instance of main . MAIN窗口应使用singleton设计以确保用户仅创建一个main实例。
  3. Use the facade design to save the trouble of implementing 3 interfaces to three classes 使用facade设计可以节省将3个接口实现为三个类的麻烦

My question is I don't really understand facade design, so I am not sure if it can be applied in this case. 我的问题是我不太了解外观设计,因此不确定在这种情况下是否可以应用。 Also I am not really sure if onExit() would be different for 3 classes and onCrushing() would perform the same function. 我也不太确定onExit()对于3个类是否不同,并且onCrushing()是否执行相同的功能。

I tried my best to explain the question clearly...if you don't understand free free to comment. 我尽力清楚地解释了这个问题...如果您不了解免费发表评论的话。 Thank you very much! 非常感谢你!

I've left some questions in a comment linked to your question but here's some guidance for you: 我在与您的问题相关的评论中留下了一些问题,但这是为您提供的一些指导:

  1. You shouldn't create an abstract class on the basis of both BuildInwindow and ApplicationWindow both having to have methods #onExit and #onCrushing if they are not to share any implementation. 你不应该两者的基础上创建一个抽象类BuildInwindowApplicationWindow都必须有方法#onExit#onCrushing如果他们不共享任何实现。 Abstract classes are most useful where there is a common implementation. 在有通用实现的地方,抽象类最有用。 An interface containing these methods would be sufficient. 包含这些方法的接口就足够了。 That said, your two windows may share other functionality and, if so, it could be shared through a common superclass (abstract if it relies on subclass implementation detail). 就是说,您的两个窗口可能共享其他功能,如果这样,则可以通过一个公共的超类(如果它依赖于子类实现的详细信息,则是抽象的)来共享。 You may find the Template Method pattern useful for managing the overall window mechanism with specific tailoring for different window types. 您可能会发现模板方法模式对于管理整个窗口机制非常有用,它可以针对不同的窗口类型进行特定的调整。 You may also find the Factory Method means of instance creation (for your window classes) will help separate the object creation and set-up from the creation mechanism. 您可能还会发现实例方法工厂方法 (针对您的窗口类)将有助于将对象的创建和设置与创建机制区分开。

  2. A single shared instance would seem sensible and a singleton would serve this purpose (so long as you're able to handle termination, etc). 单个共享实例似乎很明智,而单个实例可以满足此目的(只要您能够处理终止等)。 Alternatively, your application may just launch a single Main instance - you may even just hide the constructor through package access to ensure no others are created. 另外,您的应用程序可以只启动一个Main实例-您甚至可以通过程序包访问隐藏构造函数,以确保不会创建其他任何实例。

  3. The facade pattern just serves to simplify a complex interface. 外观模式仅用于简化复杂的界面。 It mainly does this by rolling calls to collaborating instances together under a single (coarser) interface. 它主要是通过在单个(粗化器)界面下将对协作实例的调用滚动在一起来实现的。 This wouldn't normally be a done to hide which interfaces a class supports. 隐藏一个类支持的接口通常是不可行的。 Indeed, publishing which interfaces a class extends is important to API users. 实际上,发布类扩展的接口对API用户很重要。 You could roll the three interfaces into a single interface for "convenience" but I think this is unnecessary. 您可以将这三个接口合并为一个“方便”接口,但是我认为这是不必要的。 If you do settle on a common superclass then that would "extend" the three interfaces (if all subclasses were expected to support them). 如果您确实选择了一个公共的超类,那么它将“扩展”这三个接口(如果希望所有子类都支持它们)。 It may also implement some default implementation of these interfaces (again, watch access modifiers to ensure those you intend to be can be overridden while others may be final). 它还可以实现这些接口的某些默认实现(同样,请监视访问修饰符以确保您想要的对象可以被覆盖,而其他修饰符可以是最终的)。

Edit: Guidance 编辑:指导

You just have to identify the classes and relationships: I suggest you just grab some paper and draw. 您只需要确定类和关系:我建议您拿些纸画一下。 You already have your nouns and verbs (you can otherwise go noun and verb spotting to identify classes and methods on them). 您已经有了名词和动词(否则,可以通过名词和动词查找来识别它们上的类和方法)。

So, why not draw a simple diagram containing all the info ( A , B , C , Main , etc) and draw the relationships between them. 因此,为什么不绘制一个包含所有信息( ABCMain等)的简单图并绘制它们之间的关系。 This is your start point. 这是您的起点。 You may have some confusion when working out how Main links to the window classes (given there are two kinds). 在确定Main如何链接到窗口类时(可能有两种),您可能会有些困惑。 Just write a note on it and move on to clarify the rest of the picture. 只需在上面写一个便笺,然后继续进行操作,以澄清其余图片。

Next, refine your diagram to start moving common features into a single place (abstraction). 接下来,优化图表以开始将通用功能移动到一个地方(抽象)。 You know this exists with regards to your interfaces and the methods you suggest but you may need to decide which (if any) have any common functionality. 您知道这与您的界面和建议的方法有关,但是您可能需要确定哪些(如果有)具有任何通用功能。 Then decide if interfaces satisfies your needs (methods are common but implementations are different) or if the implementation itself is the same and so a parent superclass may be useful (this addresses abstraction [who is responsible for what], encapsulation [individual implementations at the appropriate level] and polymorphism [which classes support common methods]). 然后确定接口是否满足您的需求(方法是通用的,但实现方式是不同的),或者实现本身是相同的,因此父超类可能是有用的(这解决了抽象[谁负责什么],封装[单独的实现)。适当的级别]和多态[哪些类支持常用方法]。 Note that, even if you settle on an superclass, you'd be wise to back it with an interface (it makes introduction of sibling or replacement classes easier in time - think maintenance). 请注意,即使您选择了一个超类,也最好通过一个接口来支持它(它使同级或替换类的引入更容易及时-考虑维护)。

Next, work on the issues you found. 接下来,处理发现的问题。 Has your draft design clarified any of them? 您的草稿设计澄清了吗? For instance, your Main needs to know about its windows but - what type are they? 例如,您的Main需要了解其窗口,但是-它们是什么类型? So, has any of your refinement made this clearer? 那么,您所做的任何改进都使这一点更加清楚了吗?

Do any patterns present themselves? 是否出现任何模式? for this you need to already have a feel for design patterns I'm afraid so buy and absorb the GoF Design Patterns book . 为此,您恐怕已经对设计模式有所了解,因此,请购买和吸收GoF设计模式书 It'll put you in good stead for spotting patterns as you go. 这将使您在走时发现模式方面处于有利地位。 I'd also recommend reading this specific book before taking on any others as it's technology agnostic (and some other books arebloated with tech-specific workarounds). 我还建议您先阅读这本特定的书,再考虑其他与技术无关的书籍(以及其他一些书籍因技术而异的解决方法而blo肿)。 Perhaps study the two patterns I pointed out and see if they fit your requirement. 也许研究我指出的两种模式,看看它们是否符合您的要求。

On the whole though, your ideas seem to be going in the right direction. 总体而言,您的想法似乎朝着正确的方向发展。

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

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