Objective-C compound and objective-c COMPOUND

Source: Internet
Author: User

Objective-C compound and objective-c COMPOUND

I want to talk about Composite

Learning faster with questions

What is composite? What are the advantages? When to use composite? What is dependency? How can this problem be solved?

Composite is to combine some individual components to form a whole. (One class defines another class as an instance variable)

Advantages:

1. Reference other objects to the newly created objects to form a new whole and provide more powerful functions.

2. Low Coupling Degree (relative to inheritance)

Inheritance is "is a", and combination is "has a". For example, a computer has a keyboard, a car has a engine, and so on.

Dependency: the relationship between one class and another class or multiple classes

For example, A. H file is imported into B. H file, and B. H file is imported into C. H file, so that an association is established between the three files.

If A new instance variable is added to the C. h file, the B. h and A. h files will be re-compiled to accept this change. This is the dependency.

Replace # import with @ class, and @ class tells the compiler that this is a class that can be safely used. The Compiler does not need to know the content in this class, but only needs to know that it is a class, it can be referenced by pointers.

 

Next we will use an instance to learn how to use composite.

Automobile, Engine, and Tire are used together to complete automobile assembly.

Create a project without moving the main function. Create an Engine class.

. H file does not move it

 

Create Tire class

Same as engine class

Create a car

@ Class is used here, and the engine class and tire class are regarded as instance variables. You can use @ class without calling anything in the class. Otherwise, you need to use # import

Assemble the car here

Instantiate in the Car. m file

Implement output in the main function

 

This is just a simple compound example. If you want to dive deep into it, you can write it by yourself. For example, four tires of a car have their own different positions, when you access a Tire on a Car, you must specify the specific position of the Tire. Therefore, you must rewrite setTire :( Tire *) newTire andIndex :( int) in the Car) the setter method with an index also needs to add the getter method with an index. Note that you should add if to determine the range of tire values.

 

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.