In this tutorial I end my Code Refactoring Tutorial. It was a big one and between it and my Design Patterns Video Tutorial you should be able to do most anything.
To end this tutorial, I’ll cover the Abstract Factory Pattern again to make sure you understand it. It is often considered a hard to understand pattern, but I’m sure you’ll get it after this video. The code that follows will help explain anything you miss in the video.
If you like videos like this, it helps to tell Google+ [googleplusone]
Code From the Video
Hey Derek, you could try to reduce the speaking that make things seems to obvious when this is not to all? Is kind of frustrating =/
Please send me any questions that you have. I’m happy to answer them. The reason I do that is because positive reenforcement helps some people. I know it isn’t the easiest stuff 🙂
Hi Derek I have been following along with your Reflection and Refactoring tutorials and have learned a lot, thanks.
As practice I tried to implement both in my programs and have a specific question.
I use a factory to build my board game tokens and put them in an arraylist. It works perfectly. The token factory uses reflection:
public GamePieces makeToken (int i) {
try{
Constructor constructor;
constructor = Class.forName( “com.doka.boardgamezoo.”+getType(i) ).getConstructor(Integer.TYPE);
return (GamePieces) constructor.newInstance(i);
}
(getType just returns “player”, “enemy”, “boss” etc).
Now my problem: I moved the call to the factory into an abstract final (utility) class – with a private constructor – which I use only to set up the initial game. And now I get a InvocationTargetException NullPointer error. Can Reflection only be called from concrete classes? Is there any pointer you could give, as to where to look for the error, with only this to go on?
The call to the factory is simple (and works in the Controller class):
private void setTokensFromFactory(Resources res) {
tokenSet = new ArrayList(numberTokens);
TokenFactory factory = new TokenFactory();
for (int i = 0; i < numberTokens; i++) {
tokenSet.add(factory.makeToken(i));
}
}
(And you may have noticed I am on Android – but I assume this does not matter….famous last words.
Cheers for any direction you can give.
Neil
Sorry – I meant I moved the call to a final (utility) class with only static methods….can’t be final and abstract.
You have a NullPointerException. Don’t worry about the InvocationTargetException. This occurs when you try to access or modify an object set as null. You’ll find the line this occurs on in the error message. I hope that helps
Yeah, I know the line – it is the newInstance that is causing the problem I think. But I still cannot find a reason why it works in the main Class but not when pulled out into a utility Class. All my variables have values, the constructor has the right name etc. I think it even works for i=0 but then falls over.
I will chalk it up to “trying to run before I can walk” lol.
Thanks for your time anyway.
Neil
Wow this really cleared things up for me. So can you explain how does singleton come into the picture for abstract classes? Which is the object that is only being created once that necessitates a Singleton?