RequestStreamer는 스트리밍 요청을 돕기를 원합니다. 아래에서 보는 것과 반대로이를 구현하는 더 좋은 방법이 있습니까?클래스 B의 멤버 인 클래스 A의 객체입니다. B는 A의 객체를 필요로합니다.
이러한 종류의 코드가 적합한 디자인 패턴은 무엇입니까? 그것은 주인의 패턴이나 도우미 패턴이 될 것입니까?
이 예제는 따라서 당신은 (개인 회원 데이터에 액세스) 클래스를 인스턴스화 할 수 없습니다 클래스 요청에 생성자 및 민간 소멸자를 선언하는 A와 B
사이의 관계class Request {
RequestStreamer* streamer;
int contentLen;
public:
Request()
{
contentLen = 0;
streamer = new RequestStreamer(this);
}
~Request()
{
delete streamer;
}
int getContentLen()
{
return contentLen;
}
bool initialize()
{
// Code to update 'contentLen' by reading from source request object.
// <code>
if (streamer) streamer->initialize();
}
bool sendReq()
{
streamer->streamReq();
}
int getBytes (int nBytes)
{
// some code to read nBytes from the input source of this request object
}
};
class RequestStreamer {
Request* req;
bool bEnabled;
int chunkSize;
public:
RequestStreamer(Request* aobj)
{
chunkSize = 1024*1024;
req = aobj;
}
~RequestStreamer()
{
}
bool initialize()
{
if (req && req->getContentLen() > chunkSize)
bEnabled = true;
}
bool streamReq()
{
if (!req) return false;
// Assume that there exists socket object implementation already
if (bEnabled)
{
while (req->getBytes(chunkSize) != -1)
socket->send(req->getBytes(chunkSize));
}
else
{
socket->send(req->getBytes(req->getContentLen()));
}
}
};
* A와 B 사이의 관계를 나타내는 적절한 예가 없습니다. 불가능한 그들의 관계 종류에 적합한 구조. – Ryan
관련없는 메모에서 메모리가 누출됩니다. – Rakete1111
'A'와'B'가 실제 세계의 대상이라면 무엇을 대표할까요? 'A'는 자동차와 같고'B'는 엔진입니까? 그 그림을 그리기가 정말로 어렵고, 객체 이름이'A'와'B'처럼 매우 모호 할 때 이상적으로 구조화합니다. – PrimRock