Chapter 26 hedonic model

One concept

  • Sharing mode, using sharing technology to effectively support a large number of fine-grained objects.

Two UML diagram

  • FlyweightFactory class: a sharing factory used to create and manage Flyweight objects. It is mainly used to ensure reasonable sharing of Flyweight. When a user requests a Flyweight, the FlyweightFactory object provides a created instance or creates one (if it does not exist).
  • Flyweight class: super class or interface of all classes with specific attributes. Through this interface, flyweight can accept and act on external states.
  • ConcreteFlyweight class: inherits Flyweight superclass or implemented interface, and increases storage space for internal state.
  • UnsharedConcreteFlyweight class: refers to those Flyweight subclasses that do not need to be shared, because Flyweight interface sharing is possible, but it does not force sharing.

III. internal and external state

The application of four share model

Five C + + code implementation

#include "pch.h"
#include <iostream>
#include <string>
#include <map>
using namespace std;

//The user's account is in an external state and should be handled by a special object
class User
{
public:
    User(string str)
        :name(str) {}
    string GetName() const
    {
        return this->name;
    }
private:
    string name;
};

//Abstract class of website
class WebSite
{
public:
    virtual void Use(User user) = 0;
};

//Specific website
class ConcreteWebSite : public WebSite
{
public:
    ConcreteWebSite(string webname)
    {
        this->name = webname;
    }
    void Use(User user) override
    {
        cout << "Website classification: " << this->name
            << " user: " << user.GetName() << endl;
    }
private:
    string name;
};

//Website factory class
class WebSiteFactory
{
public:
    ~WebSiteFactory()
    {
        map<string, WebSite*>::iterator it;
        for (it = flyweights.begin(); it != flyweights.end(); ++it)
        {
            delete (*it).second;
        }
    }
    WebSite* GetWebSiteCategory(string key)
    {
        map<string, WebSite*>::iterator it;

        //Determine whether this object exists. If it exists, it will be returned directly. If it does not exist, it will be returned directly.
        //Return after instantiation
        for(it = flyweights.begin(); it != flyweights.end(); it++)
        {
            if ((*it).first == key)
            {
                return (*it).second;
            }
        }
        WebSite* website = new ConcreteWebSite(key);
        flyweights.insert(make_pair(key,website));
        return website;
    }
    int GetWebSiteCount()
    {
        return flyweights.size();
    }
private:
    map<string, WebSite*> flyweights;
};



int main()
{
    WebSiteFactory* f = new WebSiteFactory();

    WebSite* fx = f->GetWebSiteCategory("Product display");
    fx->Use(User("Side dish"));

    WebSite* fy = f->GetWebSiteCategory("Product display");
    fy->Use(User("Big bird"));

    WebSite* fz = f->GetWebSiteCategory("Product display");
    fy->Use(User("Jiao Jiao"));


    WebSite* fl = f->GetWebSiteCategory("Blog");
    fx->Use(User("Old naughty boy"));

    WebSite* fm = f->GetWebSiteCategory("Blog");
    fy->Use(User("Peach Valley six cents"));

    WebSite* fn = f->GetWebSiteCategory("Blog");
    fy->Use(User("South China Sea crocodile God"));

    cout << "Total number of site categories obtained: " << f->GetWebSiteCount() << endl;

    return 0;
}

Keywords: PHP

Added by korion on Fri, 01 Nov 2019 09:01:17 +0200