Conversation started Feb 5, 2014 at 17:08.
Feb 5, 2014 17:08
Is this useful, or just another example of rampant Architecture Austronauty at work?
Where would you use it?
@MichaelT Looks OK to me, though it borderlines on discussion.
user55340
@RobertHarvey Its a hot question that's been sticking to the list recently...
user55340
@RobertHarvey I think the goal is "everywhere" though I think it is also subject to thecodelesscode.com/case/109
@MichaelT That sounds like "second system effect."
user55340
@RobertHarvey I was just curious if it is suffering from the same things that other sites have (lots of meh answers) but with SO's more active 20k+ people, the poor answers have been deleted... or if its showing everything that has been answered.
user55340
@RobertHarvey Yep. "I built a factory twice, now I'll build one that does everything so I won't have to build it again."
Feb 5, 2014 17:13
@MichaelT It's got two deleted answers.
@MichaelT Got it.
user55340
The thing is, they don't realize that they will have factories that don't fit that model... and will have to build another one... or extend that one to handle the new factory type. And eventually will have a frankensiten class.
user55340
(btw, #108 thecodelesscode.com/case/108 plays very nicely into #109)
Did you memorize all these? :)
user55340
@RobertHarvey Not all, though there are certain pairings that become useful. There are characters who have a theme - following one character follows the theme. thecodelesscode.com/names/Djishin
1
A: What is the shortcoming of custom factory comparing to generic factory

Robert HarveyGeneric versions of algorithms generally provide type safety. Your version accepts a string to specify the type. That string could be anything, and the compiler cannot check it for you to see if it is correct. If you specify a type in your version that you haven't registered, you won't know th...

Referenced the koans in this answer.
...
Factories always seem to be such an overblown concept. Am I wrong in thinking of factories as simply a static Create() method? Or DI as handing dependencies to a constructor class?
user55340
Feb 5, 2014 17:25
@RobertHarvey I've found that properly done, factories can be good... but then again I'm dealing right now with a factory for 40 someodd classes. Passing a json and build the right one.
How does it pick the correct type?
user55340
The json object has a field: 'key'. This key is the value in an annotation. The factory finds the class with the matching annotation, instantiates it and passes the json object to its constructor which then deseralizes it.
So basically a big case statement.
user55340
(the class is in a common jar file that is used in two different web applications that speak to each other through json objects that searlize and desearlize from the shared jar)
user55340
@RobertHarvey Yep, but the code is not a switch at all.
Feb 5, 2014 17:28
It loops through some data structure where you've registered the types.
Or finds them with Teh Reflexions.
user55340
Yes - but it doesn't need to be updated when a new type is added, deleted, or changes.
user55340
Teh Reflexions. Annotations make that process a bit safer.
The program I'm working on has an interface that three different types implement. The factory method is in a static class... it just picks one type based on the value of a stringly-typed parameter.
@MichaelT So basically it will tell you all of the types that fulfill a particular interface?
user55340
Feb 5, 2014 17:31
 Reflections reflections = new Reflections("my.project.prefix");
 Set<Class<?>> annotated =
           reflections.getTypesAnnotatedWith(SomeAnnotation.class);
Very cool
user55340
And annotations themselves contain data about the class.
In C#, it would be
var type = typeof(IMyInterface);
var types = AppDomain.CurrentDomain.GetAssemblies()
    .SelectMany(s => s.GetTypes())
    .Where(p => type.IsAssignableFrom(p));
user55340
 Set<Class<? extends SomeType>> subTypes =
           reflections.getSubTypesOf(SomeType.class);
user55340
That's the matching one for that block.
user55340
Feb 5, 2014 17:34
But consider (this is what I worked on with Ampt) that you have a class that can handle files ending in ".txt" or ".text". And now you have that information in the annotation so you can find that class easily with the annotation.
Interesting. So you map the stringly data types to the names of your annotations?
user55340
Values in the annotation itself.
Ah, right.
user55340
In this case I've got: @Key("reportFoo")
user55340
and now when I want to find that one - "reportFoo".equals(c.getAnnotation(Key.class).value())
user55340
Feb 5, 2014 17:36
Though realize that's a contrived version - its not a string constant that's there.
user55340
And while the essence of an annotation based factory is useful to understand, I would hate to try to make a factory that would work equally well for my reports and Ampts web pages.
But the factory doesn't seem all that complicated anyway.
Not when it is explained like this. :)
user55340
Its not. Its just someone being astronaughty of "oh, this could be abstracted away to a abstract abstract factory..." which... well... becomes a frankenstien when faced with actual problems.
So how do you differentiate the practical factory from the one that lacks oxygen?
user55340
The factory class for those 40 reports... its got 3 different forms of the factory (for different annotations) and is < 50 lines long including imports.
user55340
Feb 5, 2014 17:41
And... looking at it... I could abstract those 3 into one method that is passed a class object itself... ohh... tempting.
user55340
@RobertHarvey It does what its supposed to and nothing more.
user55340
YAGNI.
 
Conversation ended Feb 5, 2014 at 17:42.