Abstract Factory Pattern

  • Abstract Factory Design pattern is much similar to Factory design pattern
  • The only difference is that the Factory pattern the is only Interface method that returns a Concrete object where as in Abstract Factory Pattern there multiple methods that return multiple objects

Example :

package designpattern;

enum OS{
	LINUX,WINDOWS,MAC;
}
interface UI{
	public String getTextEditory();
	public String getBrowser();
	public String getPhotoEditor();
}
class Linux implements UI{
	public String getTextEditory() {	return "VI Editor";	}
	public String getBrowser() {		return "Chromium";	}
	public String getPhotoEditor() {	return "ShortWell";	}
}
class Windows implements UI{
	public String getTextEditory() {	return "Notepad";	}
	public String getBrowser() {		return "IE";	}
	public String getPhotoEditor() {	return "Paint";	}	
}
class Mac implements UI{
	public String getTextEditory() {	return "CityMac";	}
	public String getBrowser() {		return "Safari";	}
	public String getPhotoEditor() {	return "Pixlr";	}	
}
class UIFactory{
	public static UI getUIInstance(OS os) {
		UI ui = null;
		switch(os) {
			case LINUX: ui = new Linux(); break;
			case WINDOWS: ui = new Windows(); break;
			case MAC: ui = new Mac();	 break;
		}
		return ui;
	}
}
public class AbstractFactoryDemo {
	public static void main(String[] args) {
		UI ui = UIFactory.getUIInstance(OS.WINDOWS);
		System.out.println(ui.getTextEditory());
		System.out.println(ui.getBrowser());
		System.out.println(ui.getPhotoEditor());
	}
}

Reference :

Leave a Comment