设计模式-原型模式

简介:

设计模式-原型模式

效果:

原型模式,其实就是完整的复制一个对象,以一个对象为样本,进行复制作业,然后再来使用.

以下以复制一个UIView的操作来讲解原型模式的实现

注:UIView对象是不能够复制的,我们需要完整的把UIView对象的参数都复制了后,就行了.

http://stackoverflow.com/questions/4425939/can-uiview-be-copied

Your app probably crashes with something like:

 [UIView copyWithZone:]: unrecognized selector sent to instance 0x1c6280

The reason is that UIView does not implement the copying protocol, and therefore there is no copyWithZone selector in UIView.

因为UIView没有实现复制的协议,所以也就不能复制UIView了.

UIView+Prototype.h 与 UIView+Prototype.m

//
//  UIView+Prototype.m
//  Copy
//
//  Created by YouXianMing on 14/11/14.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "UIView+Prototype.h"

@implementation UIView (Prototype)

- (UIView *)createPrototype {
    UIView *prototypeView = nil;
    
    if (self) {
        // 创建出view
        prototypeView                            = [[UIView alloc] initWithFrame:self.frame];
        
        // 被复制的内容
        prototypeView.transform                  = self.transform;
        prototypeView.contentScaleFactor         = self.contentScaleFactor;
        prototypeView.multipleTouchEnabled       = self.multipleTouchEnabled;
        prototypeView.exclusiveTouch             = self.exclusiveTouch;
        prototypeView.autoresizesSubviews        = self.autoresizesSubviews;
        prototypeView.autoresizingMask           = self.autoresizingMask;
        prototypeView.clipsToBounds              = self.clipsToBounds;
        prototypeView.backgroundColor            = self.backgroundColor;
        prototypeView.alpha                      = self.alpha;
        prototypeView.clearsContextBeforeDrawing = self.clearsContextBeforeDrawing;
        prototypeView.hidden                     = self.hidden;
        prototypeView.contentMode                = self.contentMode;
        prototypeView.tintColor                  = self.tintColor;
        prototypeView.tintAdjustmentMode         = self.tintAdjustmentMode;
    }
    
    return prototypeView;
}

@end


//
//  UIView+Prototype.h
//  Copy
//
//  Created by YouXianMing on 14/11/14.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import <UIKit/UIKit.h>

@interface UIView (Prototype)

/**
 *  创建当前View的原型
 *
 *  @return 当前view的一个新的实例
 */
- (UIView *)createPrototype;

@end

使用:
//
//  ViewController.m
//  Copy
//
//  Created by YouXianMing on 14/11/14.
//  Copyright (c) 2014年 YouXianMing. All rights reserved.
//

#import "ViewController.h"
#import "UIView+Prototype.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
 
    // 作为原型的view
    UIView *redView         = [[UIView alloc] initWithFrame:CGRectMake(50, 50, 100, 100)];
    redView.backgroundColor = [UIColor redColor];
    redView.alpha           = 0.5;
    [self.view addSubview:redView];
    
    // 根据一个原型创建出view
    UIView *greenView         = [redView createPrototype];
    greenView.frame           = CGRectMake(75, 75, 100, 100);
    greenView.backgroundColor = [UIColor greenColor];
    [self.view addSubview:greenView];
    
    // 根据一个原型创建出view
    UIView *blueView         = [greenView createPrototype];
    blueView.frame           = CGRectMake(100, 100, 100, 100);
    blueView.backgroundColor = [UIColor blueColor];
    [self.view addSubview:blueView];
}

@end

需要注意的地方:

目录
相关文章
|
1月前
|
设计模式 安全 Java
【设计模式】原型模式
【设计模式】原型模式
|
2月前
|
设计模式 Java 关系型数据库
23种设计模式 —— 原型模式【克隆羊、浅拷贝、深拷贝】
23种设计模式 —— 原型模式【克隆羊、浅拷贝、深拷贝】
38 1
|
4月前
|
设计模式 Java
Java设计模式【五】:原型模式
Java设计模式【五】:原型模式
17 0
|
5月前
|
设计模式 Java 关系型数据库
认真学习设计模式之原型模式(Prototype Pattern)
认真学习设计模式之原型模式(Prototype Pattern)
38 0
认真学习设计模式之原型模式(Prototype Pattern)
|
6月前
|
设计模式 存储 Java
JAVA设计模式4:谈谈原型模式在JAVA实战开发中的应用
JAVA设计模式4:谈谈原型模式在JAVA实战开发中的应用
|
6月前
|
设计模式 存储 Java
创建型设计模式03-原型模式
创建型设计模式03-原型模式
25 0
|
4月前
|
设计模式 存储
二十三种设计模式全面解析-原型模式进阶之原型管理器:集中管理对象原型的设计模式之道
二十三种设计模式全面解析-原型模式进阶之原型管理器:集中管理对象原型的设计模式之道
|
7天前
|
设计模式 Java
小谈设计模式(10)—原型模式
小谈设计模式(10)—原型模式
|
1月前
|
设计模式 Java
设计模式之原型模式
设计模式之原型模式
|
3月前
|
设计模式 存储 JSON
Java设计模式-原型模式
原型模式也是创建对象的一种方式,它一般用在这样的场景:系统中存在大量相同或相似对象的创建问题,如果用传统的构造函数来创建对象,会比较复杂而且耗费资源。这个时候使用原型模式的克隆方式,能够节省不少时间。比如Java 类中提供的`Object clone()`就是原型模式的应用。
30 1
Java设计模式-原型模式