Bridge_Pattern_2 - 8BitsCoding/RobotMentor GitHub Wiki
Pimpl(사춘기, 여드름)??? Nono.. -> Pointer to Implementation Idiom
아래와 같은식으로 생성과 동시에 PersonImpl를 메모리할당 해지하는 방법
pimpl를 쓰는 이유는 struct 혹은 class가 자주 변경된다면 해당 object를 include한 객체의 컴파일 시간이 길어지기에 그 컴파일 시간을 줄이기 위해 include를 줄이는 방법 중 하나이다.
// Person.h
#pragma once
#include <string>
struct Person
{
std::string name;
class PersonImpl;
PersonImpl *impl; // bridge - not necessarily inner class, can vary
Person();
~Person();
void greet();
};
// Person.cpp
#include "Person.h"
struct Person::PersonImpl
{
void greet(Person* p);
};
void Person::PersonImpl::greet(Person* p)
{
printf("hello %s", p->name.c_str());
}
Person::Person()
: impl(new PersonImpl)
{
}
Person::~Person()
{
delete impl;
}
void Person::greet()
{
impl->greet(this);
}
#include <iostream>
#include <string>
#include <vector>
#include "Person.h"
using namespace std;
// two classes of objects
// Renderers - determine how an object is drawn
// Shapes - determine what to draw
struct Renderer
{
virtual void render_circle(float x, float y, float radius) = 0;
};
struct VectorRenderer : Renderer
{
void render_circle(float x, float y, float radius) override
{
cout << "Rasterizing circle of radius " << radius << endl;
}
};
struct RasterRenderer : Renderer
{
void render_circle(float x, float y, float radius) override
{
cout << "Drawing a vector circle of radius " << radius << endl;
}
};
struct Shape
{
protected:
Renderer& renderer;
Shape(Renderer& renderer) : renderer{ renderer } {}
public:
virtual void draw() = 0; // implementation specific
virtual void resize(float factor) = 0; // abstraction specific
};
struct Circle : Shape
{
float x, y, radius;
void draw() override
{
renderer.render_circle(x, y, radius);
}
void resize(float factor) override
{
radius *= factor;
}
Circle(Renderer& renderer, float x, float y, float radius)
: Shape{renderer},
x{x},
y{y},
radius{radius}
{
}
};
void bridge()
{
RasterRenderer rr;
Circle raster_circle{ rr, 5,5,5 };
raster_circle.draw();
raster_circle.resize(2);
raster_circle.draw();
}
int main()
{
}