Big talk design mode-appearance mode (facade mode), design mode Facade

Source: Internet
Author: User

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.

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.