"Java and patterns" to enjoy the meta-model

Source: Internet
Author: User
Tags stub

Flyweight in boxing is the most lightweight, that is, "The fly level" or "rainfall level", where the choice to use the "enjoy meta-mode" of the free translation, because it is more reflective of the intention of the model. The enjoy meta mode is the structure mode of the object. The enjoy meta mode efficiently supports a large number of fine-grained objects in a shared manner.

String types in Java

In the Java language, the string type is the use of the enjoy meta pattern. The string object is the final type, and the object cannot be changed once it is created. In Java, where string constants are present in a constant pool, Java ensures that a string constant has only one copy in the constant pool. String a= "abc", where "abc" is a string constant.

public class Test {public    static void Main (string[] args) {                String a = "abc";        String B = "abc";        System.out.println (a==b);            }}

The result in the above example is: true, which means that both A and B two references point to the same string constant "ABC" in the Constant pool. Such a design avoids the unnecessary amount of resource consumption generated when creating n many identical objects.

Structure of the enjoy meta-mode

The enjoy meta mode takes a share to avoid the overhead of having the same content object in large numbers. The most common and intuitive kind of overhead is the loss of memory. The key to sharing the shared meta object is to differentiate between the intrinsic state (the Internal State) and the outer states (External).

  An intrinsic state is stored inside the element of the object and does not vary depending on the environment. Therefore, a share can have an intrinsic state and can be shared.

A foreign State is changed with the change of the environment and cannot be shared. The outer state of the object must be saved by the client, and then passed in to the inside of the object when it is needed after the object is created. The outer state can not affect the intrinsic state of the objects, they are independent of each other.

The enjoy meta-mode can be divided into two forms of the simple and complex mode.

Simply enjoy meta mode

In the simple-to-enjoy mode, all the objects of the object can be shared.

The roles involved in the simple-to-enjoy meta-mode are as follows:

abstract enjoy meta (Flyweight) role: An abstract interface is given to specify the methods that are required to be implemented for all of the specific enjoyment meta-roles.

specific concreteFlyweightRole: implements the interface defined by the abstract privileges meta role. If there is an intrinsic state, you must be responsible for providing storage space for the intrinsic state.

   enjoy meta-factory (flyweightfactory) role : This role is responsible for creating and managing the rewards role. This role must ensure that the sharing meta-object can be properly shared by the system. When a client object invokes an object, the enjoy Meta factory role checks to see if there is already an eligible object in the system. If it is already available, the meta-factory role should provide the existing one, and if the system does not have an appropriate element of the object, the enjoy meta-factory role should create a suitable object for the privilege.

Source

Abstract enjoy Meta role class

