Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Design Patterns

Reply
Thread Tools

Design Patterns

 
 
Arved Sandstrom
Guest
Posts: n/a
 
      02-06-2013
On 02/05/2013 01:53 PM, Daniel Pitts wrote:
> On 2/4/13 6:31 PM, Arne Vajh°j wrote:
>> On 2/4/2013 11:44 AM, Stefan Ram wrote:
>>> Joerg Meier <(E-Mail Removed)> writes:
>>>> While we are talking about design patterns, you should be aware that
>>>> a lot
>>>> of people now consider Singletons an antipattern. Your usage of them
>>>> certainly sounds like the justly despised "global variable" replacement
>>>> many people abuse them for. Might be a good idea to reconsider that
>>>> design.
>>>
>>> Pattern or anti-pattern, I never encountered a situation where I
>>> felt a
>>> need for ╗singletonsź.

>>
>> Other have.
>>
>> GoF has it.
>>
>> Spring has had it since 1.x.
>>
>> EJB got it in 3.1.
>>
>> Implementations and usage are very different, but the idea of
>> everybody using the same object is the same.
>>
>> Arne

>
> I think the real anti-pattern is the common implementation of how to get
> the value of the singleton. Singleton's are useful, but when the
> singleton status of an object is enforced beyond reason, you end up with
> with all kinds of "work-arounds" to the fact that the object is a
> singleton. Dependency Injection can help alleviate some of those
> problems, by making the singleton nature of the object a consequence of
> it only being instantiated by the framework, rather than by the class
> itself being a "singleton class".
>


Exactly so. Let it be a "singleton" only because an external framework
is tracking that there is one instance, and you obtain the instance via
the framework.

AHS
 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-06-2013
On 2/5/2013 12:53 PM, Daniel Pitts wrote:
> On 2/4/13 6:31 PM, Arne Vajh°j wrote:
>> On 2/4/2013 11:44 AM, Stefan Ram wrote:
>>> Joerg Meier <(E-Mail Removed)> writes:
>>>> While we are talking about design patterns, you should be aware that
>>>> a lot
>>>> of people now consider Singletons an antipattern. Your usage of them
>>>> certainly sounds like the justly despised "global variable" replacement
>>>> many people abuse them for. Might be a good idea to reconsider that
>>>> design.
>>>
>>> Pattern or anti-pattern, I never encountered a situation where I
>>> felt a
>>> need for ╗singletonsź.

>>
>> Other have.
>>
>> GoF has it.
>>
>> Spring has had it since 1.x.
>>
>> EJB got it in 3.1.
>>
>> Implementations and usage are very different, but the idea of
>> everybody using the same object is the same.

>
> I think the real anti-pattern is the common implementation of how to get
> the value of the singleton. Singleton's are useful, but when the
> singleton status of an object is enforced beyond reason, you end up with
> with all kinds of "work-arounds" to the fact that the object is a
> singleton.


What workarounds are you thinking of?

Arne


 
Reply With Quote
 
 
 
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-06-2013
On 2/5/2013 12:25 AM, Lew wrote:
> Peter Duniho wrote:
>> _Needed_ is situation-specific, but examples generally will include
>> situations where multiple callers require access to some shared resource
>> abstracted by the singleton class. In some cases, a static class suffices.

>
> Nitpick: "static class" is the wrong term. I figure you mean a "utility class",
> or class with only static members.
>
>> But in other cases it's either useful or required to have a singleton (e.g.
>> because you need the singleton to implement an interface, something static
>> classes in Java can't do).

>
> Static classes certainly can implement interfaces. It's non-instantiable
> classes that can't, and classes containing only static methods.


I think you can assume he use "static class" in the C# meaning not the
Java meaning.

Arne

 
Reply With Quote
 
Arne Vajh├Şj
Guest
Posts: n/a
 
      02-06-2013
On 2/5/2013 4:43 AM, lipska the kat wrote:
> On 05/02/13 05:03, Peter Duniho wrote:
>> On 5 Feb 2013 04:10:11 GMT, Stefan Ram wrote:
>>
>>> Arne Vajh├Şj<(E-Mail Removed)> writes:
>>>>> Pattern or anti-pattern, I never encountered a situation where I
>>>>> felt a
>>>>> need for ┬╗singletons┬ź.
>>>> Other have.
>>>
>>> Maybe someone can come up with an SCSE where a singleton is needed.

