iOS design mode-prototypes
Schematic diagram
Description
1. prototype mode to copy the prototype and create a new object
2. The differences between instances of different classes are only a few combinations of States, so copying prototypes is more convenient than manual instantiation
3. Class is not easy to create, each component can use other components as a sub-node of the composite object, copying the existing composite object is easier than the copy modification
Source
Https://github.com/YouXianMing/PrototypePattern
////BaseCopyObject.h//Prototypepattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import<Foundation/Foundation.h>@interfaceBasecopyobject:nsobject <NSCopying>/** * ==> subclass do not override this method <== * * Copy operation * * @param zone * * @return newly copied objects*/- (ID) Copywithzone: (Nszone *) zone;/** * ==> overrides <== * * Parameter Assignment operation * * @param object for basecopyobject copy operation*/- (void) Parameterscopyoperationwithbasecopyobject: (Basecopyobject *) Basecopyobject;@end
////BASECOPYOBJECT.M//Prototypepattern////Created by youxianming on 15/7/29.//Copyright (c) 2015 youxianming. All rights reserved.//#import "BaseCopyObject.h"@implementationBasecopyobject- (ID) Copywithzone: (Nszone *) Zone {basecopyobject*copy = [Selfclass] Allocwithzone:zone]; [Self parameterscopyoperationwithbasecopyobject:copy]; returncopy;}- (void) Parameterscopyoperationwithbasecopyobject: (Basecopyobject *) Basecopyobject {//overridden by subclasses}@end
Analysis
The abstract base class follows the replication protocol and implements the basic replication functionality
Instantiated classes inherit the replication functionality by inheriting the abstract base class
* Make a deep copy of the dictionary and array
* The array and dictionary have completed a deep copy
The objects in the array (or objects in the dictionary) are required to implement the Nscopying protocol when implementing the copy, or they can inherit the base class that implements the Nscopying protocol.
iOS design mode-prototypes