zoukankan      html  css  js  c++  java
  • 享元模式

    1】什么是享元模式?
    
    享元模式:
    
    【2】享元模式的代码示例:
    #include <iostream>
    #include <string>
    #include <map>
    using namespace std;
    
    class Flyweight
    {
    public:
        virtual void operation(int) = 0;
    };
    
    class ConcreteFlyweight : public Flyweight
    {
        void operation(int extrinsicState)
        {
            cout << "具体FlyWeight: " << extrinsicState << endl;
        }
    };
    
    class UnsharedConcreteFlyweight : public Flyweight
    {
        void operation(int extrinsicState)
        {
            cout << "不共享的具体FlyWeight: " << extrinsicState << endl;        
        }
    };
    
    class FlyweightFactory
    {
    private:
        map<string,Flyweight*> flyweights;
    public:
        FlyweightFactory()
        {
            flyweights["X"] = new ConcreteFlyweight();
            flyweights["Y"] = new ConcreteFlyweight();
            flyweights["Z"] = new UnsharedConcreteFlyweight();
        }
        Flyweight *getFlyweight(string key)
        {
            return (Flyweight *)flyweights[key];
        }
    };
    
    int main()
    {
        int state = 22;
        FlyweightFactory *f = new FlyweightFactory();
        
        Flyweight *fx = f->getFlyweight("X");
        fx->operation(--state);
        
        Flyweight *fy = f->getFlyweight("Y");
        fy->operation(--state);
    
        Flyweight *fz = f->getFlyweight("Z");
        fz->operation(--state);
    
        Flyweight *uf = new UnsharedConcreteFlyweight();
        uf->operation(--state);
    
        return 0;
    }
    //Result:
    /*
    具体FlyWeight: 21
    具体FlyWeight: 20
    不共享的具体FlyWeight: 19
    不共享的具体FlyWeight: 18
    */

    代码示例2:

    #include <iostream>
    #include <list>
    #include <string>
    #include <map>
    using namespace std;
    
    class WebSite
    {
    public:
        virtual void use() = 0;
    }; 
    
    class ConcreteWebSite : public WebSite
    {
    private:
        string name;
    public:
        ConcreteWebSite(string name)
        {
            this->name = name;
        }
        void use()
        {
            cout << "网站分类: " << name << endl;
        }
    };
    
    class WebSiteFactory
    {
    private:
        map<string,WebSite*> wf;
    public:
         
        WebSite *getWebSiteCategory(string key)
        {
            if (wf.find(key) == wf.end())
            {
                wf[key] = new ConcreteWebSite(key);
            }    
            return wf[key];
        }
         
        int getWebSiteCount()
        {
            return wf.size();
        }
    };
    
    int main()
    {
        WebSiteFactory *wf = new WebSiteFactory();
    
        WebSite *fx = wf->getWebSiteCategory("good");
        fx->use();
        
         WebSite *fy = wf->getWebSiteCategory("产品展示");
        fy->use();
    
        WebSite *fz = wf->getWebSiteCategory("产品展示");
        fz->use();
    
    
        WebSite *f1 = wf->getWebSiteCategory("博客");
        f1->use();
    
        WebSite *f2 = wf->getWebSiteCategory("微博");
        f2->use();
    
        cout << wf->getWebSiteCount() << endl;
        return 0;
    }
    //Result:
    /*
    网站分类: good
    网站分类: 产品展示
    网站分类: 产品展示
    网站分类: 博客
    网站分类: 微博
    4
    */
    http://www.cnblogs.com/Braveliu/p/3956948.html
  • 相关阅读:
    MQTT TLS 加密传输
    python多进程并发redis
    各种消息队列的特点
    mqtt异步publish方法
    Numpy API Analysis
    Karma install steps for unit test of Angular JS app
    reinstall bower command
    Simulate getter in JavaScript by valueOf and toString method
    How to: Raise and Consume Events
    获取对象的类型信息 (JavaScript)
  • 原文地址:https://www.cnblogs.com/leijiangtao/p/4534543.html
Copyright © 2011-2022 走看看