Big talk design mode-appearance mode (facade mode), design mode Facade
What is appearance mode?
Provides a consistent interface for a group of interfaces in the subsystem. This mode defines a high-level interface, which makes the subsystem easier to use.
When to use appearance Mode
This involves three phases,First, in the initial stage of design, we should consciously separate the two layersFor example, in a classic three-tier architecture, you need to consider establishing an external Facada between the data access layer, business logic layer, business logic layer, and presentation layer, in this way, a simple jiek can be provided for complex subsystems, greatly reducing coupling,Second, in the development stage, subsystems often become more and more complex due to constant reconstruction and evolution,Many small classes are generated in most modes. This is a good thing, but it also brings difficulties in using external calls to their user programs.Facade can provide a simple interface to reduce dependencies between them. Third, when maintaining a first-class large system, it may be very difficult to maintain and expand the system.But because it contains very important functions, new demand development must depend on it. In this case, it is also very suitable to use the appearance mode Facade, you can develop a Facade object interaction for the new system. Facade interacts with the first-class code in all the complicated work.
OC code implementation
SubsystemOne. h # import <Foundation/Foundation. h>
@ Interface SubsystemOne: NSObject
-(Void) MethodOne; @ end
SubsystemOne. m
# Import "SubsystemOne. h" @ implementation SubsystemOne-(void) MethodOne {NSLog (@ "method 1");} @ end
The other three subclasses are similar.
Facade. h
#import <Foundation/Foundation.h>#import "SubsystemOne.h"#import "SubSystemTwo.h"#import "SubSystemThree.h"@interface Facade : NSObject@property(nonatomic,strong)SubsystemOne *one;@property(nonatomic,strong)SubSystemTwo *two;@property(nonatomic,strong)SubSystemThree *three;-(void)MethodA;-(void)MethodB;@end
Facade. m
# Import "Facade. h "@ implementation Facade-(instancetype) init {self = [super init]; if (self) {_ one = [[SubsystemOne alloc] init]; _ two = [[SubSystemTwo alloc] init]; _ three = [[SubSystemThree alloc] init];} return self;}-(void) methodA {NSLog (@ "method A"); [_ one MethodOne]; [_ two MethodTwo]; [_ three MethodThree];}-(void) methodB {NSLog (@ "method B"); [_ one MethodOne]; [_ three MethodThree] ;}@ end
Client
#import <Foundation/Foundation.h>#import "Facade.h"int main(int argc, const char * argv[]) { @autoreleasepool { Facade *facade=[[Facade alloc]init]; [facade MethodA]; [facade MethodB]; } return 0;}
Running result
Summary applicable scenarios
The appearance mode can be considered in the following situations: (1) in the initial stage of design, you should consciously separate different layers and establish the appearance mode between layers. (2) in the development stage, subsystems become more and more complex, and the appearance mode is added to provide a simple interface for calling. (3) When maintaining a large legacy system, it may be very difficult to maintain and expand the system, but it also contains very important functions to develop a appearance class for it, this allows the new system to interact with it.
Advantage (1) implements loose coupling between subsystems and clients. (2) The client shields subsystem components, reduces the number of objects to be processed by the client, and makes the subsystem easier to use.