Posts

Showing posts with the label Back To Basics

Factory Method Pattern - Misconception

Image
This post does not talk about the Factory Method Pattern, but I would like to discuss about from where it starts. Recently I had the privilege to attend a training on the topic. I do not miss opportunity to attend any session on process or pattern, you always have something to learn. It was an good interactive session but we had the same old discussion that instead getting to the factory method we ended up discussing the implementation (some may call it simple factory idom, but i dont) forgetting that patterns only talk about the behavior and interaction and not about implementation. Let me take a chance to explain the Factory Method Factory Method - I will use the common example of PizzaFactory as used by Head First to explain the factory method: Elements: Pizza (Product) SimplePizzaFactory (Creator) Definition: The essence of the Factory Pattern is to "Define an interface for creating an object, but let the subclasses decide which class to instantiate. The Factory method lets a...

Custom Exception with same name as JDK Exception

If I create an exception example IOException, will there be an conflict with the existion IOException of JDK? In a discussion the qustion popped up for a quick response and most us had it. But if someone has confusions, here is a quick explanation. Lets  create a custom exception IOException as stated below (IOException.java) package my.test; class IOException extends Exception { IOException() { super("MyIOException Has Raised"); } } Next lets create a test program to see what happens (TestException.java). package my.test; public class TestException { public static void main(String[] args) { try { throw new IOException(); } catch(IOException e) { System.out.println(e.getMessage()); } } } When we run the TestException.java file we get the following message: MyIOException Has Raised Now lets include an import statement to import java.io.IOException class. package my.test; import java.io.IOException; public class TestException { public static void main...