Design Mode C ++ study note 2 (Proxy Mode)

Source: Internet
Author: User

Proxy: You can see that the name is just an intermediary. The real performer is behind the proxy. The examples in cbf4life are also interesting. For more details, refer to the original author's blog: cbf4life.cnblogs.com. Paste the Code to make it easy to use.

2. 1. Explanation

Main (), Ximen Qing

IKindWomen, Interface

CWangPo, proxy

CPanJinLian, one of the actual executors

CJiaShi, operator 2

Note: agents and actual executors are derived fromCommon interfacesThe proxy has an instance of the actual performer. Each proxy function (interface implementation function) directly calls the corresponding interface function of the actual executor.

Note: The agent simply loads and then calls the actually executed function.

Check the Code:

// IKindWomen. h

# Pragma once
Class IKindWomen
{
Public:
IKindWomen (void );
Virtual ~ IKindWomen (void );
Virtual void MakeEyesWithMan () = 0;
Virtual void HappyWithMan () = 0;
};

// WangPo. h

# Pragma once
# Include "ikindwomen. h"
Class CWangPo:
Public IKindWomen
{
Public:
CWangPo (IKindWomen * pKindWomen );
~ CWangPo (void );
Void HappyWithMan (void );
Void MakeEyesWithMan (void );
Private:
IKindWomen * m_pKindWomen;
};

// WangPo. cpp

# Include "StdAfx. h"
# Include "WangPo. h"
CWangPo: CWangPo (IKindWomen * pKindWomen)
{
This-> m_pKindWomen = pKindWomen;
}
CWangPo ::~ CWangPo (void)
{
Delete this-> m_pKindWomen;
}
Void CWangPo: HappyWithMan ()
{
This-> m_pKindWomen-> HappyWithMan ();
}
Void CWangPo: MakeEyesWithMan (void)
{
This-> m_pKindWomen-> MakeEyesWithMan ();
}

// PanJinLian. h

# Pragma once
# Include "ikindwomen. h"
Class CPanJinLian:
Public IKindWomen
{
Public:
CPanJinLian (void );
~ CPanJinLian (void );
Void HappyWithMan (void );
Void MakeEyesWithMan (void );
};

// PanJinLian. cpp

# Include "StdAfx. h"
# Include "PanJinLian. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CPanJinLian: CPanJinLian (void)
{
}
CPanJinLian ::~ CPanJinLian (void)
{
}
Void CPanJinLian: HappyWithMan (void)
{
Cout <"Pan Jinlian and man do that..." <endl;
}
Void CPanJinLian: MakeEyesWithMan (void)
{
Cout <"Pan Jinlian eye-catching" <endl;
}

// JiaShi. h

# Pragma once
# Include "ikindwomen. h"
Class CJiaShi:
Public IKindWomen
{
Public:
CJiaShi (void );
~ CJiaShi (void );
Void HappyWithMan (void );
Void MakeEyesWithMan (void );
};

// JiaShi. cpp

# Include "StdAfx. h"
# Include "JiaShi. h"
# Include <iostream>
Using std: cout;
Using std: endl;
CJiaShi: CJiaShi (void)
{
}
CJiaShi ::~ CJiaShi (void)
{
}
Void CJiaShi: HappyWithMan (void)
{
Cout <"Jia and man do that..." <endl;
}
Void CJiaShi: MakeEyesWithMan (void)
{
Cout <"Jia's eye-catching" <endl;
}

// Proxy. cpp
# Include "stdafx. h"
# Include "WangPo. h"
# Include "PanJinLian. h"
# Include "JiaShi. h"
# Include <iostream>
Using std: cout;
Using std: endl;
Void DoPanJinLian ()
{
CWangPo * pWangPo;
// Simon Qing is looking for Pan Jinlian and asked Wang to arrange it.
PWangPo = new CWangPo (new CPanJinLian ());
PWangPo-> MakeEyesWithMan ();
PWangPo-> HappyWithMan ();
Delete pWangPo;
}
Void DoJiaShi ()
{
CWangPo * pWangPo;
// Simon Qing wants to find Jia and ask Wang Po to arrange it.
PWangPo = new CWangPo (new CJiaShi ());
PWangPo-> MakeEyesWithMan ();
PWangPo-> HappyWithMan ();
Delete pWangPo;
}
Int _ tmain (int argc, _ TCHAR * argv [])
{
// Simon Qing is looking for Pan Jinlian
DoPanJinLian ();

// Simon Qing is looking for Jia
DoJiaShi ();

_ CrtSetDbgFlag (_ CRTDBG_LEAK_CHECK_DF | _ CRTDBG_ALLOC_MEM_DF );
_ CrtDumpMemoryLeaks ();

System ("pause ");
Return 0;
}

It seems that the structure of the proxy mode is similar to that of the Policy mode, where an instance is loaded by a class, the policy mode is CContext, and the proxy mode is CWangPo. CContext is not derived from IStrategy, so you do not need to implement IStrategy interface functions, while CWangPo is derived from IKindWomen. Therefore, CWangPo is clear about CPanJinLian and CJiaShi interface functions. This is the proxy. The agent knows what the agent can do, that is, the function. Therefore, the agent can become an intermediary.

The proxy mode can well separate the front and back ends to achieve loose coupling. The proxy mode is structured. The class-related graph used in the example is still not an abstract class graph in the proxy mode. Such a class chart is easier to understand the proxy mode. Abstract class graph has a higher abstraction level, but it is not easy to understand.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.