Public interface Flyweight {    //A schematic method, the state of the parameter is the outside of the states public    void operation (String);}

The concreteflyweight has an intrinsic state, in this case the Intrinsicstate attribute of a character type is represented, and its value should be given when the element object is created. All the intrinsic states are not changed after the object is created.

If an object has an external state, all external states must be stored on the client, and the client is passed in with the object of the privilege when the element is used. There is only one external state, and the operation () method's parameter state is an external incoming foreign.

public class Concreteflyweight implements Flyweight {    private Character intrinsicstate = null;    /**     * constructor, intrinsic status passed in as parameter     * @param State     *    /Public concreteflyweight (Character) {        This.intrinsicstate = State;    }            /**     * External state as a parameter in the method, change the behavior of the method,     * But does not change the intrinsic state of the object.     *    /@Override public    void operation (String state) {        //TODO auto-generated method stub        SYSTEM.OUT.PRINTLN ("intrinsic state =" + this.intrinsicstate);        SYSTEM.OUT.PRINTLN ("Extrinsic state =" + state);}    }

Enjoy the meta-factory role class, it must be noted that the client cannot instantiate a specific class of classes directly, but must pass a factory object, using a factory () method to get the object of the privilege. In general, there is only one in the entire system for the enjoy Meta factory object, so you can also use singleton mode.

When the client needs to simply enjoy the meta-object, it is necessary to call the factory () method of the Meta factory and pass in the intrinsic state of the desired simple-to-enjoy meta-object, and the factory method produces the desired object.

public class Flyweightfactory {    private map<character,flyweight> files = new Hashmap<character,flyweight > ();        Public Flyweight Factory (Character state) {        //First Find object from cache        Flyweight fly = Files.get (state);        if (fly = = null) {            //Create a new Flyweight object if the object does not exist            fly = new Concreteflyweight (state);            Add this new flyweight object to the cache            Files.put (state, fly);        }        return fly;    }}

Client class

public class Client {public    static void Main (string[] args) {        //TODO auto-generated method stub        FLYWEIGHTFAC Tory factory = new flyweightfactory ();        Flyweight fly = factory.factory (new Character (' a '));        Fly.operation ("First call");                Fly = Factory.factory (new Character (' B '));        Fly.operation ("Second call");                Fly = Factory.factory (new Character (' a '));        Fly.operation ("Third Call");}    }

Although the client has requested three of the objects, only two are actually created, which is the meaning of sharing. The results of the operation are as follows:

Compound enjoy meta mode

In the simple-to-enjoy meta-mode, all the objects of the object are simply to enjoy the meta-object, which means they can be shared directly. There is also a more complex situation, the use of a few simple-to-enjoy composite model to compound, to form a compound to enjoy meta-objects. Such compound-sharing objects cannot be shared by themselves, but they can be decomposed into simple-to-enjoy meta-objects, while the latter can be shared.

  

The roles involved in the compound-sharing role are as follows:

abstract enjoy meta (Flyweight) role: An abstract interface is given to specify the methods that are required to be implemented for all of the specific enjoyment meta-roles.

specific concreteFlyweightRole: implements the interface defined by the abstract privileges meta role. If there is an intrinsic state, you must be responsible for providing storage space for the intrinsic state.

compound concretecompositeflyweightrole : The object represented by the composite privilege role cannot be shared, However, a composite object can be decomposed into a combination of multiple objects that are simply a single-element object. The compound privilege role is also known as a non-shareable, shared meta object.

   enjoy meta-factory (flyweightfactory) role : This role is responsible for creating and managing the rewards role. This role must ensure that the sharing meta-object can be properly shared by the system. When a client object invokes an object, the enjoy Meta factory role checks to see if there is already an eligible object in the system. If it is already available, the meta-factory role should provide the existing one, and if the system does not have an appropriate element of the object, the enjoy meta-factory role should create a suitable object for the privilege.

Source

Abstract enjoy Meta role class

Public interface Flyweight {    //A schematic method, the state of the parameter is the outside of the states public    void operation (String);}

Specific enjoy meta role classes

public class Concreteflyweight implements Flyweight {    private Character intrinsicstate = null;    /**     * constructor, intrinsic status passed in as parameter     * @param State     *    /Public concreteflyweight (Character) {        This.intrinsicstate = State;    }            /**     * External state as a parameter in the method, change the behavior of the method,     * But does not change the intrinsic state of the object.     *    /@Override public    void operation (String state) {        //TODO auto-generated method stub        SYSTEM.OUT.PRINTLN ("intrinsic state =" + this.intrinsicstate);        SYSTEM.OUT.PRINTLN ("Extrinsic state =" + state);}    }

A compound-sharing object is composed of a simple-to-enjoy meta-object, so it provides an aggregation management method such as add (). Because a composite object has different aggregation elements, these aggregation elements are added after the compound's object is created, which in itself means that the state of the compound's object is changed, so that the composite object cannot be shared.

The compound-enjoy meta-role implements the interface defined by the abstract-enjoy meta-role, which is the operation () method, which has a parameter that represents the outer state of the composite object. The outer state of all the primitive elements of a compound-sharing object is equal to the outer state of the compound-sharing object, while the intrinsic state of the pure-object contained in a compound-sharing object is generally unequal, otherwise there is no use value.

public class Concretecompositeflyweight implements Flyweight {        private map<character,flyweight> files = new Hashmap<character,flyweight> ();    /**     * Add a new Simple object to the aggregation in */     public    void Add (Character key, Flyweight fly) {        files.put (key,fly);    }    /**     * The outer state is passed as a parameter in the method *    /@Override public    void operation (String states) {        Flyweight fly = null;< C13/>for (Object o:files.keyset ()) {            fly = Files.get (o);            Fly.operation (state);}}}            

The enjoy Meta factory role provides two different methods, one for providing a simple-to-use meta-object, and the other for providing a compound-sharing object.

public class Flyweightfactory {    private map<character,flyweight> files = new Hashmap<character,flyweight > ();    /**     * Compound to enjoy meta factory method *    /Public Flyweight factory (list<character> compositestate) {        Concretecompositeflyweight compositefly = new Concretecompositeflyweight ();                for (Character state:compositestate) {            Compositefly.add (the State,this.factory (state));        }                return compositefly;    }    /**     * Simple to enjoy meta factory method *    /Public Flyweight factory (Character state) {        //First Find object from cache        Flyweight fly = Files.get (state);        if (fly = = null) {            //Create a new Flyweight object if the object does not exist            fly = new Concreteflyweight (state);            Add this new flyweight object to the cache            Files.put (state, fly);        }        return fly;    }}

Client role

public class Client {public    static void Main (string[] args) {        list<character> compositestate = new Arraylis T<character> ();        Compositestate.add (' a ');        Compositestate.add (' B ');        Compositestate.add (' C ');        Compositestate.add (' a ');        Compositestate.add (' B ');                Flyweightfactory flyfactory = new Flyweightfactory ();        Flyweight compositeFly1 = flyfactory.factory (compositestate);        Flyweight compositeFly2 = flyfactory.factory (compositestate);        Compositefly1.operation ("Composite call");                System.out.println ("---------------------------------");                SYSTEM.OUT.PRINTLN ("Composite sharing mode can share objects:" + (compositeFly1 = = compositeFly2));                Character state = ' a ';        Flyweight fly1 = flyfactory.factory (state);        Flyweight fly2 = flyfactory.factory (state);        System.out.println ("Pure meta mode can share objects:" + (fly1 = = Fly2));}    }

The results of the operation are as follows:

From the running result, it can be seen that the outer state of all the elements of the pure element object of a compound-object is equal to the outer state of the compound-sharing object. The Sinotrans status equals composite call.

From the running results, it can be seen that the intrinsic state of the simple-to-enjoy meta-objects contained in a compound-sharing object is generally unequal. That is, the intrinsic states are B, C, a respectively.

As you can see from the running results, the compound-sharing object cannot be shared. Objects that were created two times by factory, even with the same object compositestate, are not the same object.

From the running results, it can be seen that the simple-to-enjoy meta-objects can be shared. objects created with the same object state through the factory two times are the same object.

Pros and cons of the enjoy meta model

The advantage of the enjoy meta-mode is that it drastically reduces the number of objects in memory. However, the cost of doing this is also high:

The enjoy meta-mode makes the system more complex. In order for objects to be shared, some States need to be externally instantiated, which complicates the logic of the program.

The enjoy meta-mode takes the state of the Meta object out of the way, while reading the external state makes the run time slightly longer.

"Java and patterns" to enjoy the meta-model

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.