OBJECTIVE-C Composite

Source: Internet
Author: User

The next thing I'm going to talk about is compounding.

Learning with questions will be quicker

What is a compound? What are the advantages? When do you use the compound? What is a dependency relationship? How to solve?

Compounding is the combination of individual components to form a whole. (One class defines another class as an instance variable)

Advantages:

1. Refer other objects to the newly created object, forming a new whole and more powerful.

2. Less coupling (relative to inheritance)

Inheritance is the "is a" is a, composite is "have a" has a, such as a computer has a keyboard, the car has an engine and so on can be used to compound

Dependency: A relationship between a class and another class or multiple classes

For example, A.h file import B.h file, B.h file import C.h file, so three files to establish an association

If a new instance variable is added to the C.h file, then the B.h,a.h file will be recompiled to accept the change, which is the dependency

With @class instead of #import, the @class is to tell the compiler that this is a class that can be used with ease, and the compiler doesn't need to know what's inside the class, but just know it's a class that can be referenced by pointers

Let's use an example to understand how the composite is used

Lift a car, car, engine, Tyre (Tire), use a composite to complete the assembly of a car

Create a project, the main function does not move it, build the engine class

The. h file does not move it

Creating the Tire Class

Just like the engine class.

Create a Car class

Here is used to @class, the engine class and tire class as an instance variable, do not call the contents of the class can be used @class otherwise use #import

We'll assemble the car here.

Instantiation in CAR.M file

Implement the output in the main function

This is only a very simple composite example, if you want to go into the reader can write their own, such as the car's four tires have their own different positions, when visiting a car on a tyre to specify the exact location of the tyre, so in car also rewrite Settire: (tire*) Newtire Andindex: (int) Index setter method, also add indexed getter method, need to pay attention to add if judge the range of tyre value.

OBJECTIVE-C Composite

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.