Objective C design mode: appearance mode facade and objectivefacade

Source: Internet
Author: User

Objective C design mode: appearance mode facade and objectivefacade

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

UseMacBookProThe 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. The following is a class diagram between them.

This document is organized by Changsha Davy education.

Http://io.diveinedu.com

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.