One day a design pattern---Enjoy meta mode __ design mode

Source: Internet
Author: User
Tags getcolor

Introduction: This is a very small pattern in such a variety of design patterns to improve program performance. The main idea is-if there are multiple identical objects in the system, then only one copy of the object is required, without having to create a new object each time it is used.

Functions: Reusing objects, saving memory overhead and object creation time.

Advantages:
1. Save the overhead of creating objects repeatedly
2. Reduce the number of objects in memory, so that the need for system memory has been reduced one, role and role Division

role function
Enjoy Yuan Factory Create an entitlement class to maintain common objects (core components)
Abstract enjoy The Business interface (abstract class or interface) that is required to define the privilege
Specific enjoy Meta class Realize the abstract of the class, complete the specific logic
Client Use the Enjoy Meta mode component to get the object of the privilege through the exclusive factory
second, simple examples

Scene: We've designed a text field, and if we define every text in a text field as an object, then 10,000 objects would be needed if there were 10,000 words, which would be very memory intensive. Then we can consider using the use of the meta pattern to achieve.

Abstract Enjoy

Public interface Font {public
    String createfont ();
}

specific enjoy meta class

public class Bfont implements Font {
    private String tchar;

    Public Bfont (String tchar) {
        This.tchar = TCHAR;
    }

    @Override public
    String CreateFont () {return ' This is
        a sizefont ';
    }

    Public String Gettchar () {return
        tchar;
    }

    public void Settchar (String tchar) {
        This.tchar = TCHAR;
    }

}

enjoy Yuan factory

public class Fontfactory {
    private static map<string, font> fonts = new hashmap<string, font> ();

    public static Font GetFont (String tchar) {
        Font font = Fonts.get (TCHAR);
        if (font = = null) {
            font = new Bfont (TCHAR);
            Fonts.put (TCHAR, font);
        return font;
    }


Client

public class Testmain {public
    static void Main (string[] args) {
        System.out.println (Fontfactory.getfont ("A"));
        System.out.println (Fontfactory.getfont ("B"));
        System.out.println (Fontfactory.getfont ("A"));
    }

The output is as follows:
com.devil.designmodel.flyweight.bfont.bfont@15db9742
Com.devil.designmodel.flyweight.bfont.BFont @6d06d69c
com.devil.designmodel.flyweight.bfont.bfont@15db9742

You can see that the same letter, the object used is the same three, extended instances

Scene: In the above scene, we only judge the letters. If our text still has the color and the font size. If our specific implementation classes are divided into numbers and letters. By adding logic to the judgment of the methods in the factory, we can achieve these problems.

Abstract Enjoy

Public abstract class Font {
    String size;
    String color;

    Abstract Font GetFont ();

    Public String GetSize () {return
        size;
    }

    public void SetSize (String size) {
        this.size = size;
    }

    Public String GetColor () {return
        color;
    }

    public void SetColor (String color) {
        This.color = color;
    }

}

specific enjoy meta class

public class Charfont extends Font {
    private String tchar;

    Public Charfont (String tchar, string size, string color) {
        This.tchar = TCHAR;
        this.size = size;
        This.color = color;
    }

    @Override
    String CreateFont () {return ' This is
        charfont ';
    }

    Public String Gettchar () {return
        tchar;
    }

    public void Settchar (String tchar) {
        This.tchar = TCHAR;
    }

}
public class Numberfont extends Font {
    private Integer num;

    Public Numberfont (Integer num, string size, string color) {
        this.num = num;
        this.size = size;
        This.color = color;
    }

    @Override
    String CreateFont () {return ' This is
        numberfont ';
    }

    Public Integer Getnum () {return
        num;
    }

    public void Setnum (Integer num) {
        this.num = num;
    }

}

enjoy Yuan factory

public class Fontfactory {
    private static Map<integer, font> Numfont = new Hashmap<integer, font> ();
    private static map<string, font> Charfont = new hashmap<string, font> ();

    public static Numberfont Getnumfont (Integer num, string size, string color) {
        Font f = numfont.get (num);
        if (f = = NULL | |!f.getsize () equals (size) | |!f.getcolor (). Equals (color)) {
            F = new Numberfont (num, size, color); 
  
   numfont.put (num, f);
        }
        return (Numberfont) F;
    }

    public static Charfont Getcharfont (string tchar, string size, string color) {
        Font f = charfont.get (TCHAR);
        if (f = = NULL | |!f.getsize () equals (size) | |!f.getcolor (). Equals (color)) {
            F = new Charfont (TCHAR, size, color); 
   charfont.put (TCHAR, f);
        }
        return (Charfont) F;
    }
}
  

The factory separates the two categories and validates the color and font sizes to ensure uniqueness and reliability.

more patterns: one day a design pattern-classification and six major principles

more Source: Https://github.com/oDevilo/Java-Base

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.