Thinking in Patterns chapter 4: Object quantity

Thinking in Patterns with Java V0.9: chapter 4: Object quantity: TIPatterns.htm#_Toc41169694

1, The sections name of today's learning;

Singleton. 25

  Exercises. 27

Object pool 27

  Exercises

 

2, What is it talk about in these sections and my comments and feeling for the content;

   1), Singleton just like youself, only one, can't be duplicated, this can be used in a stiuation that only one object of this class can be created.

   Bruce speaks a lot about avoiding the singleton object to be duplicated by a new() method or clone() method. He puts up with these ways:

   1>, "make all constructors private, and you must create at least one constructor";

   2>, preventing to be cloned: 

   "making the class final prevents cloning";

   "if you’re inheriting from a class hierarchy that has already overridden clone( ) as public and implemented Cloneable, the way to prevent cloning is to override clone( ) and throw a CloneNotSupportedException as described in Appendix A of Thinking in Java, 2nd edition."

   2), Object pool is only limited quantity object can be created. The quantity of objects in pool will reduce one if use one, the object pool will empty if all the objects are used, unless someone return to unused state.

   A question: if the objects in pool will be destroyed in a fixed period regardless of their states is used or unused, how can I use the object at anytime? Some virtual host provider will clear database connections in this way. My anwser is: Before getting connection from pool, check if there are some objects in the pool, if no any object in pool, then call pool manager to create these limited quantity objects again. 

3, My questions about the program itself;

   1), How JVM works when create a object with class's private constructor?

   2), How to use method clone()?

   3), Static inner class

   4), a class can be singleton by make all the method static

   5), Every chapter's exercises will not to be done this time's reading. 

4, My questions about the english language;

  1), Strange words:

  synthesizing

                                           incomplete

One thought on “Thinking in Patterns chapter 4: Object quantity”

  1. From: outline of GoF's 23 Design Patterns

    I can see that only Singleton is a pattern in GoF's 23 Design Patterns, Object Pool written by author is only an extention of Singleton Pattern.

    My answer about question:

       2), How to use method clone()?

       3), Static inner class

    2), How to use method clone()?

    See: Thinking in Java, 3rd ed. Revision 4.0  A: Passing & Returning Objects ./html/TIJ319.htm

    3), static inner class

    See: Java中的静态内部类(static inner class)

Comments are closed.