Facade


The FACADE pattern is used when you want to provide a simple and specific interface onto a group of objects that have a complex and general interface. Consider, for example, DB.cs in Listing 34-9. This class imposes a very simple interface, specific to ProductData, on the complex and general interfaces of the classes within the System.Data namespace. Figure 23-1 shows the structure.

Figure 23-1. The DB FACADE


Notice that the DB class protects the Application from needing to know the intimacies of the System.Data namespace. The class hides all the generality and complexity of System.Data behind a very simple and specific interface.

A FACADE like DB imposes a lot of policy on the usage of System.Data, knowing how to initialize and close the database connection, translate the members of ProductData into database fields and back, and build the appropriate queries and commands to manipulate the database. All that complexity is hidden from users. From the point of view of the Application, System.Data does not exist; it is hidden behind the FACADE.

The use of the FACADE pattern implies that the developers have adopted the convention that all database calls must go through DB. If any part of the Application code goes straight to System.Data rather than through the FACADE, that convention is violated. As such, the FACADE imposes its polices on the application. By convention, DB has become the sole broker of the facilities of System.Data.

FACADE can be used to hide any aspect of a program. However, using FACADE to hide the database has become so common, the pattern is also known as TABLE DATA GATEWAY.




Agile Principles, Patterns, and Practices in C#
Agile Principles, Patterns, and Practices in C#
ISBN: 0131857258
EAN: 2147483647
Year: 2006
Pages: 272

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net