Public transport (such as a bus) has a history of more than more than 100 years. A large number of passengers in the same direction can share the cost of maintaining and operating a vehicle, such as a bus. Buses have multiple platforms, and passengers alight along the route near their destination. The cost of arriving at the destination is only related to the itinerary. It's 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 use public objects not only to save resources but also to improve performance. For example, a character needs 1 million instances of a class, but we can share an instance of the class with others, and put some unique information outside, and the resources saved can be quite substantial (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 enjoy meta mode.
What is the enjoy meta mode?
Implementing the enjoy meta-mode requires two key components, typically shareable and pooled to save them. Some kind of central object why this pool, and return the appropriate instance from it. The factory is an ideal candidate for this role. It is possible to return various types of specific privilege objects from a factory method, depending on the parent type, whose primary purpose is to why the object is in the pool, and to return the object of the privilege appropriately.
What is the most important reason to make the object of a privilege lightweight? Not their size, but the amount of space that can be saved by sharing. The unique state of some objects can be taken externally, managed elsewhere, and the remainder shared. For example, the original needs a class of 1 million objects, but because the object of this class is to enjoy the yuan, now as long as one is enough. This is due to the fact that the shared meta-object makes the whole system lightweight. By careful design, the memory savings are considerable. In iOS development, saving memory means improving overall performance.
Enjoy meta-mode: Use sharing technology to effectively support a large number of fine-grained objects.
When to use the Enjoy meta mode?
@: Applications use many objects.
@: Saving an object in memory can affect memory performance.
@: Most unique states of an object can be placed externally and lightly quantified.
@: After removing the external state, you can replace the original set of objects with fewer shared objects.
@: The application does not depend on the object identity because the shared object cannot provide a unique identity.
Example application of the enjoy meta-mode
We create a websitefactory factory class to maintain the pool's object of enjoyment, returning the various types of specific objects of privilege, based on the parent type, with the following code:
#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
#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]; } &nbsP; return webset;} - (Nsinteger) Getwebsitecount { return self.flyweights.count;} @end
The Getwebsitecategory method in the code can return a specific element of the object, and the returned object will follow the Websiteprotocol protocol, Websiteprotocol's code is as follows:
#import <Foundation/Foundation.h> #import "User.h" @protocol websiteprotocol <nsobject>-(void) Use: (User * ) User; @end
the code for Concretewebsite is as follows:
#import <Foundation/Foundation.h> #import "WebSiteProtocol.h" @interface Concretewebsite:nsobject < Websiteprotocol> @property (nonatomic, copy) NSString *webname; @end
#import "ConcreteWebSite.h" @implementation concretewebsite-(void) Use: (user *) User {NSLog (@ "site Category:%@ User name:%@", self.we Bname, user.username);} @end
the user code is as follows:
#import <Foundation/Foundation.h> @interface user:nsobject@property (nonatomic, copy) NSString *username; @end
#import "User.h" @implementation user@end
now that the code for the meta-mode has been completed, let's take a look at how the client uses the enjoy meta-mode, 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 () @end @implementation viewcontroller- (void) viewdidload { [super viewDidLoad]; // return a variety of specific object objects through the factory method, maintaining the objects in the pool for the enjoyment of the elements WebSiteFactory *factory = [[WebSiteFactory alloc] init]; // returns the specific websitetype type1 = object [factory getwebsitecategory:@ "Home"]; user *user1 = [[user alloc] init]; user1.username = @ "Zhang San"; // The object has the use method [type1 use:user1]; websitetype type2 = [factory getwebsitecategory:@ "Shop"]; user *user2 = [[user alloc ] init]; user2.username = @ "John Doe"; [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 resources that can be recreated.} @end
The output is as follows:
2015-09-12 15:59:55.322 flyweightpattern[42020:1723017] site Category: Home user name: Zhang San 2015-09-12 15:59:55.322 flyweightpattern[ 42,020:1,723,017] Website Category: Store user name: John Doe 2015-09-12 15:59:55.322 flyweightpattern[42020:1723017] Website Category: Case user name: Harry 2015-09-12 15:59:55.323 flyweightpattern[42020:1723017] Number: 3
sharing the same resources to perform tasks can be more efficient than using personal resources to accomplish the same thing. The enjoy meta-mode can save a lot of memory by sharing a subset of the required objects.
Demo Link Address: https://github.com/guoshimeihua/FlyweightPattern.git
iOS design mode--Enjoy meta mode