码迷,mamicode.com
首页 > 其他好文 > 详细

设计模式学习——代理模式(Proxy Pattern)

时间:2017-08-13 21:15:50      阅读:210      评论:0      收藏:0      [点我收藏+]

标签:ios   std   运行   blog   proxy   image   活动   main   编译   

放假啦~学生们要买车票回家了,有汽车票、火车票,等。但是,车站很远,又要考试,怎么办呢?找代理买啊,虽然要多花点钱,但是,说不定在搞活动,有折扣呢~

技术分享

 

 1  ///
 2  /// @file    Selling_Tickets.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 20:35:28
 5  ///
 6 
 7 #ifndef __SELLING_TICKETS_H__
 8 #define __SELLING_TICKETS_H__
 9 
10 #include <iostream>
11  
12 namespace marrs{
13  
14 using std::cout;
15 using std::endl;
16 
17 class SellingTickets
18 {
19     public:
20         virtual ~SellingTickets(){}
21 
22     public:
23         virtual void Selling() = 0;
24         virtual void Price() = 0;
25 
26 };
27  
28 }
29 
30 #endif // __SELLING_TICKETS_H__

 

 1  ///
 2  /// @file    Tickets.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 20:39:17
 5  ///
 6 
 7 #ifndef __TICKETS_H__
 8 #define __TICKETS_H__
 9 
10 #include "Selling_Tickets.h"
11  
12 namespace marrs{
13 
14 class Tickets
15 : public SellingTickets
16 {
17 
18 };
19  
20 }
21 
22 #endif // __TICKETS_H__

 

 1  ///
 2  /// @file    Bus_Ticket.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 20:41:08
 5  ///
 6  
 7 #ifndef __BUS_TICKET_H__
 8 #define __BUS_TICKET_H__
 9 
10 #include "Tickets.h"
11 
12 namespace marrs{
13 
14 class BusTicket
15 : public Tickets
16 {
17     public:
18         void Selling()
19         {
20             cout << "selling : BusTicket" << endl;
21         }
22 
23         void Price()
24         {
25             cout << "price   : 80 RMB" << endl;
26         }
27 };
28  
29 }
30 
31 #endif // __BUS_TICKET_H__

 

 1  ///
 2  /// @file    Train_Ticket.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 20:41:08
 5  ///
 6  
 7 #ifndef __TRAIN_TICKET_H__
 8 #define __TRAIN_TICKET_H__
 9 
10 #include "Tickets.h"
11 
12 namespace marrs{
13 
14 class TrainTicket
15 : public Tickets
16 {
17     public:
18         void Selling()
19         {
20             cout << "selling : TrainTicket" << endl;
21         }
22 
23         void Price()
24         {
25             cout << "price   : 100 RMB" << endl;
26         }
27 };
28  
29 }
30 
31 #endif // __TRAIN_TICKET_H__

 

 1  ///
 2  /// @file    Proxy.h
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 20:46:13
 5  ///
 6  
 7 #ifndef __PROXY_H__
 8 #define __PROXY_H__
 9 
10 #include "Tickets.h"
11 
12 namespace marrs{
13 
14 class Proxy
15 : public SellingTickets
16 {
17     public:
18         Proxy(Tickets * ticket)
19         : _ticket(ticket)
20         {
21         }
22 
23         ~Proxy()
24         {
25             delete _ticket;
26         }
27 
28     public:
29         void Selling()
30         {
31             _ticket->Selling();
32         }
33 
34         void Price()
35         {
36             _ticket->Price();
37             Proxy_Price();
38             Discount();
39         }
40 
41     private:
42         void Proxy_Price()
43         {
44             cout << "proxy price : 50 RMB" << endl;
45         }
46 
47         void Discount()
48         {
49             cout << "discount    : 50%" << endl;
50         }
51 
52     private:
53         Tickets * _ticket;
54 
55 };
56  
57 }
58 
59 #endif // __PROXY_H__

 

 1  ///
 2  /// @file    Student.cc
 3  /// @author  marrs(chenchengxi993@gmail.com)
 4  /// @date    2017-08-13 20:51:42
 5  ///
 6 
 7 #include "Proxy.h"
 8 #include "Bus_Ticket.h"
 9 #include "Train_Ticket.h"
10 
11 using namespace marrs;
12 
13 int main()
14 {
15     Proxy * proxy;
16     proxy = new Proxy(new BusTicket);    
17     proxy->Selling();
18     proxy->Price();
19     delete proxy;
20 
21     proxy = new Proxy(new TrainTicket);    
22     proxy->Selling();
23     proxy->Price();
24     delete proxy;
25 
26     return 0;
27 }

 

编译,运行

[ccx@ubuntu ~/object-oriented/Proxy_Pattern]$>g++ * -o Tickets.exe
[ccx@ubuntu ~/object-oriented/Proxy_Pattern]$>./Tickets.exe 
selling : BusTicket
price   : 80 RMB
proxy price : 50 RMB
discount    : 50%
selling : TrainTicket
price   : 100 RMB
proxy price : 50 RMB
discount    : 50%

 

设计模式学习——代理模式(Proxy Pattern)

标签:ios   std   运行   blog   proxy   image   活动   main   编译   

原文地址:http://www.cnblogs.com/chinxi/p/7354779.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!