Common iOS design patterns-prototype and ios Design Patterns

Source: Internet
Author: User

Common iOS design patterns-prototype and ios Design Patterns
1. What is the prototype? (What)

The prototype is a creation design mode. It creates an object quickly by copying it. There are two ways to copy it: 1 .) shortest copy (pointer copy); 2 .) deep copy (value copy); it is more efficient to create a new object in prototype mode.

 

What are deep copy and shallow copy? (What)

Deep copy is also called value copy. The newly created object will open up new space in the memory and copy the value of the object to be copied. Shallow copy is also called pointer copy, the pointer of the newly created object points to the same memory space as the pointer of the original object and has the same value. The light copy object is like the shadow of the original object.

 

NSString * str = @ "AAA ";

NSString * strCopy = [str copy];

NSMutableString * strMCopy = [str mutableCopy];

 

NSLog (@ "str object: % @, address: % p", str, str );

NSLog (@ "strCopy object: % @, address: % p", strCopy, strCopy );

NSLog (@ "strMcopy object: % @, address: % p", strMcopy, strMCopy );

 

09:28:18. 578 Prototype_Demo [516: 303] str object: AAA, address: 0x100001058
09:28:18. 579 Prototype_Demo [516: 303] strCopy object: AAA, address: 0x100001058
09:28:18. 580 Prototype_Demo [516: 303] strMCopy object: AAA, address: 0x10010af70

 

 

2. In what scenarios do I use the prototype? (Where)

1. The object type must be determined at runtime.

2. a copy of an object in a certain state is required.

3. The differences between objects are very small. Use the prototype system to copy an object before making necessary modifications.

 

3. How to Implement the prototype? (How)

1) define a Person class, which contains three Property variables:

 

@ Interface Person: NSObject <NSCopying, NSMutableCopying>

@ Property (nonatomic, copy) NSMutableString * name;

@ Property (nonatomic, copy) NSString * sex;

@ Property (nonatomic, assign) int age;

 

-(Id) initWithName :( NSMutableString *) aName withSex :( NSString *) aSex withAge :( int) aAge;

 

 

2) methods that must be implemented in implementation files and protocols

 

-(Id) initWithName :( NSMutableString *) aName withSex :( NSString *) aSex withAge :( int) aAge {

If (self = [super init]) {

Self. name = aName;

Self. sex = aSex;

Self. age = aAge;

}

Return self;

}

 

// Note that when the object calls the copy method, it will get an immutable object, no matter whether the object is variable or immutable. MutableCopy is called to obtain a variable object, regardless of whether the object is variable or not.

 

-(Id) copyWithZone :( NSZone *) zone

{

Person * obj = [[self class] allocWithZone: zone];

Obj. name = [self. name mutableCopy];

Obj. sex = [self. sex copy];

Obj. age = self. age;

Return obj;

}

 

-(Id) mutableCopyWithZone :( NSZone *) zone {

Person * obj = [[self class] allocWithZone: zone];

Obj. name = [self. name mutableCopy];

Obj. sex = [self. sex copy];

Obj. age = self. age;

Return obj;

}

 

3) use the copy and mutableCopy methods in the main function to create a new object.

NSMutableString * name = [NSMutableString stringWithFormat: @ "AAA"];
Person * person = [[Person alloc] initWithName: name withSex: @ "Boy" withAge: 20];
Person * aPerson = [person copy];
NSLog (@ "% @", aPerson. name );

Person * bPerson = [person mutableCopy];
NSLog (@ "% @", bPerson. sex );


Demo: http://download.csdn.net/detail/luozhonglan/8008379


**************************************** **************************************** **************************************** **************************************** *************************

Presented by Grapefruit




List and explain common design patterns in software development

There are three types of design patterns: creation, structure, and behavior.

The creation types include:

I. Singleton, Singleton mode: ensure that a class has only one instance and provide a global access point to it.

2. Abstract Factory: provides an interface for creating a series of related or mutually dependent objects without specifying their specific classes.

3. Factory Method: Define an interface used to create objects, and let the subclass decide which class to instantiate. Factory Method delays the instantiation of a class to the subclass.

4. Builder: separates the construction of a complex object from its representation, so that different representations can be created during the same construction process.

5. Prototype: Use a Prototype instance to specify the type of the object to be created, and copy the Prototype to create a new object.

Behavior types:

6. Iterator: provides a method to access each element of an aggregate object sequentially without exposing the internal representation of the object.

7. Observer: Observer mode: defines one-to-many dependencies between objects. When the status of an object changes, all objects dependent on it will be automatically updated by notification.

8. Template Method: defines the skeleton of an algorithm in an operation, and delays some steps to the subclass, templateMethod allows the subclass to redefine a specific step without changing the structure of an algorithm.

9. Command: encapsulate a request as an object so that you can parameterize the customer with different requests, queue requests and record request logs, and supports unrecoverable operations.

10. State: allows an object to change its behavior when its internal State changes. The object seems to have changed its class.

11. Strategy: Define a series of algorithms, encapsulate them one by one, and enable them to replace each other. This mode allows algorithms to be independent of customers who use them.

12. China of Responsibility, Responsibility chain mode: Enables multiple objects to process requests to avoid coupling between the request sender and receiver.

13. Mediator: uses an intermediary object to encapsulate object interaction of some columns.

14. Visitor, Visitor mode: indicates an operation that acts on each element in an object structure. It allows you to define new operations that act on this element without changing the element classes.

15th, Interpreter, Interpreter mode: a language is defined to define a representation of its grammar and an Interpreter. This Interpreter uses this representation to explain sentences in the language.

16. Memento: capture the internal state of an object without interrupting the object, and save the state outside the object.
There are:

17. Composite: Composite combines objects into a tree structure to represent the relationship between parts of the whole. Composite makes the use of a single object and a Composite object consistent.

18. Facade, appearance mode: provides a consistent interface for a group of interfaces in the subsystem. fa? Ade provides a high-level interface, which makes the subsystem easier to use.

19. Proxy: provides a Proxy for other objects to control access to this object.

20. Adapter: the Adapter mode converts a class of interfaces into another interface that the customer wants. The Adapter mode makes those classes unable to work together due to interface incompatibility.

21. Decrator: the Decorator mode dynamically adds some additional responsibilities to an object. In terms of the added functions, the Decorator mode is more flexible than the subclass generation mode.

22 ...... the remaining full text>

What are three java design patterns ??

Singleton mode, factory mode, and iteration Mode

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.