4

I have my code working, but I don't know if the way that I implemented it is appropriate. Basically, I want to maintain the pattern without violating it.

The code looks like this:

Package Model (with setters/getters omitted):

public class CA {
    private Integer in;
    private Integer jn;
}

public class CB {
    private Integer kn;
    private Integer ln;
}

public class CC {    
    private static CC instancia;
    private CA a;
    private CB b;

    public static CC getInstancia() {
         if(instancia == null) {
             instancia = new CC();          
         }

         return instancia; 
    }
}

Package Business:

class CCBusiness {

    static CC c = CC.getInstancia();

    void alter(Integer input) {
        c.getCA.setIn(input);
        Integer num = c.getCB.getLn();
    }
}

Package Facade:

class FacadeOne {

void methodOne() {
    CCBusiness.alter(1);
    // And more xxBusiness.xx()
}

The real code is more complex, but to explain my doubts, I think this should work.

In one facade I call several Business objects, but it is appropriate that one Business (in this case, the one of CC class) can modify attributes from other classes (in this case, the ones inside CC)? Should I create CABusiness and CBBusiness?

Because, what I understand, one Business can't call another Business, so the second as to be parametrized to receive the object from FacadeOne (if I create CABusiness and CBBusiness)?

4

2 回答 2

6

我认为一些澄清可能会对您有所帮助:外观模式可以帮助您对隐藏在外观后面的多个类进行单点访问,从而对外界隐藏。通常这些类形成某种模块或逻辑单元。

您正在努力解决的是立面背后的结构及其层次结构。这很难在不了解全局的情况下进行分析,但是根据我掌握的信息,最好有几个您的 Business 课程,可以从外观单独调用。在业务对象之间创建交叉调用将有可能使您的代码意大利面。

至于最佳实践和技巧,最简单的就是给你的课程画个草图,这通常能说明很多。而且您已经完成了基于 UML 的文档的一半。:-)

顺便说一句,避免给你的类命名,如 CA、CB……这就像命名变量 a001、a002 一样……说出名字对可读性有很大帮助!

于 2013-10-14T08:28:23.613 回答
3

通过拥有Facade,您可以避免调用多个CxBusiness对象并将它们的操作集成到有意义的结果中。这就是外观的目的,通过在简洁明了的操作后面隐藏 5 个不同组件的交互来简化与业务层的交互:methodOne.

但是,对于个人CxBusiness而言,您要避免彼此之间的交叉调用;否则,您最终会得到一个复杂的依赖结构,可能会遇到循环引用。将每个模型保留CxBusiness为每个Cx模型的唯一包装,您将减少与它们交互时不需要的副作用的数量。这些之间的任何交互都将在外观中进行。

此外,通过让外观依赖于接口而不是具体类来强制执行此模式:ICABusiness、、ICCBusiness等。然后,访问任何模型的唯一方法应该是通过这些接口,显然,您不应该有一个CxBusiness具有ICxBusiness成员的具体(不交叉依赖)。一旦你设置了这些限制,实现本身将朝着更模块化和更少耦合的设计发展。

于 2013-10-14T12:34:44.187 回答