What is the advantage of using a facade together with MVC?

Read about facade but don't quite understand the benefits of using it primarily using MVC. Can anyone help me understand? Another practical example? Excuse my ignorance!

+3


source to share


3 answers


The Facade Pattern is a cumulative service that provides multiple contextual functions or services.

Material from Wikipedia

The facade (or facade) pattern is a software design pattern commonly used with object oriented programming. The name is similar to the architectural facade.

A facade is an object that provides a simplified interface for most of your code, such as a class library. Facade can:

  • make the software library easier to use, understand, and test because the facade has convenient methods for common tasks;

  • make the library more readable for the same reason;

  • reduce the dependence of external code on internal library actions, since most of the code uses a facade, which allows for increased flexibility in system development; wrap a poorly designed collection of APIs with one well designed API (as per the objectives).

Having said that, from my head, let's say you have an accounting system for which the main form is MDI, so you have multiple menus here and there that allow the user to access those features.

To avoid multiple dependencies, instead of writing something like the following:

public class MainWindow : Form {
    public MainWindow(CustomerManagementPresenterFactory customerMgmt
        , SupplierManagementPresenterFactory supplierMgmt
        , InventoryManagementPresenterFactory inventoryMgmt
        , EmployeeManagementPresenterFactory employeeMgmt
        , StatementOfAccountManagementPresenterFactory statementOfAccntMgmt
        , InvoicesPastDueManagementPresenterFactory pastDueInvoicesMgmt) {
        // Work your dependencies here...
    }
}

      

where you can clearly see that your MainWindow depends on multiple factories (or in general), it's time to refactor. One question:

  • Do I have too many dependencies passed to the constructor MainWindow

    ?

Answer: YES, I have. Regardless, I MainWindow

need all of these dependencies to fulfill my requirements.

  • How can I refactor to reduce the number of dependencies?

You can specify that the functions are Past due invoices

, Statement of accounts

and Customer management

should be aggregated into only one service. Here is the Facade .

public class CustomerManagement {
    public CustomerManagement(CustomerManagementPresenterFactory customerMgmt
        , StatementOfAccountManagementPresenterFactory statementOfAccntMgmt
        , InvoicesPastDueManagementPresenterFactory pastDueInvoicesMgmt) {
        // Work your dependencies here...
    }
}

      

So now the constructor MainWindow

might look like this.



public class MainWindow : Form {
    public MainWindow(CustomerManagement customerMgmt
        , SupplierManagementPresenterFactory supplierMgmt
        , InventoryManagementPresenterFactory inventoryMgmt
        , EmployeeManagementPresenterFactory employeeMgmt) {
        // Work your dependencies here...
    }
}

      

Then, depending on your business domain, it SupplierManagement

can be closely linked to yours InventoryManagement

so that other aggregations can be done as follows.

public class InventoryManagement {
    public InventoryManagement(InventoryManagementPresenterFactory inventoryMgmt
        , SupplierManagementPresenterFactory supplierMgmt) {
        // Work your dependencies here...
    }
}

      

So that you can continue refactoring:

public class MainWindow : Form {
    public MainWindow(CustomerManagement customerMgmt
        , InventoryManagement inventoryMgmt
        , EmployeeManagementPresenterFactory employeeMgmt) {
        // Work your dependencies here...
    }
}

      

Now, as you can see, it is much easier to understand what you can do MainWindow

to serve your purpose. The idea is to combine what makes sense for your business area. In another context, perhaps it would be preferable to group tags CustomerManagement

, SupplierManagement

and EmployeeManagement

because they have the same information for the exchange of both Name

, Address

, Phone Number

, Email

, etc. It primarily depends on your needs. In the end, the role of the facade still persists, grouping similar business functions to add a layer of abstraction and reduce complexity.

A very interesting article on the topic Mark Seemann - Refactoring for Service Aggregation .

For more information on Design Patterns

and Dependency Injection

, I recommend Mr. Seeman's book: Dependency Injection in .NET . Although the title says .NET

, anyone wishing to learn about Dependency Injection

and Design Patterns

should read it to grow one understanding across topics and improve one skill. The example is easy to understand even if you are not from .NET.

Other interesting Q / A here on SO about the facade pattern

Renouncement

I know your question is tagged Java

and I posted the C # code. This is for a quick head-on response. I think that although my samples are C # you can easily understand the key point of the examples shown for the facade as they are simple IMHO

+5


source


Facades are meant to wrap around complex functionality, their main purpose is to hide the complexity of the underlying system. You can think of a Facade as a layer that wraps complex functionality and provides simpler methods of interaction.

It provides an interface to the client through which the client can access the system. This type of design pattern is structural in nature, as this pattern adds an interface to the outgoing system to hide its complexities.



enter image description here

+1


source


MVC is more like a general mindset when it comes to computer design. MVC could be said when talking about web development or desktop applications But with EJBs or facades it is really less general and more specific. "It was used to handle all transaction manager interactions and interactions with the stability manager." You will be using something similar with JSF as a View, JPA or Hibernate. Also, using EJBs gives more options for finished code.

Sometimes I think that the differences between certain subjects such as facades and MVC are not always black and white or so obvious.

+1


source







All Articles