23种设计模式(十一)

版本记录

版本号 时间
V1.0 2017.04.29

前言

前面讲了23种设计模式中的前几个,下面我们继续,先看前几篇文章。
1. 23种设计模式(一)
2. 23种设计模式(二)
3. 23种设计模式(三)
4. 23种设计模式(四)
5. 23种设计模式(五)
6. 23种设计模式(六)
7. 23种设计模式(七)
8. 23种设计模式(八)
9. 23种设计模式(九)
10. 23种设计模式(十)

详述

二十一、享元模式——Flyweight

  使用共享技术有效地支持大量细粒度的对象。先看一下原理图。

23种设计模式(十一)_第1张图片
享元模式原理图

下面看一下代码组织结构。

23种设计模式(十一)_第2张图片
享元模式代码结构图

看代码。

1. ViewController.m

#import "ViewController.h"
#import "HCDWebSiteFactory.h"
#import "HCDWebSite.h"
#import "HCDConcreteWebSite.h"
#import "HCDUser.h"

typedef id 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(@"个数:%ld",count);
}

@end


2. HCDUser.h
#import 

@interface HCDUser : NSObject

@property (nonatomic, strong) NSString *name;

@end


3. HCDUser.m

#import "HCDUser.h"

@implementation HCDUser

@end

4. HCDWebSite.h

#import 
#import "HCDUser.h"

@protocol HCDWebSite 

- (void)use:(HCDUser *)user;

@end


5. HCDConcreteWebSite.h
#import 
#import "HCDWebSite.h"

@interface HCDConcreteWebSite : NSObject 

@property (nonatomic, strong) NSString *webName;

@end


6. HCDConcreteWebSite.m

#import "HCDConcreteWebSite.h"

@implementation HCDConcreteWebSite

- (void)use:(HCDUser *)user
{
    NSLog(@"网站分类:%@,用户:%@",self.webName,user.name);
}

@end


7. HCDWebSiteFactory.h

#import 
#import "HCDWebSite.h"

@interface HCDWebSiteFactory : NSObject

@property (nonatomic, strong) NSDictionary *flyweights;

- (id )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 )getWebSiteCategory:(NSString *)webkey
{
    __block id 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] 网站分类:产品展示,用户:小菜
2017-04-29 15:34:54.942 21享元模式[2818:103728] 网站分类:产品展示,用户:大鸟
2017-04-29 15:34:54.943 21享元模式[2818:103728] 网站分类:博客,用户:咪咪
2017-04-29 15:34:54.943 21享元模式[2818:103728] 个数:2

结论:注重思想。


二十二、解释器模式——Interpreter

  给定一个语言,定义它的文法的一种表示,并定义一个解释器,这个解释器使用该表示中解释语言汇中的句子。

下面看一下代码组织结构。

23种设计模式(十一)_第3张图片
代码组织结构

下面看代码。

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 

@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 
#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

  表示一个作用于某对象结构中的各元素的操作,它使你可以在不改变各元素的类的前提下定义作用于这些元素的新操作。先看一下代码组织结构。

23种设计模式(十一)_第4张图片
代码结构

下面看代码。


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 

@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 

@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 

@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种设计模式,写了好几天,今天总算完事了,祝自己五一快乐吧,今天过得还算有意义,喜欢我写的就点个赞吧,谢谢大家。再次祝大家五一快乐哦!!!!

23种设计模式(十一)_第5张图片
大海

你可能感兴趣的:(23种设计模式(十一))