Скачать презентацию Façade March 2003 91 3913 R Mc Fadyen Скачать презентацию Façade March 2003 91 3913 R Mc Fadyen

d365c84304bbbb8c1df5938c004f8ba1.ppt

  • Количество слайдов: 10

Façade March 2003 91. 3913 R Mc. Fadyen 1 Façade March 2003 91. 3913 R Mc. Fadyen 1

Facade P. 368+ From www. m-w. com Main Entry: fa·cade Variant(s): also fa·çade /f&-'säd/ Facade P. 368+ From www. m-w. com Main Entry: fa·cade Variant(s): also fa·çade /f&-'säd/ Function: noun Etymology: French façade, from Italian facciata, from faccia face, from (assumed) Vulgar Latin facia Date: circa 1681 1 : the front of a building; also : any face of a building given special architectural treatment 2 : a false, superficial, or artificial appearance or effect March 2003 91. 3913 R Mc. Fadyen 2

Facade P. 368+ Problem: There a set of classes, a subsystem, that you need Facade P. 368+ Problem: There a set of classes, a subsystem, that you need to interact with for some purpose, but you don’t want to create dependencies on this subsystem. Solution: Create a class that wraps this subsystem. The wrapper will define an interface that hides the details (classes, methods) of the subsystem. The façade is a “front-end” object that defines a single point of entry to the subsystem’s services. A façade, a wrapper March 2003 91. 3913 R Mc. Fadyen Showing the classes as a package (a subsytem) 3

Facade P. 368+ Text example. Next. Gen POS is a system to be sold Facade P. 368+ Text example. Next. Gen POS is a system to be sold to many customers. Customers will want to customize Next. Gen POS Example: Payment rules may vary for gift certificates. How are gift certificates to be handled: • One per customer purchase? • No change is returned - another gift certificate issued? To allow flexibility, it is desired to reduce the impact of changes to the rest of the system To reduce the impact, the Façade pattern is applied and the subsytem will be hidden behind a single object March 2003 91. 3913 R Mc. Fadyen 4

Figure 23. 19 Sale objects, in the Domain package, access the POS Rule Engine Figure 23. 19 Sale objects, in the Domain package, access the POS Rule Engine via the façade in the POSRule. Engine package On page 370: Public class Sale … if ( POSRule. Engine. Facade. get. Instance(). is. Invalid(…). . . March 2003 91. 3913 R Mc. Fadyen ASIDE: on Page 371 mention of the Singleton pattern 5

On page 370: Public class Sale … if ( POSRule. Engine. Facade. get. Instance(). On page 370: Public class Sale … if ( POSRule. Engine. Facade. get. Instance(). is. Invalid(…). . . ASIDE: on Page 371 mention of the Singleton pattern Singleton is a pattern whereby one instance of an object of some class is created/allowed. The class will have a static method that returns the singleton. Façades are normally accessed via the Singleton pattern. March 2003 91. 3913 R Mc. Fadyen 6

Facade : Sale : POSRule. Engine. Facade : : Sales. Line. Item make. Line. Facade : Sale : POSRule. Engine. Facade : : Sales. Line. Item make. Line. Item() is. Invalid (…) . . . [valid] add (…) March 2003 91. 3913 R Mc. Fadyen 7

Example: (from Design Patterns Explained by Shalloway & Trott) Suppose a client object must Example: (from Design Patterns Explained by Shalloway & Trott) Suppose a client object must deal with Databases, Models, and Elements. The client must first open the Database, then get a Model, then queries the Model for Elements, and finally gets Elements. I. e. Database Client Model Element March 2003 91. 3913 R Mc. Fadyen 8

Example: (from Design Patterns Explained by Shalloway & Trott) It may be easier to Example: (from Design Patterns Explained by Shalloway & Trott) It may be easier to create a Database Façade that can be used by clients I. e. Client. A Database Client. B Db Facade Model Element Client. C To use the database, one only needs to become aware of Db. Facade, and learn how to use it. March 2003 91. 3913 R Mc. Fadyen 9

When to use Facade (from Design Patterns Explained by Shalloway & Trott) • To When to use Facade (from Design Patterns Explained by Shalloway & Trott) • To create a simpler interface: number of methods, number of objects one has to deal with • The system being hidden may have an older, more complex interface • The cost of developing the façade, and of developers learning the new interface may be less than learning the old one • You may only be needing a subset of the functionality • You may want to augment the functionality of the system • To facilitate tracking system usage – by forcing all requests to go through a Façade class, one can easily track the usage. • To facilitate subsystem replacement – only one class is affected, the Façade class. This is the motivation in Larman’s example. March 2003 91. 3913 R Mc. Fadyen 10