JAVA EXAMPLE PROGRAMS

JAVA EXAMPLE PROGRAMS

Publish Your Article Here

Factory Pattern (Factory Method Pattern)


The factory method pattern is a creational pattern. It deals with the problem of creating objects without specifying the exact class of object that will be created. Lets brief this, in real programing world, we need to create different objects with similar functionality. Lets say, we need Currency objects based on country. Now you define an interface called Currency, and specify your functionality in the form of method. Now implement this interface for different countries, and each method implementation will be specific to the country.

Now how can I create objects? Create a factory class and provide a static method to get required object. This method should take input, here input can be country name. Based on the country value, create specific country object, and return it. In this page you can see java code example for this.

Now lets come to the definitions: the factory method pattern is a creational pattern which uses factory methods to deal with the problem of creating objects without specifying the exact class of object that will be created. This is done by creating objects via a factory method, which is either specified in an interface and implemented in implementing classes (concrete classes); or implemented in a base class, which can be overridden when inherited in derived classes; rather than by a constructor.

Here is the Currency Interface

package com.java2novice.dp.factory;

public interface Currency {

	public String getCurrency();
	
	public String getSymbol();
}

Implementation class for Indian Currency

package com.java2novice.dp.factory;

public class India implements Currency{

	@Override
	public String getCurrency() {
		
		return "Rupee";
	}

	@Override
	public String getSymbol() {
		
		return "Rs";
	}

	public static void main(String a[]){
		
		India in = new India();
		System.out.println(in.getSymbol());
	}
}

Implementation class for USA Currency

package com.java2novice.dp.factory;

public class USA implements Currency{

	@Override
	public String getCurrency() {
		
		return "Dollar";
	}

	@Override
	public String getSymbol() {
		
		return "$";
	}
}

Factory class to create objects

package com.java2novice.dp.factory;

public class CurrencyFactory {

	public static Currency getCurrencyByCountry(String cnty) throws Exception{
		
		if("IN".equalsIgnoreCase(cnty)){
			return new India();
		} else if("USA".equalsIgnoreCase(cnty)){
			return new USA();
		}
		throw new Exception("Invalid country...");
	}
	
	
	public static void main(String a[]){
		Currency india;
		try {
			india = CurrencyFactory.getCurrencyByCountry("IN");
			System.out.println("Indian currency: "+india.getCurrency());
			System.out.println("Indian currency symbol: "+india.getSymbol());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}

Output:
Indian currency: Rupee
Indian currency symbol: Rs
Next Program >>
blog comments powered by Disqus

Java design pattern examples

  1. Factory Pattern
  2. Abstract Factory Pattern
  3. Builder Design Pattern
  4. Prototype Pattern
  5. Adapter Pattern
  6. Composite Pattern
  7. Proxy Pattern
Knowledge Centre
Can we call servlet destory() from service()?
As you know, destory() is part of servlet life cycle methods, it is used to kill the servlet instance. Servlet Engine is used to call destory(). In case, if you call destory method from service(), it just execute the code written in the destory(), but it wont kill the servlet instance. destroy() will be called before killing the servlet instance by servlet engine.
Famous Quotations
Good judgment comes from experience, and experience comes from bad judgment.
-- Barry LePatner

About Author

Most Visited Pages

Other Interesting Sites

Reference: Java™ Platform Standard Ed. 7 - API Specification | Java is registered trademark of Oracle.
Privacy Policy | Copyright © 2017 by Nataraja Gootooru. All Rights Reserved.