Number of lines | Number of spaces corresponding to a line | Number of Hash symbol |
---|---|---|
1 | 3 | 2 |
2 | 2 | 4 |
3 | 1 | 6 |
4 | 0 | 8 |
5 | 0 | 8 |
6 | 1 | 6 |
7 | 2 | 4 |
8 | 3 | 2 |
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
may be not but since the class is stateless i thought it should be and also the joshua bloch has mentioned it to enforce this pattern for stateless classes.salvin wrote:Is a singleton necessary in the above program ?
salvin wrote:Even if it is, you are eagerly initializing it here:
?
1
private static final DiamondPrinter printer = new DiamondPrinter();
Why do you need to use instanceCounter to ensure it's a singleton ?
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
All things are lawful, but not all things are profitable.
praveen kumaar wrote:what's wrong in iniltializing it there.suppose by mistake if i instantiate the class anywhere else in my code then the counter will help me to remind this property by throwing an exception.
praveen kumaar wrote:may be not but since the class is stateless i thought it should be and also the joshua bloch has mentioned it to enforce this pattern for stateless classes.
salvin francis wrote:I am really curious about this. Could you quote your source where bloch has asked to enforce this ? The way I always thought about stateless class was a class which had static methods and didn't use instance variables like the way you did.
I wrote:Line 15 is also misleading in that it claims to introduce a "static factory" where the method doesn't really fit the definition of a factory method.
Knute Snortum wrote:I like that you ...
Carlos Reves wrote:
Knowing this we can print the shape with one simple loop...
Don't get me wrong, your math is exact!
But i would rather do it simpler...
There are three kinds of actuaries: those who can count, and those who can't.
Apart from the things junilu mentioned in his post,read item 21 in the joshua book(2nd edition,effective java) where he uses the "singleton pattern" for the "functional object".part of the his quote issalvin wrote:I am really curious about this. Could you quote your source where bloch has asked to enforce this ? The way I always thought about stateless class was a class which had static methods and didn't use instance variables like the way you did. Maybe I am wrong here ?
For a reference the page number is 103 and 104 in the 2nd edition.joshua bloch wrote:As is typical for concrete strategy classes, the StringLengthComparator
class is stateless: it has no fields, hence all instances of the class are functionally equivalent. Thus it should be a singleton to save on unnecessary object creation
costs (Item 3, Item 5):....
Yes i have called it a static factory in the same sense joshua mentioned and even i have use the same name(for method) and pattern he mentioned.Junilu wrote:Ok, I dug through Bloch 2nd Ed. and Item 3 does refer to the getInstance() method in the singleton example as a static factory method. Bloch writes further, however, stating that "One advantage of the factory-method approach is that it gives you the flexibility to change your mind about whether the class should be a singleton without changing its API." This an important nuance to note, something I have to admit I never really thought much about before but it does make sense to call it a factory method if you're thinking along those lines. You learn something new everyday.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
In fact when i am posting my code,i do not want to add comments.despite of adding comments i want my code should be appealing itself(every line).the part of my program which is the difficult to parse is the logic of printing spaces and hash symbols at each line which i have already explained at the very first in my post.and other part,i think,is obvious.i will keep it in mind next time that i should comment only those lines which are less appealing.Junilu wrote:2. Your comments don't follow a consistent pattern and most of them are not really necessary. Line 12 for instance is only stating the obvious. Line 15 is also misleading in that it claims to introduce a "static factory" where the method doesn't really fit the definition of a factory method. (EDIT: Bloch does in fact call getInstance() a static factory. See my other reply below)
I have read a kind of same thing in a "Clean Code".actually i take a look at the part in a book where it is advised to organize the code just in a way the things are happening in it(so that we can translate the code in english sentence as we going down the code).Also i had read a little about the SLAP(single level of abstraction of rinciple),mentioned by you in some other thread and it is also there in clean code.does my code follows it.junilu wrote:This is more a matter of style/form than anything but it also has to do with readability. I read code from top to bottom. I expect high-level public code to be up top and detailed private code to be down towards the bottom. I also like to keep static members together towards the top of the class and instance members below them. I find that this organization generally allows me to scan a class and get a general feel for its capabilities much faster. Thus, in my code, where there is a main() method, it is generally one of the first methods encountered when reading from top to bottom, followed by whatever other static methods there may be, followed by constructors, then high-level public methods, and then the more detail/implementation-focused private methods to round out the bottom of the class.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
All things are lawful, but not all things are profitable.
praveen kumaar wrote:Apart from the things junilu mentioned in his post,read item 21 in the joshua book(2nd edition,effective java) where he uses the "singleton pattern" for the "functional object".part of the his quote is
joshua bloch wrote:As is typical for concrete strategy classes, the StringLengthComparator
class is stateless: it has no fields, hence all instances of the class are functionally equivalent. Thus it should be a singleton to save on unnecessary object creation
costs (Item 3, Item 5):....
Knute Snortum wrote:In lines 26 and 36, 9 is still a "Magic number". Change HEIGHT and the program breaks.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
praveen kumaar wrote:... i can ask a client to set HEIGHT by providing a single int parameter constructor in my printer class.
Try to enjoy your work while doing it,it will Automatically convert in Hard Work...
Campbell Ritchie wrote:I would prefer PK's approach with two methods. I prefer your for loop form.
I wrote:To me, it's best to learn how to work at both ends of the spectrum and then figure out how to get a feel for how to make a good compromise between the two extremes in situations where it's critical to balance both time and quality. The most useful skills to have that allow you to walk that fine line are refactoring and recognizing code smells so don't pass up any opportunity to learn how to do those better.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |