iOS and Java prototype design mode, welcome to scan QR Code to join the subscription number for discussion

Source: Internet
Author: User
Tags shallow copy

    1. What is prototype mode

    2. When to use prototype mode

    3. Prototype patterns have those pros and cons

    4. there are similarities and differences between OC and Java syntax prototype patterns, and a simple example

    • First look at what is prototype mode:

java: Specify the kinds of objects to create using a prototypical instance, and create new objects by copying th Is prototype. (Create the kind of object created with the prototype instance and create a new object to copy from the prototype object.) )

IOS: The client knows the abstract prototype class, and at run time, any object of the abstract prototype subclass can be duplicated as the client wishes, so multiple instances of the same type can be manufactured without manual creation.

As you can see from both definitions, the prototype pattern is to create a new instance by copying the object. The core key is replication.

Let's take a look at the common class diagram for iOS and Java prototyping patterns:

Java:

Ios:

From their prototype pattern, it can be seen that they have a key approach: clone-cloning method. From the definition we are not difficult to draw the prototype pattern is the replication of the Prototpye object.

    • When to use prototype mode

According to the definition can know the core of the prototype pattern is clone or copy, then why don't we just new an object or alloc an object instead of copy to get the object?

First, it is more intuitive to create a more troublesome, not simple new or alloc. Let's look at some of the descriptions in the book first:

    • The object that needs to be created should be independent of its type and how it was created

    • Classes that need to be instantiated are determined at run time.

    • Do not want the factory hierarchy corresponding to the product hierarchy

    • Differences between instances of different classes are only a few combinations of states, so copying the corresponding number of prototypes is easier than creating a manual instance

    • Classes are not easy to create, for example, each component can use other components as a composite object for child nodes. Copying an existing composite object makes it easier to modify a copy

First of all these words are very around the mouth, first find a better understanding of the explanation, need to instantiate the class is at run time to decide, what does that mean, my understanding is that this object is a composite object, such as a tree structure of the object, do not know his specific implementation, also can not create, so I copy a copy.

The difference between the different instances is only a combination of States, this is also a good understanding, such as I draw a small head, but I need a lot of small heads they are just different colors, yellow, white, black, I do not have to re-draw them all, I just need to draw one, the others are copied to him, and then modify the color.

Class is not easy to create, this is better understood, we create a class requires a lot of steps, such as allocating memory, then loading data, data from the database and so on, so copy more resource-saving.

Do not want the factory hierarchy corresponding to the product hierarchy and the objects that need to be created should be independent of their type and how it was created I understand that it would be inappropriate to use new or alloc. For example, a program with a lot of people involved in a programmer may only be responsible for writing one of the pieces, When you try to write a system that exactly fits the dip, you may not be able to avoid creating a new object that conforms to a particular interface, but since the dip principle does not allow you to rely on a specific type, it is completely impossible to decide which specific class to create, and at this point the prototype pattern provides a very flattering approach: " I want to create an object of the same type as it! This is a welcome discussion.

    • Prototype patterns have those pros and cons

The above description is basically the advantage of the prototype model, his disadvantage is that each prototype class needs to implement the Copy method, if an object of the class does not support replication, it will encounter a lot of trouble.

    • examples of the similarities and differences between OC and the Java syntax prototype model

First of all they have a key point-copy, iOS has a depth of copy, of course, there are also Java inside we can use a common diagram to express

In general, the shallow copy is just a copy of the pointer, the resource is not copied, the copied pointer all point to the same memory area, *p modify any one will modify the resources, deep copy is completely two objects, of course, resources are copied.

