1. 程式人生 > >設計模式學習——代理模式(Proxy Pattern)

設計模式學習——代理模式(Proxy Pattern)

pre .cn mar str ubuntu 技術 運行 obj proxy

放假啦~學生們要買車票回家了,有汽車票、火車票,等。但是,車站很遠,又要考試,怎麽辦呢?找代理買啊,雖然要多花點錢,但是,說不定在搞活動,有折扣呢~

技術分享

 1  ///
 2  /// @file    Selling_Tickets.h
 3  /// @author  marrs([email protected])
 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([email protected])
 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([email protected])
 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([email protected])
 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([email protected])
 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([email protected])
 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 }

編譯,運行

[[email protected] ~/object-oriented/Proxy_Pattern]$>g++ * -o Tickets.exe
[[email protected] ~/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)