Home > Java > Factory Pattern

Factory Pattern

September 18, 2015 Leave a comment Go to comments

Factory pattern is used when we have a super class with multiple sub-classes and based on input, we need to return one of the sub-class. This pattern take out the responsibility of instantiation of a class from client program to the factory class so logic instance creation is hidden from client. Super class in factory design pattern can be an interface, abstract or a normal java class.

Factory pattern example.

factory-pattern-diagram

Factory Pattern Class Diagram

SocialMediaType.java

public enum SocialMediaType {
   TWITTER,PATH,FACEBOOK;
}

SocialMedia.java

public abstract class SocialMedia {

	public SocialMedia(SocialMediaType type) {
		this.type = type;
	}

	private SocialMediaType type;

	public SocialMediaType getType() {
		return type;
	}

	public void setType(SocialMediaType type) {
		this.type = type;
	}
	protected abstract void uniqueness();
}

Facebook.java

public class Facebook extends SocialMedia{
	public Facebook() {
		super(SocialMediaType.FACEBOOK);
		uniqueness();
	}

	@Override
	protected void uniqueness() {
	    System.out.println("This is uniqueness of Facebook");
	}
}

Path.java

public class Path extends SocialMedia{

	public Path() {
		super(SocialMediaType.PATH);
		uniqueness();
	}
	@Override
	protected void uniqueness() {
		 System.out.println("This is uniqueness of Path");
	}
}

Twitter.java

public class Twitter extends SocialMedia{

	public Twitter() {
		super(SocialMediaType.TWITTER);
		uniqueness();
	}

	@Override
	protected void uniqueness() {
		 System.out.println("This is uniqueness of Twitter");
	}

}

SocialMediaFactory.java

public class SocialMediaFactory {
	private static SocialMedia socialMedia;
	public static SocialMedia getSocialMedia(SocialMediaType type) {
		switch (type) {
		case PATH:
			   socialMedia = new Path();
			break;
		case TWITTER:
			   socialMedia = new Twitter();
			break;
		case FACEBOOK:
			   socialMedia = new Facebook();
			break;
		default:
			break;
		}
		return socialMedia;
	}
}

App.java

public class App
{
    public static void main( String[] args )
    {
        System.out.println(SocialMediaFactory.getSocialMedia(SocialMediaType.FACEBOOK));
        System.out.println(SocialMediaFactory.getSocialMedia(SocialMediaType.PATH));
        System.out.println(SocialMediaFactory.getSocialMedia(SocialMediaType.TWITTER));
    }
}

Result:

Factory Pattern

Factory Pattern

Advertisements
Categories: Java Tags: , ,
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: