Objective C design mode-appearance mode facade
If a framework contains many classes or has complex functions, you can use a helper class to provide simple interfaces for some common scenarios. In this way, when using this framework, customers can easily cope with common scenarios and implement functions that meet their own requirements in the framework. This is like buying a computer, you can buy a brand-name machine, and you can buy your own accessories for assembly. Here is an example of buying a computer.
Assume that the computer is composed of a display, motherboard, CPU, memory, and video card. Of course, this is far more than actually. Each device has many parameters to select. We define a class for them to complete the selection.
// Select the Display @ interface Display: NSObject-(void) chooseDisplay :( NSString *) type; @ end @ implementation Display-(void) chooseDisplay :( NSString *) type {NSLog (@ select display: % @, type) ;}@ end // select motherboard @ interface MainBoard: NSObject-(void) chooseMainBoard :( NSString *) type; @ end @ implementation MainBoard-(void) chooseMainBoard :( NSString *) type {NSLog (@ select motherboard: % @, type);} @ end // select CPU @ interface CPU: NSObject-(void) chooseCPU :( NSString *) type; @ end @ implementation CPU-(void) chooseCPU :( NSString *) type {NSLog (@ select CPU: % @, type);} @ end // Client code @ interface Client: NSObject-(void) assebleComputer; @ end @ implementation Client-(void) assebleComputer {Display * display = [Display new]; [display chooseDisplay: @ AOC]; MainBoard * mainBoard = [MainBoard new]; [mainBoard chooseMainBoard: @ Asus]; CPU * cpu = [CPU new]; [cpu chooseCPU: @ i7 processor]} @ end
The output result is as follows:
Select monitor: AOC select motherboard: ASUS select CPU: i7 processor
Class diagram:
We can use these selected classes to assemble computers with arbitrary configurations. However, if you are a white user, they may never have heard of any processor or motherboard. At this time, we may want to do this in a simple way.
@ Interface MacBookPro: NSObject-(void) chooseDevice; @ end @ implementation MacBookPro-(void) chooseDevice {Display * display = [Display new]; [display chooseDisplay: @ Samsung]; mainBoard * mainBoard = [MainBoard new]; [mainBoard chooseMainBoard: @ Asus]; CPU * cpu = [CPU new]; [cpu chooseCPU: @ i5 processor]} @ end
The customer's code is:
@interface Client : NSObject- (void)assebleComputer;@end@implementation Client- (void)assembleComputer { MacBookPro *computer = [MacBookPro new]; [computer chooseDevice];}@end
Output result:
Monitor: Samsung motherboard: asus cpu: i5 Processor
UseMacBookPro
The advantage of this category is that customers can get a complete computer without learning about the composition of the computer. In this way, we can satisfy both the white and professional users.