Modern C++ Design Pattern/Chapter 12. 프록시

created : Sun, 12 Apr 2020 23:10:03 +0900
modified : Sat, 26 Sep 2020 23:22:05 +0900
cpp design pattern proxy

스마트 포인터

속성 프록시

template <typename T> struct Property
{
  T value;
  Property(const T initial_value)
  {
    *this = initial_value;
  }
  operate T()
  {
    return value;
  }
  T operator=(T new_value)
  {
  return value = new_value;
  }
}


struct Creature
{
  Property<int> strength{ 10 };
  Property<int> agility{ 5 };
}

Creature creature;
creature.agility = 20;
auto x = creature.strength;

가상 프록시

struct Image
{
  virtual void draw() = 0;
};

struct Bitmap : Image
{
  Bitmap(const string& filename)
  {
    cout << "Loading image from " << filename << endl;
  }

  void draw() override
  {
    cout << "Drawing Image " << filename << endl;
  }
};

struct LazyBitmap : Image
{
  LazyBitmap(const string& filename)
    : filename(filename) {}
  ~LazyBitmap() { delete bmp; }
  void draw() override
  {
    if (!bmp)
      bmp = new Bitmap(filename);
    bmp->draw();
  }

private:
  Bitmap *bmp(nullptr);
  string filename;
};

커뮤니케이션 프록시

요약