C + + design mode shallow to enjoy meta-mode

Source: Internet
Author: User
Enjoy meta mode (Flyweight): Use sharing technology to effectively support a large number of fine-grained objects.

Four role classes:

Flyweight: The superclass or interface of all the specific classes of the class, through which the flyweight can accept and act on the external state.

Flyweight to enjoy meta-factory class: An exclusive meta factory that is used to create and manage flyweight, when a user requests a flyweight, the Flyweightfactory object provides a created instance or creates one if it does not exist.

Concreteflyweight: Inherit flyweight superclass or implement flyweight interface, and increase storage space for internal state.

Unsharedconcreteflyweight specific flyweight subclasses that do not need to be shared, refer to flyweight subclasses that do not require sharing. Because flyweight interface class sharing is possible, it does not force a share.

Pattern implementation:

[code]//class Flyweight{public:    virtual void operation (int extrinsicstate) {}};//-specific classes class Concreteflyweight : Public flyweight{public:    virtual void operation (int extrinsicstate) override{        std::cout << " Concreteflyweight: "<< extrinsicstate << Std::endl;    }};/ /Flyweight Subclass Class Unsharedconcreteflyweight:public Flyweight{public:    virtual void operation (int Extrinsicstate) {        std::cout << "unsharedconcreteflyweight:" << extrinsicstate << Std::endl;    }}; Enjoy meta factory, used to create and manage Flyweight objects class Flyweightfactory{private:    std::map<std::string, flyweight*> flyweights; Public:    flyweightfactory () {        flyweights["X"] = new Concreteflyweight;        flyweights["Y"] = new Concreteflyweight;        flyweights["Z"] = new Concreteflyweight;    }    flyweight* getflyweight (std::string key) {        return (flyweight*) Flyweights[key];}    ;

Client:

[Code]//clientint Main () {    //external state    int extrinsicstate =;    Factory    flyweightfactory *f = new Flyweightfactory;    flyweight* FX = f->getflyweight ("X");    Fx->operation (--extrinsicstate);  output:concreteflyweight:21    flyweight* fy = f->getflyweight ("Y");    Fy->operation (--extrinsicstate);  Output:concreteflyweight:20    flyweight* FZ = f->getflyweight ("Z");    Fz->operation (--extrinsicstate);  output:concreteflyweight:19    Flyweight *uf = new Unsharedconcreteflyweight;  Output:unsharedconcreteflyweight:18    uf->operation (--extrinsicstate);    return 0;}

Enjoy meta-mode benefits:

If an application uses a large number of objects, and a large number of these objects cause significant storage overhead, you should consider using them.

Most states of an object can use an external state, and if you delete an object's external state, you can replace many group objects with a relatively small number of shared objects, and you can consider using the enjoy meta mode.

The above is the C + + design mode to enjoy the meta-mode of content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.