版本記錄
版本號 | 時間 |
---|---|
V1.0 | 2017.04.29 |
前言
前面講了23種設計模式中的前幾個,下面我們繼續(xù)试读,先看前幾篇文章杠纵。
1. 23種設計模式(一)
2. 23種設計模式(二)
3. 23種設計模式(三)
4. 23種設計模式(四)
5. 23種設計模式(五)
6. 23種設計模式(六)
7. 23種設計模式(七)
8. 23種設計模式(八)
9. 23種設計模式(九)
10. 23種設計模式(十)
詳述
二十一、享元模式——Flyweight
??使用共享技術有效地支持大量細粒度的對象钩骇。先看一下原理圖比藻。
下面看一下代碼組織結構。
看代碼倘屹。
1. ViewController.m
#import "ViewController.h"
#import "HCDWebSiteFactory.h"
#import "HCDWebSite.h"
#import "HCDConcreteWebSite.h"
#import "HCDUser.h"
typedef id<HCDWebSite> HCDWebSiteType;
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
HCDWebSiteFactory *facoty = [[HCDWebSiteFactory alloc]init];
HCDWebSiteType fx = [facoty getWebSiteCategory:@"產品展示"];
HCDUser *user = [[HCDUser alloc]init];
user.name = @"小菜";
[fx use:user];
HCDWebSiteType fy = [facoty getWebSiteCategory:@"產品展示"];
HCDUser *user1 = [[HCDUser alloc]init];
user1.name = @"大鳥";
[fy use:user1];
HCDWebSiteType fz = [facoty getWebSiteCategory:@"博客"];
HCDUser *user2 = [[HCDUser alloc]init];
user2.name = @"咪咪";
[fz use:user2];
NSInteger count = [facoty getWebSiteCount];
NSLog(@"個數(shù):%ld",count);
}
@end
2. HCDUser.h
#import <Foundation/Foundation.h>
@interface HCDUser : NSObject
@property (nonatomic, strong) NSString *name;
@end
3. HCDUser.m
#import "HCDUser.h"
@implementation HCDUser
@end
4. HCDWebSite.h
#import <Foundation/Foundation.h>
#import "HCDUser.h"
@protocol HCDWebSite <NSObject>
- (void)use:(HCDUser *)user;
@end
5. HCDConcreteWebSite.h
#import <Foundation/Foundation.h>
#import "HCDWebSite.h"
@interface HCDConcreteWebSite : NSObject <HCDWebSite>
@property (nonatomic, strong) NSString *webName;
@end
6. HCDConcreteWebSite.m
#import "HCDConcreteWebSite.h"
@implementation HCDConcreteWebSite
- (void)use:(HCDUser *)user
{
NSLog(@"網(wǎng)站分類:%@,用戶:%@",self.webName,user.name);
}
@end
7. HCDWebSiteFactory.h
#import <Foundation/Foundation.h>
#import "HCDWebSite.h"
@interface HCDWebSiteFactory : NSObject
@property (nonatomic, strong) NSDictionary *flyweights;
- (id<HCDWebSite> )getWebSiteCategory:(NSString *)webkey;
- (NSInteger)getWebSiteCount;
@end
8. HCDWebSiteFactory.m
#import "HCDWebSiteFactory.h"
#import "HCDConcreteWebSite.h"
@implementation HCDWebSiteFactory
- (instancetype)init
{
self = [super init];
if (self) {
_flyweights = [NSDictionary dictionary];
}
return self;
}
- (id<HCDWebSite> )getWebSiteCategory:(NSString *)webkey
{
__block id<HCDWebSite> webset = nil;
[self.flyweights enumerateKeysAndObjectsUsingBlock:^(id key, id obj, BOOL *stop) {
if (webkey == key) {
webset = obj;
*stop = YES;
}
}];
if (webset == nil) {
HCDConcreteWebSite *concreteset = [[HCDConcreteWebSite alloc] init];
concreteset.webName = webkey;
webset = concreteset;
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
看結果银亲。
2017-04-29 15:34:54.942 21享元模式[2818:103728] 網(wǎng)站分類:產品展示,用戶:小菜
2017-04-29 15:34:54.942 21享元模式[2818:103728] 網(wǎng)站分類:產品展示,用戶:大鳥
2017-04-29 15:34:54.943 21享元模式[2818:103728] 網(wǎng)站分類:博客,用戶:咪咪
2017-04-29 15:34:54.943 21享元模式[2818:103728] 個數(shù):2
結論:注重思想。
二十二纽匙、解釋器模式——Interpreter
??給定一個語言务蝠,定義它的文法的一種表示,并定義一個解釋器烛缔,這個解釋器使用該表示中解釋語言匯中的句子馏段。
下面看一下代碼組織結構。
下面看代碼践瓷。
1. ViewController.m
#import "ViewController.h"
#import "HCDContext.h"
#import "HCDTerminalExpression.h"
#import "HCDNonterminalExpression.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
HCDContext *context = [[HCDContext alloc]initWithInput:@"12345"];
NSMutableArray *list = [[NSMutableArray alloc]init];
[list addObject:[[HCDTerminalExpression alloc]init]];
[list addObject:[[HCDNonterminalExpression alloc]init]];
[list addObject:[[HCDTerminalExpression alloc]init]];
[list addObject:[[HCDTerminalExpression alloc]init]];
for(HCDAbstractExpression *exp in list) {
[exp interpret:context];
}
}
@end
2. HCDContext.h
#import <Foundation/Foundation.h>
@interface HCDContext : NSObject
- (instancetype)initWithInput:(NSString *)input;
@property (nonatomic, copy) NSString *input;
@property (nonatomic, copy) NSString *output;
@end
3. HCDContext.m
#import "HCDContext.h"
@implementation HCDContext
- (instancetype)initWithInput:(NSString *)input
{
self = [super init];
if (self) {
_input = input;
}
return self;
}
- (NSString *)output
{
return [NSString stringWithFormat:@"輸入的是%@",_input];
}
@end
4. HCDAbstractExpression.h
#import <Foundation/Foundation.h>
#import "HCDContext.h"
@interface HCDAbstractExpression : NSObject
- (void)interpret:(HCDContext *)context;
@end
5. HCDAbstractExpression.m
#import "HCDAbstractExpression.h"
@implementation HCDAbstractExpression
- (void)interpret:(HCDContext *)context
{
return;
}
@end
6. HCDTerminalExpression.h
#import "HCDAbstractExpression.h"
@interface HCDTerminalExpression : HCDAbstractExpression
@end
7. HCDTerminalExpression.m
#import "HCDTerminalExpression.h"
@implementation HCDTerminalExpression
- (void)interpret:(HCDContext *)context
{
NSLog(@"終端解釋器,context == %@",context.output);
}
@end
8. HCDNonterminalExpression.h
#import "HCDAbstractExpression.h"
@interface HCDNonterminalExpression : HCDAbstractExpression
@end
9. HCDNonterminalExpression.m
#import "HCDNonterminalExpression.h"
@implementation HCDNonterminalExpression
- (void)interpret:(HCDContext *)context
{
NSLog(@"非終端解釋器,context == %@",context.output);
}
@end
看結果院喜。
2017-04-29 15:49:05.226 22解釋器模式[3116:115594] 終端解釋器,context == 輸入的是12345
2017-04-29 15:49:05.228 22解釋器模式[3116:115594] 非終端解釋器,context == 輸入的是12345
2017-04-29 15:49:05.228 22解釋器模式[3116:115594] 終端解釋器,context == 輸入的是12345
2017-04-29 15:49:05.229 22解釋器模式[3116:115594] 終端解釋器,context == 輸入的是12345
結論:思想。
二十三晕翠、訪問者模式——Visitor
??表示一個作用于某對象結構中的各元素的操作喷舀,它使你可以在不改變各元素的類的前提下定義作用于這些元素的新操作。先看一下代碼組織結構崖面。
下面看代碼元咙。
1. ViewController.m
#import "ViewController.h"
#import "HCDObjectStructure.h"
#import "HCDConcreteElementB.h"
#import "HCDConcreteElementA.h"
#import "HCDConcreteVisitor1.h"
#import "HCDConcreteVisitor2.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
HCDObjectStructure *o = [[HCDObjectStructure alloc]init];
HCDConcreteElementA *eA = [HCDConcreteElementA new];
HCDConcreteElementB *eB = [HCDConcreteElementB new];
[o attach:eA];
[o attach:eB];
HCDConcreteVisitor1
*v1 = [HCDConcreteVisitor1 new];
HCDConcreteVisitor2
*v2 = [HCDConcreteVisitor2 new];
[o accept: v1];
[o accept: v2];
}
@end
2. HCDVisitors.h
#import <Foundation/Foundation.h>
@class HCDConcreteElementA,HCDConcreteElementB;
@interface HCDVisitors : NSObject
- (void)VisitConcreteElementA:(HCDConcreteElementA *)concreteElementA;
- (void)VisitConcreteElementB:(HCDConcreteElementB *)concreteElementB;
@end
3. HCDVisitors.m
#import "HCDVisitors.h"
#import "HCDConcreteElementA.h"
#import "HCDConcreteElementB.h"
@implementation HCDVisitors
- (void)VisitConcreteElementA:(HCDConcreteElementA *)concreteElementA
{
return;
}
- (void)VisitConcreteElementB:(HCDConcreteElementB *)concreteElementB
{
return;
}
@end
4. HCDConcreteVisitor1.h
#import "HCDVisitors.h"
@interface HCDConcreteVisitor1 : HCDVisitors
@end
5. HCDConcreteVisitor1.m
#import "HCDConcreteVisitor1.h"
#import "HCDConcreteElementA.h"
@implementation HCDConcreteVisitor1
- (void)VisitConcreteElementA:(HCDConcreteElementA *)concreteElementA
{
NSString *eleName = NSStringFromClass([concreteElementA class]);
NSString *visitorName = NSStringFromClass([self class]);
NSLog(@"%@被%@訪問",eleName, visitorName);
}
@end
6. HCDConcreteVisitor2.h
#import "HCDVisitors.h"
@interface HCDConcreteVisitor2 : HCDVisitors
@end
7. HCDConcreteVisitor2.m
#import "HCDConcreteVisitor2.h"
#import "HCDConcreteElementB.h"
@implementation HCDConcreteVisitor2
- (void)VisitConcreteElementB:(HCDConcreteElementB *)concreteElementB
{
NSString *eleName = NSStringFromClass([concreteElementB class]);
NSString *visitorName = NSStringFromClass([self class]);
NSLog(@"%@被%@訪問",eleName, visitorName);
}
@end
8. HCDElements.h
#import <Foundation/Foundation.h>
@class HCDVisitors;
@interface HCDElements : NSObject
- (void)accept:(HCDVisitors *)visitor;
@end
9. HCDElements.m
#import "HCDElements.h"
@implementation HCDElements
- (void)accept:(HCDVisitors *)visitor
{
return;
}
@end
10. HCDConcreteElementA.h
#import "HCDElements.h"
@interface HCDConcreteElementA : HCDElements
- (void)operationA;
@end
11. HCDConcreteElementA.m
#import "HCDConcreteElementA.h"
#import "HCDVisitors.h"
@implementation HCDConcreteElementA
- (void)operationA
{
return;
}
- (void)accept:(HCDVisitors *)visitor
{
[visitor VisitConcreteElementA:self];
}
@end
12. HCDConcreteElementB.h
#import "HCDElements.h"
@interface HCDConcreteElementB : HCDElements
- (void)operationB;
@end
13. HCDConcreteElementB.m
#import "HCDConcreteElementB.h"
#import "HCDVisitors.h"
@implementation HCDConcreteElementB
- (void)operationB
{
return;
}
- (void)accept:(HCDVisitors *)visitor
{
[visitor VisitConcreteElementB:self];
}
@end
14. HCDObjectStructure.h
#import <Foundation/Foundation.h>
@class HCDElements;
@class HCDVisitors;
@interface HCDObjectStructure : NSObject
{
NSMutableArray *elements;
}
- (void)attach:(HCDElements *)element;
- (void)detach:(HCDElements *)element;
- (void)accept:(HCDVisitors *)visitor;
@end
15. HCDObjectStructure.m
#import "HCDObjectStructure.h"
#import "HCDElements.h"
#import "HCDVisitors.h"
@implementation HCDObjectStructure
- (instancetype)init
{
self = [super init];
if (self) {
elements = [[NSMutableArray alloc]init];
}
return self;
}
- (void)attach:(HCDElements *)element
{
[elements addObject:element];
}
- (void)detach:(HCDElements *)element
{
[elements removeObject:element];
}
- (void)accept:(HCDVisitors *)visitor
{
for (HCDElements *e in elements) {
[e accept:visitor];
}
}
@end
看結果。
2017-04-29 16:43:15.989 23訪問者模式[3771:148226] HCDConcreteElementA被HCDConcreteVisitor1訪問
2017-04-29 16:43:15.990 23訪問者模式[3771:148226] HCDConcreteElementB被HCDConcreteVisitor2訪問
結論:注意思想巫员。
后記
?? 23種設計模式庶香,寫了好幾天,今天總算完事了简识,祝自己五一快樂吧赶掖,今天過得還算有意義感猛,喜歡我寫的就點個贊吧,謝謝大家奢赂。再次祝大家五一快樂哦E惆住!I旁睢咱士!