- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
It's IInstanceFactories all the way down!
Admin
Boss:"Can you make it simpler?" "Sure!"
function createFactorySimple() { public IInstanceFactory getInstanceFactory();
public IInstanceFactory getInstance();
public IInstanceFactory getOrderFactory();
public IInstanceFactory getGroupFactory();
public IInstanceFactory getUserFactory();
}
Admin
We need to dig deeper!
public IInstanceFactory getInstanceOfInstance();
public IInstanceFactory getInstanceOfInstanceOfInstanceFactory();
Admin
Not sure this will compile. Interfaces cannot have acess modifiers.
Admin
This is not that bad. At least it's correctly named. We have an EntityManagerInstancesCreator backed by an EntityManagerFactory in our code base.
Admin
Yes they can. They're simply not required because public and abstract are implied. http://docs.oracle.com/javase/specs/jls/se7/html/jls-9.html#jls-9.4: "It is permitted, but discouraged as a matter of style, to redundantly specify the public and/or abstract modifier for a method declared in an interface."
Admin
But what makes the instance that makes the instance factory that makes the instance factory?! Clearly needs moar XML to solve that problem!
Also chickens. And eggs.
Admin
Java is silly, c# would not compile...
Admin
Unless this is C#, in which case access modifiers are forbidden on interfaces.
Admin
While the submitter didn't specify, it's pretty clear that this isn't C#, and it is Java. I sometimes can't be sure from just the code itself, but I could tell this one was Java from across the room.
Admin
That's actually a real design pattern called the abstract factory pattern.
Of course, that doesn't make it any less silly.
Admin
That is not how you implement the Abstract Factory pattern, unless you're building a Factory to return a Factory so that you can instantiate while you instantiate.
Admin
This is never the answer to anything.
Admin
Admin
<answer to="everything"><is><![CDATA[xml]]></is></answer>
Admin
Admin
Q: "What is never the answer to anything?" A: ... Oh, never mind.
Admin
I always thought we needed to make things more difficult or at least redundant i.e.:
Admin
But don't worry, the developer who wrote this had browsed through Gang Of Four's book and was able to state a few design patterns by their names, so he was CLEARLY a good designer.
But then, in a language where everything is an object, I guess everything that creates objects as an IInstanceFactory?
What do you do if you want to create an instance of an IInstanceFactory? I don't see an IInstanceFactoryFactory or is that the difference between getInstanceFactory and getInstance?
Admin
Admin
The factory pattern is a lot of boilerplate implementing a simple functor between categories. The problem isn't the factories, or even nested factories. It's the boilerplate.
In Haskell, the (identity) factory pattern is implemented by:
data F a = F a
instance Functor F where fmap f (F a) = F . f $ a
We can create stacks of functors using functor transformers.
Admin
Was it here that someone pointed out the wonderful AbstractSingletonProxyFactoryBean?
Admin
Thank you Captain Obvious!
Admin
Admin
TL;DR: Haskell is Perl encrypted with a "F" map!
Admin
the real WTF is that the instance Factory is not parametrized.
i.e.
public Interface IInstanceFactory<K extends IInstanceFactory> {...}
and then returning special instances of K!
Admin
could be pre-generics legacy code?
Admin
There are times when unhiding the commentary is not a good thing. It seems Gaza Rullz took care of picturizing it for everyone.
Admin
Special K probably explains a lot more of this than you think.
Admin
CommentFactory.getCommentFactory()
Admin
Unicorns! Unicorns and rainbows!
o_o
Captcha: suscipit
Admin
A: "What is never the answer to anything?" Q: "What is never the answer to anything?"
Admin
If that's too much boilerplate, you can even do:
data F a = F a deriving (Functor)
Or are you whining about the (.) and ($)? The latter is just function application, and the former is function composition.
Admin
+10!
Admin
Which is precisely why I always think the usual factory pattern is broken. Factories should not create instances of themselves - when do you ever go to a real factory and say "Build me another factory like this one!"? Instead, the factory should let you get an instance of a manager who can help you build specialised objects.
Admin
Well, it could go worse, you could have had an abstract EntityManagerFactoryInstanceCreatorFactoryBuilder
captcha: nobis. Bad usage of the factory pattern is a big nobis-nobis
Admin
I don't understand anything of real programming languages like Java or C#, just crappy php
Admin
I don't understand anything if real programming languages such as Java or C#, just crappy php
Admin
Actually in my Dependency Injection library (which is implemented in C++ but could also be implemented in another language) you have Builders and Factories.
You normally get a BuilderFactoryImpl<BuilderX> which derives from BuilderFactory<BuilderX> and creates you a BuilderX which derives from BuilderT<X> and creates you an X or something that derives from X.
This is all done at initiation time, so if you want something that will create objects later your X would be a Factory<Y>. So you can have a factory to a builder of factories.
If you think that is complicated:
BuilderFactoryImpl<T> is actually the only implemented version of BuilderFactory<T>. There isn't actually that much of a need to use a hierarchy here.
The difference between a Builder and a Factory is that a Builder only builds a single object, a Factory can build more than one object.
BuilderFactory instances are often re-used, when you want more than one instance of a class (each of which needs its own builder)
Different Builder implementations normally build different derived classes, but you can have more than one that creates the same class but in a different way (e.g. uses different parameters)
Essentially, and this is the key point you need to know about the AbstractFactory pattern:
The purpose of this pattern is to enable you to create objects from your source code, without know at this point in your code the actual class that you are going to create. You only know the base class from which this class derives.
The Factory Pattern is often the best pattern to use for message handling, whereby you read the "header" then decide what type of message you have and get it to "process".
Quite often you will have some kind of table (key to factory).
Admin
Oh, but this is nothing strange, it's just a factory factory! Not the first time I see this.
Admin
Admin
Exactly.
I still remember when language researchers strived to make their languages look as natural as possible (when "ideal" was considered to be a compiler that understood plain English sentences). At the current rate of "acceleration away from ideal" people will be switching to punch cards soon, just to squeeze out that last remaining drop of "incomprehensible".
Admin
Refactoryfactorying prime numbers is hard.
Admin
(Apple went back to that well and gave us HyperTalk and AppleScript just to really drive the point home!)
Admin
public is a legitimate access modifier for interfaces.
Admin
I always use 'public interface' to remind myself that the interface will be public. This needn't have been the case. It would have been nice if Java had supported interfaces that were package local - i.e. just 'interface'.
This would have allowed libraries to provide multiple implementations of an interface without exposing the interface to random external implementations. (Aside: No serious Java code-base is complete without its own buggy, undocumented, contact-violating implementation of List or Map. And don't get me started on bad implementations of Iterator!)
It would also have allowed the interface to evolve (in some ways e.g. adding methods) between releases. Since the only implementations would be provided in the same package, these could be updated in line with the interface. When an interface became stable -and- it became useful to allow external implementations, then the interface could have become a 'public interface'.
Admin
Admin
What Java doesn't support (for reasons I haven't entirely grasped) is non-public members of interfaces.
Admin
Admin
My pubic interfaces weren't supposed to be public.