2. Design and implementation of mobile phone backup software
| A software company will develop a mobile phone backup software, the function is as follows: can be in the phone's contacts, text messages, photos of these resources to backup. Contacts, SMS, photo backup is three separate modules. Please use the appearance mode to design this. |
1. How to implement without using the Appearance mode
Telphonenumber for the Address Book Backup module, shortmessage for SMS Backup module, image as a photo backup module. The implementation code is as follows:
#ifndef _resourse_h_#define _resourse_h_#include <iostream> #include <string>using namespace std;// Phone number class Telphonenumber{public:void Bakeuptelphonenumber () {cout << backup phone number << endl;}};/ /SMS Class Shortmessage{public:void Bakeupshortmessage () {cout << "Backup SMS" << endl;}};/ /Photo class Image{public:void Bakeupimage () {cout << "back up photos" << Endl;}}; #endif
The test code is implemented as follows:
#include <iostream> #include "Resourse.h" using namespace Std;int Main () {/******************* backup phone number ************ /telphonenumber * Ptelphonenumber = new Telphonenumber ();p telphonenumber-> Bakeuptelphonenumber ();/******************* Backup SMS **************************/shortmessage * pShortMessage = new Shortmessage ();p shortmessage->bakeupshortmessage ();/******************* Backup Photos **************************/image * Pimage = new Image ();p image->bakeupimage ();/******************* destroy Operation **************************/delete Ptelphonenumber;ptelphonenumber = Null;delete Pshortmessage;pshortmessage = null;delete pImage;pImage = NULL;return 0;}
The results of compiling and running are as follows:
Although the above code can be implemented for phone numbers, text messages, photos to be backed up. But the client directly operates the phone backup, SMS Backup, photo backup module, the client needs to know the details of the backup, the client and the three class coupling increased, contrary to the rules of the principle of the most children. The backup operation is now only performed on the client, and if the backup operation is required elsewhere, the reusability is not strong, and if the backup operation is modified, multiple places will be modified with poor maintainability. Therefore, it is necessary to refactor the above code, encapsulating a skin class, which is backed up by the appearance class on three resources, which allows the customer class to be decoupled from the backup module.
2. How to use the appearance mode
The backup module code does not change, the implementation code is as follows:
#ifndef _resourse_h_#define _resourse_h_#include <iostream> #include <string>using namespace std;// Phone number class Telphonenumber{public:void Bakeuptelphonenumber () {cout << backup phone number << endl;}};/ /SMS Class Shortmessage{public:void Bakeupshortmessage () {cout << "Backup SMS" << endl;}};/ /Photo class Image{public:void Bakeupimage () {cout << "back up photos" << Endl;}}; #endif
Add a backup appearance class Bakeupfacade, which operates on the backup module and unlocks the coupling between the customer class and the specific backup module. It delegates all requests from the client to the appropriate subsystem, which is passed to the corresponding Subsystem object processing.
#ifndef _facade_h_#define _facade_h_#include "Resourse.h"//Backup appearance class bakeupfacade{private://maintenance on phone numbers, SMS, A reference to a photo object Telphonenumber * m_ptelphonenumber; Shortmessage * M_pshortmessage;image * m_pimage;public://appearance constructor, create phone, SMS, Photo object Bakeupfacade () {m_ptelphonenumber = new Telphonenumber (); m_pshortmessage = new Shortmessage (); m_pimage = new Image ();} Backup resource void Bakeupresourse () {m_ptelphonenumber->bakeuptelphonenumber (); m_pshortmessage->bakeupshortmessage (); M_pimage->bakeupimage ();}; #endif
The test code is implemented as follows:
#include <iostream> #include "Facade.h" using namespace Std;int Main () {//Create skin Object Bakeupfacade * Pbakeupfacade = new Bakeupfacade ();//backup Pbakeupfacade->bakeupresourse ();//Destroy object Delete Pbakeupfacade;pbakeupfacade = Null;return 0;}
Compiled and executed with the following results:
The appearance class is introduced, the appearance class operates the backup module, and the coupling between the customer class and the backup module is relieved. Customer classes do not need to directly manipulate the subsystem, but by the appearance of the class is responsible for processing, for the client is transparent, customer class only need to manipulate the appearance of the class can be, in line with the "Di Fan law." If multiple places need to facade, that is, the appearance can realize the sharing of functions, that is, to achieve reuse. The same calling code is only used to write once in the facade, without having to repeat it in multiple calls. If a backup module needs to be modified, only need to modify this backup module, no impact on the client, good maintainability.
The appearance mode of C + + design mode (ii)