140 likes | 333 Views
Façade Pattern. Seung Ha. Façade?. Façade is generally one side of the exterior of a building, especially the front. Meaning “frontage” or “face”. In software architecture, façade defines high level interface to use subsystems. High-level Interface.
E N D
Façade Pattern Seung Ha
Façade? • Façade is generally one side of the exterior of a building, especially the front. • Meaning “frontage” or “face” • In software architecture, façade defines high level interface to use subsystems.
High-level Interface Provide a unified interface to a set of interfaces in a subsystem. Façade defines a higher-level interface that makes the subsystem easier to use. client classes subsystem classes facade
When Façade Pattern is used? • To provide simple interface to a complex subsystems. • To decouple the subsystem from clients and other systems. • To layer subsystems.
Structure and Participants • Façade • The façade class interacts subsystem classes with the rest of the application. • Subsystem classes • Software library / API collection accessed through the façade class • Clients • The objects using the façade pattern to access resources from the subclasses Client1 Client2 facade DoSomething() Class1 Class3 Public void DoSomething() { Class1 book = new Class1(); Class2 customer = new Class2(); Class3 billing = new Class3(); book.CheckStock(); customer.GetShippingInformation(); billing.Process(book, customer); } Class2
Consequences • Shields clients from subsystem components. • Reducing the number of objects that clients deal with and making the subsystem easier to use. • Layer a system and the dependencies between objects. • Eliminate complex or circular dependencies. This can be an important consequence when the client and the subsystem are implemented independently. • Reducing compilation dependencies – vital in large software systems
Implementation • Reducing client-subsystem coupling: • The coupling between clients and the subsystem can be reduced even further by making Façade an abstract class with concrete subclasses for different implementation of a subsystem. • Public vs. private subsystem classes: • Public interface to a subsystem consists of classes that all clients can access (Façade class is part of the public interface.) • Private interface is just for subsystem extenders.
Sample Code Client Web Page <Classes> • Data Access Layer vs. Business Layer • Data access subclasses defines how to access databases and meta data. • Business logic classes knows how to use data. • Façade – Data object • Common authentication interface • XML standardized data structure • Simple interface to access data Business Logic <Classes> Facade Data Object <Classes> Data Access Subclasses Database SQL Server Database Access
Sample Code Client – ASP.NET page ... private void GetData(intproductID) { VirtualObjectModel.Framework.Login login = new VirtualObjectModel.Framework.Login( System.Configuration.ConfigurationSettings.AppSettings["LoginID"], System.Configuration.ConfigurationSettings.AppSettings["Password"], System.Configuration.ConfigurationSettings.AppSettings["Database"], System.Configuration.ConfigurationSettings.AppSettings["DatabaseServer"]); VOM.ObjectModel.Northwind.Productsobj = new VOM.ObjectModel.Northwind.Products(login.ConnectionString); obj.GetData(productID); SetData(obj); } ...
Sample Code Business Object [Serializable()] public class Products { ... public virtual void GetData(intproductID) { try { XLMLDataTablexdt = new XLMLDataTable(_connectionString); xdt.DataSourceType = XLMLType.DataSourceType.SQLServer; xdt.CommandText = @"SELECT * FROM Northwind.dbo.[Products] WHERE ProductID = @ProductID"; xdt.XLMLParameterList.Add("@ProductID", VirtualObjectModel.Framework.XLMLType.DataType.INT).Value = productID; xdt.GetData(); foreach (XLMLDataRowxdr in xdt.XLMLDataRowList) { Products obj = this; xdr.SetAttribute(obj); }; } catch (System.Exception ex) { throw ex; } } ... }
Sample Code Façade – Data Object public class XLMLDataTable:XLML { ... public void GetData() { try { if (this.ConnectionString != string.Empty || this.CommandText != string.Empty) { XLMLDataTablexlmlDataTable = new XLMLDataTable(); switch (this.DataSourceType) { case XLMLType.DataSourceType.SQLServer: xlmlDataTable = GetXLMLDataTable(MSSQLServerDataService.ExecuteReader(this.ConnectionString, this.CommandType, ... break; case XLMLType.DataSourceType.Access: xlmlDataTable = GetXLMLDataTable(GetDataTable(this.ConnectionString, this.CommandText)); break; default: break; } this.Description = xlmlDataTable.Description; this.ID = xlmlDataTable.ID; this.XLMLDataColumnList = xlmlDataTable.XLMLDataColumnList; this.XLMLDataRowList = xlmlDataTable.XLMLDataRowList; } } catch (Exception ex) { this.Exception = ex; } } ... }
Sample Code Data Access Subclass public class XLML { ... protected XLMLDataTableGetXLMLDataTable(SqlDataReadersqlDataReader) { XLMLDataTablexlmlDataTable = new XLMLDataTable(); try { SetDataTableSchema(sqlDataReader, ref xlmlDataTable); SetDataRowList(sqlDataReader, ref xlmlDataTable); } catch (Exception ex) { throw ex; } finally { sqlDataReader.Close(); } return xlmlDataTable; } ... }
References • Erich Gamma, Richard Helm, Ralph Johnson, and John Vlissides. Design Patterns. Addison-Wesley Professional Computing Series • Wikipedia – Façade & Façade pattern