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 >>

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
Java class can be private?
We can not declare top level class as private. Java allows only public and default modifier for top level classes in java. Inner classes can be private.
Famous Quotations
It is amazing what you can accomplish if you do not care who gets the credit.
-- Harry Truman

About Author

I'm Nataraja Gootooru, programmer by profession and passionate about technologies. All examples given here are as simple as possible to help beginners. The source code is compiled and tested in my dev environment.

If you come across any mistakes or bugs, please email me to [email protected].

Most Visited Pages

Other Interesting Sites

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