Explain the use of the share mode in the development of iOS App design mode

  • 2020-05-27 07:14:51
  • OfStack

The concept of enjoying yuan mode

In object-oriented software design, using common objects can not only save resources but also improve performance. Shared objects can only provide some intrinsic information, not be used to identify objects. One design pattern used specifically for designing shareable objects is called the enjoy pattern (Flyweight pattern).

Implementing the share pattern requires two key components, usually shareable share objects and a pool to hold them. Some central object maintains this pool and returns the appropriate instance from it.
Use sharing techniques to effectively support a large number of fine-grained objects.

Public transport, such as buses, has a history of more than 100 years. Large Numbers of passengers going in the same direction can share the cost of owning and operating a vehicle, such as a bus. Buses have multiple platforms and passengers follow the route to get on and off near their destination. The cost of getting to your destination is only related to the trip. It is much cheaper to take a bus than to keep a car. That's the advantage of using public resources.

In object-oriented software design, we use common objects not only to save resources but also to improve performance. For example, a person needs 1 million instances of a class, but we can share 1 instance of that class and put some unique information outside, and the savings can be considerable (the difference between 1 instance and 1 million instances). Shared objects only provide some intrinsic information and cannot be used to identify objects. One design pattern that is specifically used to design shareable objects is called the enjoy pattern.

What is the most important reason why the browser is lightweight? Not their size, but the total amount of space that can be saved by sharing. The unique state of some objects can be taken outside, managed elsewhere, and the rest Shared. For example, you used to need 1 million objects for a class, but since the objects of this class are called privileges, now you only need 1 object. This is why the system as a whole is lightweight due to shareable privilege objects. With careful design, the memory savings are considerable. In iOS development, saving memory means improving overall performance.

An example application of the enjoy mode

We create an WebSiteFactory factory class to maintain the Shared objects in the pool, and return the specific Shared objects of various types according to the parent type. The code is as follows:

#import <Foundation/Foundation.h>
#import "WebSiteProtocol.h"
@interface WebSiteFactory : NSObject
@property (nonatomic, strong) NSDictionary *flyweights; // The Shared object
- (id<WebSiteProtocol>)getWebSiteCategory:(NSString *)webKey;
- (NSInteger)getWebSiteCount;

#import "WebSiteFactory.h"
#import "ConcreteWebSite.h"
@implementation WebSiteFactory
- (instancetype)init {
    self = [super init];
    if (self) {
        _flyweights = [NSDictionary dictionary];
    return self;
- (id<WebSiteProtocol>)getWebSiteCategory:(NSString *)webKey {   
    __block id<WebSiteProtocol> webset = nil;
    [self.flyweights enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
        if (webKey == key) {
            webset = obj;
            *stop = YES;
    if (webset == nil) {
        ConcreteWebSite *concreteWebset = [[ConcreteWebSite alloc] init];
        concreteWebset.webName = webKey;
        webset = concreteWebset;
        NSMutableDictionary *mutabledic = [NSMutableDictionary dictionaryWithDictionary:self.flyweights];
        [mutabledic setObject:webset forKey:webKey];
        self.flyweights = [NSDictionary dictionaryWithDictionary:mutabledic];
    return webset;
- (NSInteger)getWebSiteCount {
    return self.flyweights.count;

The getWebSiteCategory method in the code can return a specific enjoy object, which also complies with the WebSiteProtocol protocol. The code of WebSiteProtocol is as follows:

#import <Foundation/Foundation.h>
#import "User.h"
@protocol WebSiteProtocol <NSObject>
- (void)use:(User *)user;

The code for ConcreteWebSite is as follows:

#import <Foundation/Foundation.h>
#import "WebSiteProtocol.h"
@interface ConcreteWebSite : NSObject <WebSiteProtocol>
@property (nonatomic, copy) NSString *webName;

#import "ConcreteWebSite.h"
@implementation ConcreteWebSite
- (void)use:(User *)user {
    NSLog(@" Site classification :%@ The user name :%@", self.webName, user.userName);

The code for User is as follows:

#import <Foundation/Foundation.h>
@interface User : NSObject
@property (nonatomic, copy) NSString *userName;

#import "User.h"
@implementation User

So far, we have finished the code of the enjoy mode. Let's see how to use the enjoy mode in the client side. The code is as follows:

#import "ViewController.h"
#import "WebSiteProtocol.h"
#import "WebSiteFactory.h"
#import "ConcreteWebSite.h"
#import "User.h"
typedef id<WebSiteProtocol> WebsiteType;
@interface ViewController ()

@implementation ViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Returns a variety of concrete enjoy objects through the factory method , Maintain the Shared objects in the pool
    WebSiteFactory *factory = [[WebSiteFactory alloc] init];
    // Returns the specific enjoy object
    WebsiteType type1 = [factory getWebSiteCategory:@" Home page "];
    User *user1 = [[User alloc] init];
    user1.userName = @" zhang 3";
    // The enjoy object has both use methods
    [type1 use:user1];
    WebsiteType type2 = [factory getWebSiteCategory:@" The store "];
    User *user2 = [[User alloc] init];
    user2.userName = @" li 4";
    [type2 use:user2];
    WebsiteType type3 = [factory getWebSiteCategory:@" case "];
    User *user3 = [[User alloc] init];
    user3.userName = @" The king 5";
    [type3 use:user3];
    NSInteger count = [factory getWebSiteCount];
    NSLog(@" The number of : %ld", (long)count);
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.

The output is as follows:

2015-09-12 15:59:55.322 FlyweightPattern[42020:1723017]  Site classification : Home page   The user name : zhang 3
2015-09-12 15:59:55.322 FlyweightPattern[42020:1723017]  Site classification : The store   The user name : li 4
2015-09-12 15:59:55.322 FlyweightPattern[42020:1723017]  Site classification : case   The user name : The king 5
2015-09-12 15:59:55.323 FlyweightPattern[42020:1723017]  The number of : 3

Sharing the same resources to perform tasks can be more efficient than using personal resources to do the same thing. The enjoy mode can save a lot of memory by sharing 1 part of the required objects.

When to use enjoy mode
(1) the application USES many objects;
(2) storing objects in memory will affect memory performance;
(3) most of the specific states (external states) of the object can be placed externally and lightened;
(3) after the external state is removed, the original group of objects can be replaced with fewer Shared objects;
(4) the application does not depend on the object identifier, because the Shared object cannot provide a 1-only identifier.

Related articles: