The concept of the pattern of privilege
In object-oriented software design, the use of public objects can not only save resources but also improve performance. A shared object can only provide some intrinsic information and cannot be used to identify the object. A design pattern designed to design shareable objects is called the Flyweight pattern.
Implementation of the META mode requires two key components, typically shareable object and save their pool. Some kind of central object maintains the pool and returns the appropriate instance from it.
The use of sharing technology to effectively support a large number of fine-grained objects.
Public transport, such as buses, is more than 100 years old. A large number of passengers in the same direction can share the cost of maintaining and operating vehicles (such as buses). Buses have multiple platforms, and passengers get off the route near their destinations. The cost of reaching the destination is only related to the itinerary. It is much cheaper to take a bus than to keep a car. This is the benefit of using public resources.
In object-oriented software design, we can not only save resources but also improve performance by using common objects. For example, a person needs 1 million instances of a class, but we can share one instance of the class, and put some unique information outside, saving a significant amount of resources (the difference between an instance and 1 million instances). Shared objects provide only some intrinsic information and cannot be used to identify objects. A design pattern specifically designed to design shareable objects is called the "meta pattern."
What is the most important reason to make the privilege object lightweight? Not their size, but the amount of space that can be saved by sharing. The unique state of some objects can be obtained externally, managed elsewhere, and the rest is shared. For example, the original need for a class of 1 million objects, but because the object of this class is a privilege, now only one is enough. This is the reason why the whole system becomes lighter because of the shareable objects that can be shared. With careful design, memory savings are considerable. In iOS development, saving memory means improving overall performance.
Example application of the pattern of the privilege
We created a websitefactory factory class to maintain the objects in the pool, returning various types of specific objects based on the parent type, with the following code:
Copy Code code as follows:
#import <Foundation/Foundation.h>
#import "WebSiteProtocol.h"
@interface Websitefactory:nsobject
@property (nonatomic, strong) Nsdictionary *flyweights; Shared objects
-(id<websiteprotocol>) Getwebsitecategory: (NSString *) Webkey;
-(Nsinteger) Getwebsitecount;
@end
Copy Code code as follows:
#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;
}
@end
The Getwebsitecategory method in the code can return a specific privilege object, and the returned object of this privilege adheres to the Websiteprotocol protocol, and the Websiteprotocol code is as follows:
Copy Code code as follows:
#import <Foundation/Foundation.h>
#import "User.h"
@protocol Websiteprotocol <NSObject>
-(void) Use: (user *) user;
@end
The code for Concretewebsite is as follows:
Copy Code code as follows:
#import <Foundation/Foundation.h>
#import "WebSiteProtocol.h"
@interface Concretewebsite:nsobject <WebSiteProtocol>
@property (nonatomic, copy) NSString *webname;
@end
Copy Code code as follows:
#import "ConcreteWebSite.h"
@implementation Concretewebsite
-(void) Use: (user *) User {
NSLog (@ "site Category:%@ User name:%@", Self.webname, User.username);
}
@end
The user code is as follows:
Copy Code code as follows:
#import <Foundation/Foundation.h>
@interface User:nsobject
@property (nonatomic, copy) NSString *username;
@end
Copy Code code as follows:
#import "User.h"
@implementation User
@end
Now that the code for the meta mode has been completed, let's take a look at how we use the META mode in the client, and the code is as follows:
Copy Code code as follows:
#import "ViewController.h"
#import "WebSiteProtocol.h"
#import "WebSiteFactory.h"
#import "ConcreteWebSite.h"
#import "User.h"
typedef id<websiteprotocol> WEBSITETYPE;
@interface Viewcontroller ()
@end
Copy Code code as follows:
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
Returns a variety of specific objects through the factory method, maintaining the privileges in the pool
Websitefactory *factory = [[Websitefactory alloc] init];
Returns a specific Privilege object
Websitetype type1 = [Factory getwebsitecategory:@ "Home Page"];
User *user1 = [[User alloc] init];
User1.username = @ "John";
The privilege object has a use method
[Type1 Use:user1];
Websitetype type2 = [Factory getwebsitecategory:@ "store"];
User *user2 = [[User alloc] init];
User2.username = @ "Dick";
[Type2 Use:user2];
Websitetype type3 = [Factory getwebsitecategory:@ "case"];
User *user3 = [[User alloc] init];
User3.username = @ "Harry";
[Type3 Use:user3];
Nsinteger count = [Factory Getwebsitecount];
NSLog (@ "Number:%ld", (long) count);
}
-(void) didreceivememorywarning {
[Super didreceivememorywarning];
Dispose of any of the can is recreated.
}
@end
The output is as follows:
2015-09-12 15:59:55.322 flyweightpattern[42020:1723017] site Category: Home user name: John
2015-09-12 15:59:55.322 [42,020:1,723,017] Site Category: Store user name: Dick
2015-09-12 15:59:55.322 flyweightpattern[42020:1723017] Site Category: Case user name: Harry
2015-09-12 15:59:55.323 flyweightpattern[42020:1723017] Number: 3
Sharing the same resources to perform tasks may be more efficient than using individual resources to accomplish the same thing. The element mode can save a lot of memory by sharing a portion of the required objects.
when to use the privilege mode
(1) applications use many objects;
(2) Saving objects in memory can affect memory performance;
(3) The most peculiar state of the object (external state) can be put to the external and lightweight;
(3) After removing the external state, it can replace the original set of objects with fewer shared objects;
(4) The application does not depend on the object label because the shared object does not provide a unique label.