

參考鏈接: 1. 明智地使用Pimpl 2. 編譯防火墻——C++的Pimpl慣用法解析

 代碼#include <stdio.h>#include <string>#include <vector>#include <iostream>using std::string;using std::vector;using namespace std;class Subject;class Observer{public:    virtual void update(Subject *sub, int value) = 0;};class Subject{public:    void attach(Observer*obs)    {        m_views.push_back(obs);    }    void set_val(int value)    {        m_value = value;        notify();    }    void notify()    {        for(int i = 0; i < m_views.size(); i++){            m_views[i]->update(this, m_value);        }    }private:    int m_value;    vector<Observer*> m_views;};class View : public Observer{public:    void update(Subject *sub, int value)    {        m_watchValue = value;    }    void paintView()    {        cout << m_watchValue<< endl;    }private:    int m_watchValue;};int main(int argc, char *argv[]){    View v1;    View v2;    Subject s;    s.attach(&v1);    s.attach(&v2);    s.set_val(100);    v1.paintView();    v2.paintView();    cout << "-----------------------" << endl;    s.set_val(200);    v1.paintView();    v2.paintView();    return 0;}///////////////////////////----------------------100100-----------------------200200
代碼#include <stdio.h>#include <string>#include <vector>#include <iostream>using std::string;using std::vector;using namespace std;class Subject;class Observer{public:    virtual void update(Subject *sub, int value) = 0;};class Subject{public:    void attach(Observer*obs)    {        m_views.push_back(obs);    }    void set_val(int value)    {        m_value = value;        notify();    }    void notify()    {        for(int i = 0; i < m_views.size(); i++){            m_views[i]->update(this, m_value);        }    }private:    int m_value;    vector<Observer*> m_views;};class View : public Observer{public:    void update(Subject *sub, int value)    {        m_watchValue = value;    }    void paintView()    {        cout << m_watchValue<< endl;    }private:    int m_watchValue;};int main(int argc, char *argv[]){    View v1;    View v2;    Subject s;    s.attach(&v1);    s.attach(&v2);    s.set_val(100);    v1.paintView();    v2.paintView();    cout << "-----------------------" << endl;    s.set_val(200);    v1.paintView();    v2.paintView();    return 0;}///////////////////////////----------------------100100-----------------------200200 代碼class Component{public:    Component(int val):m_value(val){}    virtual void add(Component*){}private:    int m_value;};class Primitive:public Component{public:    Primitive(int val):Component(val){}};class Composite:public Component{public:    Composite(int val):Component(val){}    void add(Component *elem) {        c.push_back(elem);    }private:    vector<Component*> c;};
代碼class Component{public:    Component(int val):m_value(val){}    virtual void add(Component*){}private:    int m_value;};class Primitive:public Component{public:    Primitive(int val):Component(val){}};class Composite:public Component{public:    Composite(int val):Component(val){}    void add(Component *elem) {        c.push_back(elem);    }private:    vector<Component*> c;}; 代碼#include <iostream>using namespace std;enum imageType{    LSAT, SPOT};class Image{public:    virtual void draw() = 0;    static Image* findAndClone(imageType);    virtual ~Image() {}protected:    virtual imageType returnType() = 0;    virtual Image *clone() = 0;    static void addPrototype(Image *image)    {        _prototypes[_nextSlot++] = image;    }private:    static Image* _prototypes[10];    static int _nextSlot;};Image *Image::_prototypes[];int Image::_nextSlot;Image *Image::findAndClone(imageType type){    for(int i = 0 ; i < _nextSlot; i++)    {        if(_prototypes[i]->returnType() == type)        {            return _prototypes[i]->clone();        }    }    return NULL;}//////////////////////////////////////////////////////////////////////////class LandSatImage:public Image{public:    imageType returnType() {        return LSAT;    }    void draw() {        cout << "LandSatImage::draw " << _id <<endl;    }    Image *clone() {        return new LandSatImage(1);    }protected:    LandSatImage(int dummy) {        _id = _count++;    }private:    static LandSatImage _landSatImage;    LandSatImage(){        addPrototype(this);    }    int _id;    static int _count;};LandSatImage LandSatImage::_landSatImage;int LandSatImage::_count = 1;//////////////////////////////////////////////////////////////////////////class SpotImage:public Image{public:    imageType returnType() {        return SPOT;    }    void draw() {        cout << "SpotImage::draw "<< _id <<endl;    }    Image *clone() {        return new SpotImage(1);    }protected:    SpotImage(int dummy) {        _id = _count++;    }private:    SpotImage() {        addPrototype(this);    }    static SpotImage _spotImage;    int _id;    static int _count;};SpotImage SpotImage::_spotImage;int SpotImage::_count = 1;//////mainconst int Num_IMAGES = 8;imageType input[Num_IMAGES] = { LSAT, LSAT, LSAT, LSAT, SPOT, SPOT, LSAT};int main(){    Image *images[Num_IMAGES];    int i = 0;    for(i = 0; i < Num_IMAGES; i++)    {        images[i] = Image::findAndClone(input[i]);    }    for(i = 0; i < Num_IMAGES; i++)    {        images[i]->draw();    }    for(i = 0; i < Num_IMAGES; i++)    {        delete images[i];    }    return 0;}
代碼#include <iostream>using namespace std;enum imageType{    LSAT, SPOT};class Image{public:    virtual void draw() = 0;    static Image* findAndClone(imageType);    virtual ~Image() {}protected:    virtual imageType returnType() = 0;    virtual Image *clone() = 0;    static void addPrototype(Image *image)    {        _prototypes[_nextSlot++] = image;    }private:    static Image* _prototypes[10];    static int _nextSlot;};Image *Image::_prototypes[];int Image::_nextSlot;Image *Image::findAndClone(imageType type){    for(int i = 0 ; i < _nextSlot; i++)    {        if(_prototypes[i]->returnType() == type)        {            return _prototypes[i]->clone();        }    }    return NULL;}//////////////////////////////////////////////////////////////////////////class LandSatImage:public Image{public:    imageType returnType() {        return LSAT;    }    void draw() {        cout << "LandSatImage::draw " << _id <<endl;    }    Image *clone() {        return new LandSatImage(1);    }protected:    LandSatImage(int dummy) {        _id = _count++;    }private:    static LandSatImage _landSatImage;    LandSatImage(){        addPrototype(this);    }    int _id;    static int _count;};LandSatImage LandSatImage::_landSatImage;int LandSatImage::_count = 1;//////////////////////////////////////////////////////////////////////////class SpotImage:public Image{public:    imageType returnType() {        return SPOT;    }    void draw() {        cout << "SpotImage::draw "<< _id <<endl;    }    Image *clone() {        return new SpotImage(1);    }protected:    SpotImage(int dummy) {        _id = _count++;    }private:    SpotImage() {        addPrototype(this);    }    static SpotImage _spotImage;    int _id;    static int _count;};SpotImage SpotImage::_spotImage;int SpotImage::_count = 1;//////mainconst int Num_IMAGES = 8;imageType input[Num_IMAGES] = { LSAT, LSAT, LSAT, LSAT, SPOT, SPOT, LSAT};int main(){    Image *images[Num_IMAGES];    int i = 0;    for(i = 0; i < Num_IMAGES; i++)    {        images[i] = Image::findAndClone(input[i]);    }    for(i = 0; i < Num_IMAGES; i++)    {        images[i]->draw();    }    for(i = 0; i < Num_IMAGES; i++)    {        delete images[i];    }    return 0;}新聞熱點
疑難解答
圖片精選