Enjoy meta mode (Flyweight)

Source: Internet
Author: User

The main purpose of the enjoy meta-mode is to share the shared pool, which can reduce the overhead of memory when there are many objects in the system, and is usually used in conjunction with the Factory mode.

Flyweightfactory is responsible for creating and managing the unit of entitlement, and when a client requests, the factory checks to see if there are any eligible objects in the current object pool, and if so, returns an already existing object, and if not, creates a new object, flyweight is a superclass. A reference to the shared pool, we can easily associate with the Java JDBC Connection pool, think of the characteristics of each connection, we can not be difficult to summarize: for the sharing of some objects, they have a number of common properties, take the database connection pool, URL, Driverclassname, Username, password and dbname, these properties are the same for each connection, so it is appropriate to use the enjoy meta-mode to process, build a factory class, the above-mentioned similar attributes as internal data, and other as external data, in the method call, as a parameter passed in, This saves space and reduces the number of instances.

Look at an example:

Look at the code for the database connection pool:

[Java]View Plaincopy
  1. Public class ConnectionPool {
  2. Private Vector<connection> Pool;
  3. /* Public Properties */
  4. Private String url = "Jdbc:mysql://localhost:3306/test";
  5. Private String username = "root";
  6. Private String password = "root";
  7. Private String driverclassname = "Com.mysql.jdbc.Driver";
  8. Private int poolsize = 100;
  9. Private Static ConnectionPool instance = null;
  10. Connection conn = null;
  11. /* Construction method, do some initialization work */
  12. Private ConnectionPool () {
  13. Pool = new vector<connection> (poolsize);
  14. for (int i = 0; i < poolsize; i++) {
  15. Try {
  16. Class.forName (Driverclassname);
  17. conn = drivermanager.getconnection (URL, username, password);
  18. POOL.ADD (conn);
  19. } catch (ClassNotFoundException e) {
  20. E.printstacktrace ();
  21. } catch (SQLException e) {
  22. E.printstacktrace ();
  23. }
  24. }
  25. }
  26. /* Return connect to Connection pool */
  27. Public synchronized void release () {
  28. POOL.ADD (conn);
  29. }
  30. /* Returns a database connection in the connection pool */
  31. Public synchronized Connection getconnection () {
  32. if (Pool.size () > 0) {
  33. Connection conn = pool.get (0);
  34. Pool.remove (conn);
  35. return Conn;
  36. } Else {
  37. return null;
  38. }
  39. }
  40. }
Through the management of connection pool, it realizes the sharing of database connection, does not need to recreate the connection every time, saves the cost of database re-creation, and improves the performance of the system!

Enjoy meta mode (Flyweight)

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.