Use of the Facade Pattern in software is about apparent elegance on the ‘user’ side of the facade and the hiding of inelegance on the ‘implementation’ side. The implementation can change or be replaced easily over time, but that applies to many patterns. Thus it is the elegance of the facade that’s making it attractive to use in a layered design. There is a second aspect, thought, that is more subtle and often forgotten: reducing the number of calls needed through it. Hopefully to one.

Dishonored Facade Implementations

Just pretend for a second that a object that straddles a divide has gettable and settable things that are representative of more meaningful business things:

public interface Broken {
  void setGivenName(String gn)
  void setFamilyName(String fn)
  String getGivenName()
  String getFamilyName()
}

What we have in the above is something where two are likely to be called in series for typical interactions. We could see that getGivenName and getFamilyName could often be called together when populating an UI’s fields. Similarly setGivenName and setFamilyName could be called together when pushing the UI’s fields back towards persistence:

// populate UI:

gn = b.getGivenName()
fn = b.getFamilyName()

// (elsewhere) persist UI fields:

b.setGivenName(gn)
b.setFamilyName(fn)

If the facade happens to by hiding inelegance of lower level implementation of things that go over a wire, we will now observe two calls over the a RPC or RESTful divide. There are performance consequences that add up quickly.

Honoring Facade Patterns

When the facade pattern is implemented correctly, more data are channelled though fewer invocations (again these are representative of more meaningful business operations):

public class Name {
  // imagine constructor, getter, setter that
  // are characteristic of Immutable or ValueObject
  String firstName;
  String givenName;
}
public interface BetterFacade {
  void changeName(Name name)
  Name getName()
}

What we have now a single operation through the facade that takes or gives something that could be serialized quite easily. Again it does not matter at this stage whether the underlying implementation is a Remote Procedure Call, or something RESTful.

At this level of object granularity, it should be easy to get right.

The Dark Ages of Enterprise Computing

Sun’s late-90’s J2EE is the largest effort to bring bad practice to enterprise computing. Particularly the EJB stuff in its early incarnations. Sun promoted Session Facade as one of the constructs you should code in to your stack. In so doing, they forced a ton of inelegance on the developer. The page I link to shows the J2EE definition from the 2001-2002 era (Oracle could nix it at any time). It is also in the EJB 2.0 style before EJB 3.0 injected some small amount of sanity in 2005. From 1998 to 2004, Sun told folks to make directed graphs of components while constructing applications, that required a ton of boiler-plate code. In the effort to just get it working many teams forgot the desire to reduce invocations across the divide. Java teams are still perhaps forgetful in the years since the progressive abandonment of EJB in particular and J2EE generally since (the mid noughties).

Clues that you are doing it wrong.

While the above diagram does not show categorically an incorrect architecture, even if the there’s a TCP/IP separation between each of the boxes. It does show the start of a fan-out of connections. What starts with one I/O from the browser to the web-tier, results in three I/Os to lower level services. That could be nine more if each of those three did the same to layers below. We might have a constraint in that these have to be separate boxes, and we have to do I/O in series to them as part of the buyCartContents operation. Incidentally, the Catalog node in the above diagram, could have some caching attached to reduce some of our worries, but that is a different thing.

What I am trying to get across at this level of architecture, is the fact that you could quickly engineer something with a lot of downstream invocations that add up while you are otherwise busy with developing functionality. As you’re making your stack, look at the numbers of invocations across divides that could otherwise be facade-like, and worry of the growth of them is uncontrolled. Look at facade methods that are similar to each other, or always invoked in series and could be rolled into one.

One technique for doing it right from the start.

Simply put, you should rigidly stick to top-down design as you build your application stack over time. This is true for the run up to your initial go live, and the in the incremental releases that follow that first push. Do not let Database Administrators (DBAs) drive the design of your web-app. Nor engineers of lower level services. Both of these are ‘bottom-up’ techniques and classic false economies for enterprise development teams. Instead think about the facade like operations from the UI, and change downstream code as appropriate to support that. If that means that your UI teams are more instructional to service teams, so be it.

While adhering to the above, keep busy with your refactoring agenda , even if you are using a language that does not have an IDE as good as Intellij (Java) is. With a decent Agile perspective the right design will emerge at all times.



Published

September 21st, 2011
Reads: 1,729

Syndicated by DZone.com
Reads:

Categories

Comments formerly in Disqus, but exported and mounted statically ...


Wed, 21 Sep 2011Ian Fairman

Your recollection of the early years of EJB and J2EE sounds like revisionism to me. EJB was a big leap forward in usability compared to CORBA. Yes, it was difficult to use but it was easier than what had gone before. And EJB commodotized distributed objects and opened up the conversation on distributed systems. Without EJB there would have been no Spring.

Wed, 21 Sep 2011paul_hammant

I myself didn't like CORBA even then. I liked ObjectSpace's Voyager and that was about it. There was a ton of diversity before Sun formed the committee that made the spec that neutralized the interesting app-servers as they were rendered compatible. Spring (and others) might have come along given the roots of Inversion of Control (Apache's Avalon: 1998). OSGi also came into being the same year, and neither of those were originally positions as enterprise business component frameworks.

Thu, 12 Jan 2012patricklogan

CORBA was not great, but I am not sure EJB was a leap forward. e.g. CORBA was cross-language. Also CORBA had two faces: static and dynamic, mainly because the committee formed from two different camps. Yes, one from a static solution, the other from a dynamic solution.

The best CORBA implementations I encountered originated in the dynamic camp (and also happened to be implemented in Smalltalk). Dynamic CORBA was more elaborate than Voyager, but probably had more in common with Voyager than it did with its static CORBA partner.

Also don't forget the very dynamic Apache River, nee Jini / Javaspaces. Still the best distributed object system for Java, I'd say.

Well, those were the days...

Thu, 12 Jan 2012paul_hammant

Hi Pat. One thought about JINI from the moment it was created: I lacked a "one-liner" to say why you'd use it. Put your 1995 hat on for a sec:

Java: Rectangles of web-pages that are cross-platform HLL with UI and interop to arbitrary back-ends*
JavaSpaces: Shared memory between Java apps in different VMs.
JINI: ??????

Interesting thoughts about efficacy of dynamic CORBA that I never knew about.

* no same-origin policy back then.

Thu, 12 Jan 2012patricklogan

Yeah, Jini is not an easy one-liner. Maybe "Jini is an expandable set of Java-based services for building reliable distributed systems".

Discovery, registry, leasing, transactions, events, notifications, (add your own here), (replace and enhance the standard ones here as per specs).

I look at Akka, which has a one liner, "Actors for Scala and Java". And think that Jini still has so much more than Akka that that's good for distributed systems.