Observer design pattern in C++

前端 未结 7 1003
礼貌的吻别
礼貌的吻别 2020-12-06 03:00

Is the observer design pattern already defined in STL (Like the java.util.Observer and java.util.Observable in Java) ?

7条回答
  •  忘掉有多难
    2020-12-06 03:43

                 #include
                 #include
                 #include
                 #include
                 using namespace std;
    
    
                 class Customer;
                 class flipkart
                 {
                    vectorlist;
                    vector::iterator it;
                    public:
                    void Register(Customer *customer)
                    {
                       list.push_back(customer);
                    }
                    void unregister(Customer *customer)
                    {
                        list.erase(remove(list.begin(), list.end(),customer), list.end()); 
                    }
    
                    void notify(string item,float vprice);
                 };
    
    
                 class observer
                 {
                     public:
                     virtual void update(string item,float vprice)=0;
                 };
                 class Customer:public observer
                 {
                     string name;
                     public:
                     Customer(string n)
                     {
                         name=n;
                     }
    
                     void update(string item,float vprice)
                     {
                         cout<<"**Flipkart**updated price for "<update(item,vprice);
                     }
                 }
    
                 class product:public flipkart
                 {
                     public:
                     void change_price(string item,float vprice)
                     {
                         notify(item,vprice);
                     }
                 };
    
                 int main()
                 {
                     Customer customer1("Dhoni"),customer2("Yuvraj"),customer3("Kohli");
                     product LCD;
    
                     LCD.Register(&customer1);
                     LCD.Register(&customer2);
                     LCD.Register(&customer3);
    
                     LCD.change_price("LCD HD2 TV",12000);
    
                     LCD.unregister(&customer2);
                     cout<<"after unregisterng customer2:\n";
    
                     LCD.change_price("LCD HD2 TV",11500);
                 }
    

提交回复
热议问题