Problem: Put the elephant in the refrigerator
I. process-oriented
Focus on business functions
1. open the refrigerator door
2. Put the elephant in the refrigerator
3. Close the refrigerator door
Ii. Object-oriented
Focus on functional encapsulation and layers
1. Object 1: Refrigerator
Function: opens the refrigerator door, puts in something, and closes the refrigerator door.
2. Object 2: elephant
Code Demonstration:
1 // 2 // fridge. m 3 // object-oriented 4 // 5 // created by MACOs on 14-8-10. 6 // copyright (c) 2014 MACOs. all rights reserved. 7 // 8 9 # import "fridge. H "10 11 @ implementation fridge12 @ synthesize name, opend, array; 13-(ID) initwidthname :( nsstring *) iname14 {15 if (Self = [Super init]) {16 self. name = INAME; 17 opend = false; 18 self. array = [[nsmutablearray alloc] init]; 19 nslog (@ "initialize a device: % @ Refrigerator", self. name); 20} 21 return self; 22} 23 24-(bool) opendoor25 {26 if (opend) {27 nslog (@ "the door has been opened, open "); 28} 29 else30 {31 opend = true; 32 nslog (@" successfully opened the door of the refrigerator "); 33} 34 return opend; 35} 36 37-(bool) Putin :( ID) something38 {39 if (opend) {40 [array addobject: Something]; 41 nslog (@ "% @ into the refrigerator, number of items % lu ", something, [array count]); 42} 43 else44 {45 nslog (@" % @ cannot be placed in the refrigerator, number of items % lu: the refrigerator door is not opened ", something, [array count]); 46} 47 return opend; 48} 49 50-(bool) closedoor51 {52 If (opend) {53 opend = false; 54 nslog (@ "successfully closing the refrigerator door"); 55} 56 else57 {58 nslog (@ "unable to close the refrigerator door: the door has been closed "); 59} 60 return opend; 61} 62 @ end
1 // 2 // elephant. m 3 // object-oriented 4 // 5 // created by MACOs on 14-8-10. 6 // copyright (c) 2014 MACOs. all rights reserved. 7 // 8 9 # import "elephant. H "10 11 @ implementation elephant12 @ synthesize name; 13 14-(ID) initwidthname :( nsstring *) iname15 {16 nslog (@" initialize one end: % @ elephant ", INAME ); 17 if (Self = [Super init]) {18 self. name = INAME; 19} 20 return self; 21} 22 23-(nsstring *) description24 {25 return [[nsstring alloc] initwithformat: @ "[% @ Elephant]", self. name]; 26} 27 @ end
1 // 2 // main. m 3 // oopdemo 4 // 5 // created by MACOs on 14-8-10. 6 // copyright (c) 2014 MACOs. all rights reserved. 7 // Object-Oriented Design Mode 8 9 # import <Foundation/Foundation. h> 10 # import "fridge. H "11 # import" elephant. H "12 13 int main (INT argc, const char * argv []) 14 {15 16 @ autoreleasepool {17 fridge * fridge = [[fridge alloc] initwidthname: @ "Haier"]; 18 elephant * elephant = [[elephant alloc] initwidthname: @ "South America"]; 19 [fridge opendoor] & 20 fridge. opend & 21 [fridge Putin: Elephant] & 22 [fridge closedoor]; 23} 24 return 0; 25}
1 04:55:47. 890 oopdemo [2625: 303] initializes a Haier refrigerator 2 04:55:48. 029 oopdemo [2625: 303] initialize a head: South America elephant 3 04:55:48. 036 oopdemo [2625: 303] successfully opened the refrigerator door 4 04:55:48. 047 oopdemo [2625: 303] [South America Elephant] was put into the refrigerator successfully. The number of items was 15 04:55:48 on February 10. 053 oopdemo [2625: 303] the refrigerator door is successfully closed 6 program ended with exit code: 0
Execution result