-1

A Facade is a higher level API over a whole subsystem. A God is class that violates SRP. Where do we draw the line? Does the following code represent any of the two:

enum UnitType{Demon, Paladin};

namespace demons { bool are_scary(); static unsigned numbers; void attack(); void phase_out(); }

namespace paladins { static int morale; void attack(); void retreat(); }

struct Combatant { // Depending on the type of unit // and the values of the two global variables // calls one of the appropriate functions // and perhaps modifies the globals. // Is this a proper facade for the 'units' subsystem? void attack(); void retreat(); };

Extremely contrived, I know. What I am trying to illustrate is that a Facade rarely just forwards calls to appropriate functions/methods/types but rather often needs to implement additional logic and that logic encompasses a whole wide system.

Vorac
  • 7,169

3 Answers3

7

A facade contains the absolute minimum amount of business logic necessary to translate from the outer layer to the inner layer(s); if it contains a significant amount of business logic itself, I'd say it's not a facade but something else - but that doesn't necessarily mean it's a "god object" either.

With all due respect to your example, I think it's too contrived to be useful; if this were a project of any size, the first thing I'd be doing would be to be refactoring both your demons and paladins functionality to be actual objects with no sign of globals or anything like that.

4

Based on your post, you completely misunderstand the concept of a facade. There is no compromise to be made so that a facade doesn't become a "God class." To answer your question, you need to understand first what is a facade, or more specifically, what is the purpose of the Facade Pattern.

Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use.

You summarized this well in your post. So, it surprises me when you conflate this to "God classes". A facade object doesn't pack all the functionality in it. It simply wraps subinterfaces to simplify the API for its users.

To illustrate, I will use the following analogy. The gear shift in your car is part of a facade that allows drivers to interact in a simple way with a car's transmission. But the functionality of the transmission is not packed inside the facade. A new (simplified) layer is created because drivers are not expert users like mechanics. Therefore, to drive, they make use of this simplified interface to shift gears, for instance. However, mechanics, as expert users, can continue to interact with the transmission class (or interface) directly and call the method that the Shift Gear facade calls internally. So, as you can see, the functionalilty is not packed in the facade.

The conclusion I must reach is simply that, if you have an implementation of the fadade pattern that looks like a "God class", then it was done poorly to say the least. I hope my simple illustration help you understand the difference.

hfontanez
  • 221
0

The existing answers already cover the point that the facade contains the minimum of logic, delegating that to other objects; whereas a god object contains that logic itself.

However, it's still possible for a facade to be somewhat god-like. A system may have so many methods available that, even with that minimum of logic, the facade is large and has poor cohesion. This might not technically be a god object, but it could get to a point where it's undesirable for similar reasons.

If this occurs, it might be an indication that the facade pattern is a poor fit for the situation, or that there's another issue. Solutions could include breaking up the facade into a number of more specialised, cohesive objects (taking pragmatic benefit rather than strictly following the pattern), or even breaking the system itself up into smaller systems with their own facades. Or it might be that having everything in one place is beneficial enough to justify a large facade, despite it being unwieldy.

just me
  • 653