-------------------- <A href = "http://www.itheima.com" target = "blank"> ASP. NET + unity development </a>,
<A href = "http://www.itheima.com" target = "blank">. Net training </a>. We look forward to communicating with you! -----------------------
Delegation mode-categories, protocols, and blocks
Delegate. What is a delegate? XXX is a common form of entrusting people to do things. From here we can know that the entrusting is done by both parties. The delegation in OC is often used together with the protocol. The definition of delegation is the design pattern that one class specifies another class to process certain tasks. These days I have learned about classes, protocols (both formal protocols), and code blocks. These knowledge points are closely related to delegation, so today I tried to write a program with proxy mode (delegate. The content of the program is as follows: there is a child, a nanny, and a mother who uses the proxy mode (delegated) to handle the situation when the child cries.
The first thing I think of is the protocol. Because the teacher talks about the protocol used in the proxy mode (delegation), the protocol is used first to implement this problem. To solve this problem, let's take a look. I try to use object-oriented thinking to solve this problem. What are the first objects in this question? There are mothers: Mother, children: Baby, and babysitter: babynurse. There are three classes for this problem. What are their respective contents? For the children in this issue, I cried like this: cry. In order to make comparisons, I gave the children a smile: smile. Second, mom should have the right to have a child and a designated nanny, and have the obligation to make some treatment when the child is crying. So I set two member variables for MY MOTHER: a child: Baby, one is that it will handle the crying nanny: delegatemother. For the action to handle a child crying: findbabycry. What will the nanny do? Will do housework: dohomework. But this is not what we need here. We need to take care of the crying children. So she had to learn how to cope with the actions of crying children, such as feeding: Nurse and playing with her: playwithbaby. These actions are not required by all nannies, so they are encapsulated with the protocol. After knowing this, we will come up with an algorithm for solving this problem ). When the child is crying, the mother finds this action, and it will entrust the nanny who will handle the crying child to deal with it (maybe as long as the mouth is changed: the child is crying, you can see). Then the nanny will put the skill it learned to coax the child, and then the child will smile. For details, see the code.
Header file of the Child baby class:
@ Interface baby: nsobject
-(Void) cry;
-(Void) smile;
@ End
Mother Class header file:
@ Interface MOTHER: nsobject
@ Property (retain, nonatomic) baby * baby;
@ Property (retain, nonatomic) babynurse <makebabyhappy> * delegatemother;
// When mother find baby crying, delegatemother will works
-(Void) findbabycry;
@ End
Nanny babynurse header file:
@ Interface babynurse: nsobject <makebabyhappy>
-(Void) dohomework;
-(Void) copewithbabycrying: (baby *) baby;
@ End
Makebabyhappy protocol file:
@ Protocol makebabyhappy <nsobject>
@ Optional
// Nurse to baby
-(Void) Nurse;
// Play with baby
-(Void) playwithbaby;
@ End
At the core of the proxy mode is how Mom finds the child crying: findbabycry
// When mother find baby crying, delegatemother will works
-(Void) findbabycry
{
Nslog (@ "I am in mother -----");
[Self. Baby cry];
[Self. delegatemother copewithbabycrying: _ baby]; // just call the nanny.
}
The above is a proxy mode (delegated) program completed using the Agreement. In fact, the idea is still very consistent with the real life, and now it is not a nanny with children, the mother entrusts the children to the nanny for care. It is a nanny's business to take care of the children. For the above process completed by using the protocol, the informal protocol can also be left. What is the informal agreement? As mentioned in the previous blog, informal Protocols refer to the nsobject category. Therefore, we can achieve what the nanny has to do through non-positive agreements, and we can also accomplish what we want, so that the children will not cry. Although the delegation method is implemented by using this informal protocol, unlike the delegation by both parties in the proxy mode, it is more like a mother learning to get the skills such as kids. This skill is the nsobject category: makebabyhappy, which contains the cool child skill: Feeding: Nurse, playing with her: playwithbaby.
@ Interface nsobject (makebabyhappy)
// Nurse to baby
-(Void) Nurse;
// Play with baby
-(Void) playwithbaby;
@ End
Mom mother1 implementation (also core ):
@ Implementation mother1
-(Void) findbabycry
{
Nslog (@ "I am a mother with genius about take care baby ");
[Self. babycry];
[Selfnurse];
[Selfplaywithbaby];
[Self. babysmile];
}
@ End
As for using block, I directly add the Code:
There are
Nanny nurse class implementation:
@ Implementation nurse
-(Void) copebabycryingusingblock: (void (^) () block
{
Nslog (@ "I am a nurse ");
Block ();
}
@ End
Mom mother2 class implementation:
-(Void) findbabycrying
{
[Self. babycry];
// If nurse is null, program will not error. It will skip below statement
[Self. nursecopebabycryingusingblock: ^ {
Nslog (@ "He must be hungry, you need to nurse some milk ");
// You Can other things about making baby happy
}];
[Self. babysmile];
}
We can see that the implementation of "Block" is in the mother class and inside the delegate object. The advantage is that the Code is more intensive.
-------------------- <A href = "http://www.itheima.com" target = "blank"> ASP. NET + unity development </a>,
<A href = "http://www.itheima.com" target = "blank">. Net training </a>. We look forward to communicating with you! -----------------------
Commissioned models for dark horse programmers-categories, protocols, and blocks