2

While developing a web based java application I am a bit confused about the use of Facade pattern or, more precisely, can say that I am not able to visualize its exact use. Here is my use-case:

I Have a web-application where I have Action classes, Service layer and DAO layer as well. My Action classes are responsible to interacting with the UI (JSP) while the service layer will be responsible for handling any business logic as well interacting with the DAO layer and getting DB related work done.

I can create service layer interface and can expose them in my action classes so as my action classes can interact with the Service layer and do any work.

I was asked to introduce another layer as Facade where I am required to expose methods to my Action class while the service layer is now being exposed in the Facade.

Can any one help me to clarify what is being asked is correct and what benefits will be achieved by this approach as it seems to me like introducing additional layer with no much work being done in facade layer (heavy lifting still be in service layer.)

Nishant
  • 54,584
  • 13
  • 112
  • 127
Umesh Awasthi
  • 23,407
  • 37
  • 132
  • 204

4 Answers4

2

by using a facade in between your services and your actions, you not always have to use particular services in your actions, you just use the facade. Also, some actions will use combinations of services in the same way, if you can put this in a method in your facade, you'll save yourself some time cause you can just reuse a facade method that uses that combination of services.

Tom
  • 4,096
  • 2
  • 24
  • 38
2

In addition to what @Tom has written, transactions are another thing to think about. If you want to use multiple services in the same transaction, e.g. reading from one service, writing to the database using a second service, you can do that using the same transaction if you define your transaction boundaries on the Facade level.

You are right that in the most simple case, your Facade will delegate straight to a service, but in more complex cases, it might pay off to have an additional layer on top. Especially if your services are very fine-grained, it makes sense to have a more coarse-grained layer wrapped around them. Whether you call that layer a Facade or just another service layer is then a minor matter.

nwinkler
  • 52,665
  • 21
  • 154
  • 168
0

Here is good example of Facade pattern

/* Complex parts */

class CPU {
    public void freeze() { ... }
    public void jump(long position) { ... }
    public void execute() { ... }
}

class Memory {
    public void load(long position, byte[] data) { ... }
}

class HardDrive {
    public byte[] read(long lba, int size) { ... }
}

/* Facade */

class ComputerFacade {
    private CPU processor;
    private Memory ram;
    private HardDrive hd;

    public ComputerFacade() {
        this.processor = new CPU();
        this.ram = new Memory();
        this.hd = new HardDrive();
    }

    public void start() {
        processor.freeze();
        ram.load(BOOT_ADDRESS, hd.read(BOOT_SECTOR, SECTOR_SIZE));
        processor.jump(BOOT_ADDRESS);
        processor.execute();
    }
}

/* Client */

class You {
    public static void main(String[] args) {
        ComputerFacade computer = new ComputerFacade();
        computer.start();
    }
}
Dmitriy Puchkov
  • 1,530
  • 17
  • 41
0

Can any one help me to clarify what is being asked is correct and what benefits will be achieved by this approach?

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

Facade pattern is more like a helper for client applications

When to use:

  1. A simple interface is required to access a complex system.
  2. The abstractions and implementations of a subsystem are tightly coupled.
  3. Need an entry point to each level of layered software.
  4. System is very complex or difficult to understand.

Refer to below post to understand the pattern better with working code examples (TravelFacade which helps in FlightBooking, TrainBooking and HotelBooking) :

What is Facade Design Pattern?

Ravindra babu
  • 37,698
  • 11
  • 250
  • 211