>>
>> _Needed_ is situation-specific, but examples generally will include
>> situations where multiple callers require access to some shared resource
>> abstracted by the singleton class. In some cases, a static class
>> suffices.
>> But in other cases it's either useful or required to have a singleton
>> (e.g.
>> because you need the singleton to implement an interface, something
>> static
>> classes in Java can't do).

>
> OK, I know I'm probably still being ignored because I've upset some
> people but can someone please explain to me exactly what a 'static
> class' is (WRT Java of course)
>
> This is illegal
>
> public static class Foo{...
>
> a class with all static methods certainly can implement an interface
>
> This compiles
>
> public class Foo implements Serializable{
> public static void Bar(){
> }
> }


In Java you can use:

package demo;

public class Foo {
public static class Bar {
...
}
...
}

in which case Bar is just a class with the name
demo.Foo.Bar.

You can also have:

package demo;

public class Foo {
public class Bar {
...
}
...
}

in which case instances of Bar is tied to instances of
Foo (access to its members).

In C# you have:

namespace Demo
{
public class Foo {
public class Bar {
...
}
...
}
}

which is the equivalent of the first Java example (with static).

You can also have:

namespace Demo
{
public static class Bar {
...
}
public class Foo {
...
}
}

in which case Bar is a class that can only have static methods
(and can not implement interfaces).

Arne

 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-06-2013
On 2/5/2013 4:03 PM, Lew wrote:
> lipska the kat wrote:
>> OK, I know I'm probably still being ignored because I've upset some
>> people but can someone please explain to me exactly what a 'static
>> class' is (WRT Java of course)

>
> RTFM:
> http://docs.oracle.com/javase/specs/...html#jls-8.5.1
>
> The original statement was inaccurate, as you point out.
>
> What he meant must have been something like "except for static methods, which cannot
> implement an interface".


He just left out "C#" in the text.

Arne


 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-06-2013
On 2/5/2013 1:20 PM, Eric Sosman wrote:
> On 2/5/2013 12:51 PM, Stefan Ram wrote:
>> Eric Sosman <(E-Mail Removed)> writes:
>>>> Maybe someone can come up with an SCSE where a singleton is needed.
>>> Runtime.getRuntime().exit(0);

>>
>> The library (Java SE) could have been defined to allow:
>>
>> class Main
>> { public static void main( final java.lang.Runtime runtime )
>> { runtime.println( runtime.getArgc() + " command-line arguments." );
>> runtime.exit(); }}
>>
>> or - with less changes to the current state of Java - to allow:
>>
>> Runtime.exit( 0 );

>
> A singleton class can be transformed into an uninstantiable
> class having only static methods. An uninstantiable class with
> only static methods can be transformed into a singleton class.
> The two designs are duals: Why should one be deprecated and the
> other preferred?


I would in most cases with more serious code prefer singleton
due to its interface capability.

For throw away code I would probably go for the static just
because I am a lazy bastard.

> If all-static vs. singleton is the most pressing problem
> someone faces, he has an easy life indeed!


I agree with that.

Arne


 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-06-2013
On 2/4/2013 11:10 PM, Stefan Ram wrote:
> Arne Vajh°j <(E-Mail Removed)> writes:
>>> Pattern or anti-pattern, I never encountered a situation where I felt a
>>> need for ╗singletonsź.

>> Other have.

>
> Maybe someone can come up with an SCSE where a singleton is needed.


Needed as in "problem can not be solved without"? I doubt such exist!

Needed as in "a large of programmers prefer it to solve the problem"?
There are many. Connection pools, configuration settings, web app
statistics etc..

Arne



 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      02-06-2013
Arne Vajh°j wrote:
> I think you can assume he use "static class" in the C# meaning not the
> Java meaning.


Wait, isn't this comp.lang. *java* .programmer?

I'm in the wrong newsgroup!

No, we do not assume any such thing.

--
Lew

 
Reply With Quote
 
Arne Vajh°j
Guest
Posts: n/a
 
      02-06-2013
On 2/5/2013 8:51 PM, Lew wrote:
> Arne Vajh°j wrote:
>> I think you can assume he use "static class" in the C# meaning not the
>> Java meaning.

>
> Wait, isn't this comp.lang. *java* .programmer?
>
> I'm in the wrong newsgroup!


It happens that other languages come up here.

> No, we do not assume any such thing.


Well - his description fit with C# semantic and it is
rather well known that he uses C#, so it would be a
very natural assumption.

Arne


 
Reply With Quote
 
Daniel Pitts
Guest
Posts: n/a
 
      02-06-2013
On 2/5/13 5:34 PM, Arne Vajh°j wrote:
> On 2/4/2013 11:10 PM, Stefan Ram wrote:
>> Arne Vajh°j <(E-Mail Removed)> writes:
>>>> Pattern or anti-pattern, I never encountered a situation where I felt a
>>>> need for ╗singletonsź.
>>> Other have.

>>
>> Maybe someone can come up with an SCSE where a singleton is needed.

>
> Needed as in "problem can not be solved without"? I doubt such exist!
>
> Needed as in "a large of programmers prefer it to solve the problem"?
> There are many. Connection pools, configuration settings, web app
> statistics etc..


Logging is another one. They thing about all those is that there is a
globally accessible shared instance, but that doesn't have to be
enforced by the class itself. Rather, the life-cycle of the object is
managed externally.

Self-managed life-cycles should be very rare. For instance, something
which actually manages external resources may need to enforce some
invariants about its life-cycle, in order to manage external state in a
sane manor. This isn't something an application programmer need worry
about on a frequent basis.

I'm *not* a fan of a global Configuration object for many reasons. One
is that the configuration class becomes overly involved with the rest of
the application. At least, if they treat it as a place to "go grab some
settings". If the settings are injected, then that's a different story.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Has anybody here read Design Patterns Explained: A New Perspective on Object-Oriented Design John Java 0 06-01-2007 02:45 PM
Design Pattern Relationship Diagram - Design Patterns - Gang of Four Tim Smith C++ 2 12-15-2004 05:22 PM
ebook on Software Design Patterns (C#.NET) Siz ASP .Net 1 12-12-2004 09:32 PM
New open source software to use Design Patterns Josh28 ASP .Net 0 11-29-2004 10:54 AM
where to find good patterns and sources of patterns (was Re: singletons) crichmon C++ 4 07-07-2004 10:02 PM



Advertisments