Explanation of Faade and Adapter Patterns n Faade


; // returns object given index // Using Dynamic Array (cont. ) T &operator[](size_t index); // returns object given index // Using](https://slidetodoc.com/presentation_image_h2/5d11ea82a1eae2126acf994ee5748316/image-3.jpg)





![The Sort_AT_Adapter Class (cont) // These methods adapt Access_Table methods: // The indexing ([]) The Sort_AT_Adapter Class (cont) // These methods adapt Access_Table methods: // The indexing ([])](https://slidetodoc.com/presentation_image_h2/5d11ea82a1eae2126acf994ee5748316/image-9.jpg)
- Slides: 9
Explanation of Façade and Adapter Patterns n Façade Intent n Provide a unified interface to a set of interfaces in a subsystem n n Façade defines a higher-level interface that makes the subsystem easier to use Adapter Intent n Convert the interface of a class into another interface clients expect n Adapter lets classes work together that couldn’t otherwise because of incompatible interfaces
Dynamic Array // // // The Array class is the low-level implementation of the data storage. It is hidden from the Sort class in multiple levels- the Access_Table class makes use of the Array class to store what needs to be stored from input. The Sort_AT_Adapter class then provides the adaptation between the Sort and Access_Table classes. template <class T> // Templated class definition for Array class Array { // interfaces available to other classes public: Array (size_t size = 0); // Constructor int init (size_t size); // initialize size with given size argument
Dynamic Array (cont. ) T &operator[](size_t index); // returns object given index // Using operators instead of function // calls is a convenience issue. This is the // facade pattern. size_t size (void) const; //. . . // returns current size of array // interfaces exclusive to this class private: T *array_; // a pointer to a single object size_; // the current size of the array };
The Access_Table Class // // // The Access_Table class makes use of the Array class to store its input. The function calls for using the Access_Table class are not the same as the function calls that The Sort class expects to use, this Access_Table class will be adapted using the Sort_AT_Adapter class. This class uses some Adapter pattern ideas, in that it hides the indexing call to the access_array_ variable with a function called element(index). template <class T> class Access_Table { // Templated class definition for Access_Table // interfaces available to other classes public: // Factory Method for initializing Access_Table. virtual int make_table (size_t num_lines, Char *buffer) = 0;
The Access_Table Class (cont) // Destructor: // Release buffer memory. virtual ~Access_Table (void) { delete [] buffer_; } // Retrieve reference to <indexth> element. T &element (size_t index) { return access_array_[index]; // passes call to the underlying Array object } // Length of the access_array. size_t length (void) const { return access_array_. size (); } // passes call to the underlying array
The Access_Table Class (cont) // interfaces available only to this class and classes derived from it protected: Array<T> access_array_; // Access table is array of type T. char *buffer_; // Hold the data buffer. };
The Sort_AT_Adapter Class // // The Sort_AT_Adapter class is an intermediary adapter class that translates the calls that the Sort class would make into the appropriate calls for the Access_Table class. The calls the Sort class makes are standard for that class, and we shouldn't have to be aware of how the Acess_Table class works in the Sort class. Instead, we adapt the Access_Table class to the Sort class by making an additional class that calls the appropriate functions between classes. This way two classes can communicate with one another when they otherwise could not. // Low level detail: we use this struct for storing pointers to the actual // data struct Line_Ptrs { // Comparison operator used by sort(). int operator< (const Line_Ptrs &);
The Sort_AT_Adapter Class (cont) // beginning of line and field/column. char *bol_, *bof_; }; class Sort_AT_Adapter : // Note the use of the class form of the Adapter private Access_Table<Line_Ptrs> { // interfaces available to other classes public: // creates the table of data virtual int make_table (size_t num_lines, char *buffer); typedef Line_Ptrs TYPE; // Type trait.
The Sort_AT_Adapter Class (cont) // These methods adapt Access_Table methods: // The indexing ([]) operator is used to access a given index. // We translate this into the actual function call that gets // and indexed value, which is may not be in a simple indexable // array at all. This is the Facade pattern - we hide the actual // implementation with a simpler, easier to use operator. T &operator[] (size_t index) { return element (index); } // The size function is what the Sort class expects to call. // This is translated to the "length" function of the Access_Table // class. size_t size (void) const { return length (); } };