按照正确的OO设计使用另一个对象的功能-封装 [英] using another object's functionality following a proper OO design - encapsulation

查看:60
本文介绍了按照正确的OO设计使用另一个对象的功能-封装的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在讨论适当的面向对象设计,以使用Java类中另一个对象的功能(方法),同时使两个对象尽可能保持解耦。

I am debating the proper, OO-design to use another object's functionality (methods) from a java class, while both objects remain decoupled as much as possible.

例如,在类的某个点上,要实现我的逻辑,我需要调用一个属于另一个对象的方法,例如助手类。该帮助程序类不必与我的原始类有任何关系,它只具有一个特定的方法,该方法对于我的类是可见的,可供我的类使用。

For example, at some point in my class, to implement my logic, I need to call a method that belongs to another object, say a helper class. This helper class does not need to be in any way related to my original class, it just has a specific method which is visible to, and available for my class to use.

在实现逻辑之后,不需要使用辅助方法(或辅助对象)。

After the logic is implemented, the helper method (or the helper object) is not needed down the line.

很显然,我需要在其中引用该辅助对象为了使用其方法。但是要强制执行封装,我不应该在原始类中声明一个实例变量来引用此辅助对象吗?这种推理正确吗?另外,helper类不知道任何可能使用它的客户端类。

Obviously, I would need a reference to this helper object in order to use its method. But to enforce encapsulation, I should not declare an instance variable in my original class to reference this helper object? Is that reasoning correct? Also, the helper class is not aware of any client class that might use it.

在这种情况下,使用局部变量是否更合适?在将利用其功能的方法中声明和实例化辅助对象?在我的原始类中声明和实例化此类辅助对象的最佳位置在哪里?

Would a local variable be more appropriate in this case? Declare and instantiate the helper object in the method which will make use of its functionality? Where is the best location in my original class to declare and instantiate such a helper object?

我想知道是否有一个高级示例,或者是否对此做了解释在OO文章中有更多阐述。我希望您能对上述内容进行任何有关封装的输入或提示。

I was wondering if there is a high-level example, or if this is explained with a bit more elaboration in OO articles. I'd appreciate any encapsulation-focused input or hint on the above.

推荐答案

那么这是场景吗?这些是问题吗?

So is this the scenario? Are these the questions?

Class MyClass {
   private SomeState myState;

   public voic oneMethod() {
         // Q1 : what type is **ahelper** ?
         // Q2 : where do we declare it ?
         // Q3 : where do we initialise it?
         aHelper.doSomeWork();

         // update your state depending upon the results
   }
}

第一季度。我认为您应该将aHelper声明为接口

Q1. I think you should declare aHelper as an Interface

 HelperInterface aHelper;

然后,我们没有加入任何特定的实现。

Then we are not coupled to any specific implementation.

Q2。如果只在一个地方使用它,则在该函数中声明它,否则作为成员变量声明。

Q2. If you only use it in one place declare it in that function, otherwise as a member variable. You don't increase coupling by doing either.

 HelperInterface aHelper = ? what here?
 aHelper.soSomeWork();

Q3。

public MyClass(HelperInterface injectedHelper) {
    aHelper = injectedHelper;
}

这可以使测试非常容易,您的测试可以注入Mocked Helper类。

This can make testing very easy, your tests can inject a Mocked Helper class.

或者您可以使用惰性初始化器。如果您的助手是方法中的局部变量,则这非常方便。再次根据您的喜好注入工厂或将其设为静态。

Or you can use a lazy intialiser. This is quite handy if your helper is a local variable in your method(s). Again the factory can be injected or be static depending upon your preference.

private getHelper() {
    if (aHelper == null ){ make the helper, perhaps using a factory }

    return aHelper     
}

这篇关于按照正确的OO设计使用另一个对象的功能-封装的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