There is a clone method in the base class object class in Java, which is the key to the prototype pattern. Common source code for Java prototype mode

    • Public class Niuqhclass implements cloneable {

    • @Override

    1. Public MyClass Clone ()

    2. {

    3. Niuqhclass NIUQH = null;

    4. try {

    5. NIUQH = (niuqhclass) super.clone ();

    6. }

    7. catch (Clonenotsupportedexception e)

    8. {

    9. E.printstacktrace ();

    10. }

    11. return NIUQH;

    12. }

The way iOS is implemented is primarily to implement nscopying, overriding Copywithzone:.

or directly on the example: if one day Easy Car network Internet ranked first in the world, need to set up a company in the United States, Britain and Japan, the company structure and now, we look at two implementations of Java and iOS:

Java:

  1. Package MyClass;

  2. /*

  3. * Easy Car NET class

  4. * Just list the CEO and the employees of course the reality is that a tree structure is more complex than this

  5. * This implements the interface cloneable overrides the public Clone () method

  6. * */

  7. public class Yiche implements cloneable {

  8. Public Yiche () {

  9. }

  10. Ceo

  11. Private String CEO;

  12. Private String employee;

  13. @Override

  14. Public Yiche Clone ()

  15. {

  16. Yiche YC = null;

  17. try {

  18. YC = (Yiche) super.clone ();

  19. }

  20. catch (Clonenotsupportedexception e)

  21. {

  22. E.printstacktrace ();

  23. }

  24. return YC;

  25. }

  26. Related Set/get methods

  27. Public String Getceo () {

  28. return CEO;

  29. }

  30. public void Setceo (String CEO) {

  31. This.ceo = CEO;

  32. }

  33. Public String GetEmployee ()

  34. {

  35. return employee;

  36. }

  37. public void Setemployee (String employee) {

  38. This.employee = employee;

  39. }

Scene class

    1. private static int max_count = 3;

    2. public static void Main (string[] args) {

    3. int i = 0;

    4. Define the easy model version.

    5. Yiche YC = new Yiche ();

    6. Yc.setceo ("Li bin"); (CEO only one)

    7. while (I<max_count)

    8. Yiche CLONEYC = Yc.clone (); (Copy a template, add employees)

    9. Yc.setemployee ("random Person"); (employees have a lot of different people)

The result is that three subsidiaries can be sent to the United States of America, Britain and Japan, so it's easier to copy than rewrite a tree object to look at iOS implementations

First of all, iOS must first write an interface (protocol protocol) Iyiche he inherited the NSObject protocol. Notice the difference between the NSObject class and the NSObject protocol, and why Apple does so, and it's not here anymore.

  1. #import <Foundation/Foundation.h>

  2. @protocol Iyiche <NSObject>

  3. @property (Nonatomic,retain) nssting *ceo;

  4. @property (Nonatomic,retain) nssting *employee;

  5. -(ID) copy;

  6. @end

  7. Easy Car Company realized the agreement Iyiche

  8. #import <Foundation/Foundation.h>

  9. #import "IYiche.h"

  10. @interface yiche:nsobject<nscopying,iyiche>

  11. {

  12. @protected

  13. A subsidiary's own example of wanting to add

  14. }

  15. @property (Nonatomic,retain) nssting *ceo;

  16. @property (Nonatomic,retain) nssting *employee;

  17. -(ID) Copywithzone: (Nszone *) zone;

  18. @end

Here's a question why not the copy method in the interface but

-(ID) Copywithzone: (Nszone *) zone; method, first Iyiche implements the Nscopying protocol interface View API there is no copy method in this protocol, this method in the Nscopying class, The Nscopying class accepts the copy message and will want to use the subclass of the Nscopying protocol to forward the message, and the subclass needs to implement the-(ID) copywithzone defined in the Nscopying protocol: (Nszone *) zone; The nscopying protocol does not have a copy method so the Iyiche is declared. Take a look at the implementation of this method

#pragma mark-

#pragma mark Nscopying method

This method needs to be implemented to support the memo

-(ID) Copywithzone: (Nszone *) zone

{

Using the Self class here is a type that you want its subclasses to return as well as subclasses.

Yiche *yichecopy = [[Self class]allocwithzone:zone]init];

return yichecopy;

}

The specific scene class will no longer write, mainly to explain the prototype model of a simple example, and then write the time also feel that there are some shortcomings, feel can understand the things in writing also feel not so sure, this article mainly refer to the OBJECT-C programming, design patterns of cicada, cocoa design mode. If there is no understanding of the place to hope that everyone to put forward their views, to give correct.

Let's talk about iOS programming with Big data:

iOS and Java prototype design mode, welcome to scan QR Code to join the subscription number for discussion

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.