C语言实现《大话设计模式》中的代理模式例程.pdf
文本预览下载声明
C 语言实现《大话设计模式》中的代理模式例程
分类:设计模式2012-06-12 11:07 545 人阅读评论(0) 收藏举报
设计模式语言cstructfunction
[cpp] view plaincopyprint?
1. #ifndef __PROXY_H__
2. #define __PROXY_H__
3. #include rtthread.h
4. #include finsh.h
5. //被追求者类
6. typedef struct _SchoolGirl SchoolGirl;
7. struct _SchoolGirl
8. {
9. char *Name;
10. void (*SchoolGirlDelete)(SchoolGirl *pSchoolGirl);
11. };
12. static void SchoolGirlDelete(SchoolGirl *pSchoolGirl)
13. {
14. rt_free(pSchoolGirl);
15. }
16. SchoolGirl *SchoolGirlCreate(char *Name)
17. {
18. SchoolGirl *pSchoolGirl = (SchoolGirl *)rt_malloc(sizeof (SchoolGirl));
19. pSchoolGirl-Name = Name;
20. pSchoolGirl-SchoolGirlDelete = SchoolGirlDelete;
21. return pSchoolGirl;
22. }
23. //追求者类
24. typedef struct _Pursuit Pursuit;
25. struct _Pursuit
26. {
27. SchoolGirl *mm;
28. void (*PursuitDelete)(void *pPursuit);
29. void (*GiveDolls)(Pursuit *pPursuit);
30. void (*GiveFlowers)(Pursuit *pPursuit);
31. void (*GiveChocolate)(Pursuit *pPursuit);
32. };
33. static void PursuitDelete(void *pPursuit)
34. {
35. rt_free(pPursuit);
36. }
37. static void PursuitGiveDolls(Pursuit *pPursuit)
38. {
39. rt_kprintf( %s, 送你洋娃娃\n, pPursuit-mm-Name);
40. }
41. static void PursuitGiveFlowers(Pursuit *pPursuit)
42. {
43. rt_kprintf( %s, 送你鲜花\n, pPursuit-mm-Name);
44. }
45. static void PursuitGiveChocolate(Pursuit *pPursuit)
46. {
47. rt_kprintf( %s, 送你巧克力\n, pPursuit-mm-Name);
48. }
49. Pursuit *PursuitCreate(SchoolGirl *mm)
50. {
51. Pursuit *pPursuit = (Pursuit *)rt_malloc(sizeof(Pursuit));
52. pPursuit-mm = mm;
53. pPursuit-PursuitDelete = PursuitDelete;
54. pPursuit-GiveDolls = PursuitG
显示全部