Agent, a look at the name will know that this is only an intermediary, the real performer in the back of the agent. Cbf4life's example in his book is also very interesting, and more detailed content and description can refer to the original Author blog: cbf4life.cnblogs.com. Now paste the code to make it easy to follow.
2.1. Explanation
Main (), Ximen Qing
Ikindwomen, interface
Cwangpo, Agent
Cpanjinlian, one of the actual performers
Cjiashi, two of the actual performers
Description: The agent and the actual performer derive from the common interface , and the broker has an instance of the actual performer. Each function of the agent (the implementation function of the interface) directly invokes the corresponding interface function of the actual performer.
Note: The agent simply loads and then invokes the actual performer's function.
Look at 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 men do that ... "<< Endl;
}
void Cpanjinlian::makeeyeswithman (void)
{
cout << "Pan Jinlian" << 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 << "Chia and men do that ... "<< Endl;
}
void Cjiashi::makeeyeswithman (void)
{
cout << "Chia wink" << 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 want to find Pan Jinlian, let Wang to arrange.
Pwangpo = new Cwangpo (new Cpanjinlian ());
Pwangpo->makeeyeswithman ();
Pwangpo->happywithman ();
Delete Pwangpo;
}
void Dojiashi ()
{
Cwangpo *pwangpo;
//Simon Qing want to find Chia, let Wang to arrange.
Pwangpo = new Cwangpo (new Cjiashi ());
Pwangpo->makeeyeswithman ();
Pwangpo->happywithman ();
Delete Pwangpo;
}
int _tmain (int argc, _tchar* argv[])
{
//Simon Qing want to find pan Jinlian
Dopanjinlian ();
Simon Qing wants to find chia.
Dojiashi ();
_CrtSetDbgFlag (_CRTDBG_LEAK_CHECK_DF | _crtdbg_alloc_mem_df);
_CrtDumpMemoryLeaks ();
System ("pause");
return 0;
}
It seems that the structure of the proxy pattern is similar to the policy pattern, which is a class that loads an instance of the interface, the policy mode is ccontext to load, and the proxy mode is CWANGPO to load. Ccontext is not derived from istrategy, so there is no need to implement IStrategy interface functions, and Cwangpo is derived from ikindwomen so Cwangpo is well aware of Cpanjinlian and Cjiashi interface functions. This is the agent, the agent knows what the agent can do is the function, so the agent may become an intermediary.
The proxy mode can be very good to separate the front and back, and realize the loose coupling. The proxy mode belongs to the structural pattern. is still the class-related graph used in the example, not the abstract class diagram of the proxy pattern. Such a class diagram makes it easier to understand the proxy pattern. Abstract class diagrams, of course, have higher levels of abstraction, but are not conducive to understanding.
Design pattern C + + Learning Note II (proxy mode)