Categories
Algorithms & Design Design Pattern

Command Pattern

EquityShare.java

public class EquityShare {

	private String companyName;
	private double price;
	public EquityShare(String companyName, double price) {
		this.companyName=companyName;
		this.price=price;
	}
	
	public void purchaseEquityShare(){
		System.out.println("purchasing equity share : " + this);
	}

	public void sellEquityShare(){
		System.out.println("selling equity share : " + this);
	}
	
	public String toString(){
		return "[ companyName :"+companyName +", price : "+price+"]";
	}
}

BrokerStaff.java

public class BrokerStaff {

	protected EquityShare equityShare;
	public BrokerStaff() {
		// TODO Auto-generated constructor stub
	}

	
	
	public EquityShare getEquityShare() {
		return equityShare;
	}



	public void setEquityShare(EquityShare equityShare) {
		this.equityShare = equityShare;
	}



	public void executeOrder(String orderType){
		if("purchase".equals(orderType))
			equityShare.purchaseEquityShare();
		else if("sell".equals(orderType))
			equityShare.sellEquityShare();
	}
}

CommandPatternDemo.java

public class CommandPatternDemo {

	public CommandPatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {

		//details of Equity Shares
		EquityShare techSoft = new EquityShare("TECHSOFT",1000);
		EquityShare roboSoft = new EquityShare("ROBOSOFT",1500);
		EquityShare radioSoft = new EquityShare("RADIOSOFT",400);
		
		//Get the Broker Staff
		BrokerStaff broker = new BrokerStaff();
		
		//purchase techsoft
		broker.setEquityShare(techSoft);
		broker.executeOrder("purchase");
		
		//sell robosoft
		broker.setEquityShare(roboSoft);
		broker.executeOrder("sell");
		
		//purchase radiosoft
		broker.setEquityShare(radioSoft);
		broker.executeOrder("purchase");
		
	}

}

Output

Categories
Algorithms & Design Design Pattern

Chain Of Responsibility Pattern

CustomerServiceProvider.java

public abstract class CustomerServiceProvider {

	protected CustomerServiceProvider nextProvider;
	public CustomerServiceProvider() {
		// TODO Auto-generated constructor stub
	}
	
	public abstract void attendCustomer();
	public abstract void setNextProvider();

}

AutomatedCustomerServiceProvider.java

import java.util.Scanner;

public class AutomatedCustomerServiceProvider extends CustomerServiceProvider {

	public AutomatedCustomerServiceProvider() {
		// TODO Auto-generated constructor stub
	}

	public  void attendCustomer(){
		System.out.println("--------------------------------------------------------------------------------------");
		System.out.println("Hello I am automated robot to serve you, thank you for calling");
		System.out.println("if you are satisfied, press 0 to exit else press 1 to talk with support staff");
		Scanner sc = new Scanner(System.in);
		if(sc.nextInt()==0){
			System.out.println("Thank you for calling, have a nice day");
			return;
		} else {
			System.out.println("connecting with support staff, pls stay online");
			this.nextProvider.attendCustomer();
		}
	}
	public  void setNextProvider(){
		 nextProvider = new SupportingCustomerServiceProvider();//set support staff as next provider
		 nextProvider.setNextProvider();//set expert as next provider
				 
	}

}

SupportingCustomerServiceProvider.java

import java.util.Scanner;

public class SupportingCustomerServiceProvider extends CustomerServiceProvider {

	public SupportingCustomerServiceProvider() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void attendCustomer() {
		System.out.println("--------------------------------------------------------------------------------------");
		System.out.println("Hello I am support staff to serve you, thank you for calling");
		System.out.println("if you are satisfied, press 0 to exit else press 1 to talk with expert staff");
		Scanner sc = new Scanner(System.in);
		if(sc.nextInt()==0){
			System.out.println("Thank you for calling, have a nice day");
			return;
		} else {
			System.out.println("connecting with expert staff, pls stay online");
			this.nextProvider.attendCustomer();
		}

	}

	@Override
	public void setNextProvider() {
		// TODO Auto-generated method stub
		 nextProvider = new ExpertCustomerServiceProvider();//set support staff as next provider
	}

}

ExpertCustomerServiceProvider.java

import java.util.Scanner;

public class ExpertCustomerServiceProvider extends CustomerServiceProvider {

	public ExpertCustomerServiceProvider() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void attendCustomer() {
		System.out.println("--------------------------------------------------------------------------------------");
		System.out.println("Hello I am expert staff to serve you, thank you for calling");
		System.out.println("Happy to serve you, have a nice day, bye bye");
	}

	@Override
	public void setNextProvider() {
		// TODO Auto-generated method stub

	}

}

ChainOfResponsibilityPatternDemo.java

public class ChainOfResponsibilityPatternDemo {

	public ChainOfResponsibilityPatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		//Create Chain of Customer Service providers robot -> support staff-> expert staff
		CustomerServiceProvider robot = new AutomatedCustomerServiceProvider();
		robot.setNextProvider();//setting next providers as 
		
		//call for service
		robot.attendCustomer();

	}

}

Output

Categories
Algorithms & Design Design Pattern

Proxy Pattern

Account.java

public abstract class Account {
	
	protected String customerName;
	protected int accountId;
	protected String accountType;
	
	public Account(){
		
	}
	public Account(String customerName, int accountId, String accountType) {
		this.customerName=customerName;
		this.accountId=accountId;
		this.accountType=accountType;
	}

	public abstract void displayAccountInfo();
}

SalaryAccount.java

public class SalaryAccount extends Account {

	private double balance;
	private String companyName;
	
	
	public SalaryAccount(String customerName, int accountId, String accountType,String companyName) {
		super(customerName, accountId, accountType);
		this.balance=Math.random()*10000;
		// TODO Auto-generated constructor stub
	}

	@Override
	public void displayAccountInfo() {
		System.out.println("Account Id ::" +this.accountId);
		System.out.println("customer Name ::" +this.customerName);
		System.out.println("accountType ::" +this.accountType);
		System.out.println("balance ::" +this.balance);
		System.out.println("Company Name ::" +this.companyName);
	}

	
}

ProxySalaryAccount.java

public class ProxySalaryAccount extends Account{

	private Account account;
	
	public ProxySalaryAccount(String customerName, int accountId, String accountType) {
		// TODO Auto-generated constructor stub
		super(customerName,accountId,accountType);
	}

	@Override
	public void displayAccountInfo(){
		getAccountDetails();
		account.displayAccountInfo();
	}

	public void getAccountDetails(){
		//loading actual account from database when only required, so that to save memory
		
		if(account==null)
			account = new SalaryAccount(customerName, accountId,accountType,"TechSoft");
	}
	
}

BankOfficer.java

public class BankOfficer {

	
	public BankOfficer() {
		// TODO Auto-generated constructor stub
	}

	public void authenticateCustomer(Account account){
		System.out.println("This is first step to authenticate, if not authenticated we will reject your request");
		System.out.println("You are authenticated : "+account.accountId);
	}
	
	public void addedRequestInQueue(Account account){
		System.out.println("Pls wait for your turn to come, we will call you in some time");
	} 
	
	public void processRequest(Account account){
		System.out.println("Here are your details");
		account.displayAccountInfo();
	}
	public void submitRequest(Account account){
		authenticateCustomer(account);
		addedRequestInQueue(account);
		processRequest(account);
	}

}


ProxyPatternDemo.java

public class ProxyPatternDemo {

	public ProxyPatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
	 //call BankOfficer
		BankOfficer officer  = new BankOfficer();
		
		//initially passing proxy object to avoid loading from database.
		//on actual process of display, real object is created by loading from database.
		//before actual process, there several steps which can work with proxy account.
		//also by authentication, we are controlling access to Original account so for that time only proxy account is used
		officer.submitRequest(new ProxySalaryAccount("Jany Jose", 1234, "Deposit"));

	}

}

Output

Categories
Algorithms & Design Design Pattern

Fly Weight Pattern

Square.java

public class Square {
	private String color;
	public Square(String color) {
		this.color=color;
	}
	public String getColor() {
		return color;
	}
	public void setColor(String color) {
		this.color = color;
	}
	public String toString(){
		return "square with color:"+color;
	}
}

SquareFactory.java

import java.util.HashMap;
import java.util.Map;

public class SquareFactory {

	public SquareFactory() {
		// TODO Auto-generated constructor stub
	}

	private static Map objectMap = new HashMap();
	
	public static Square getSquare(String color){
		Square square = objectMap.get(color);
		if(square==null){
			System.out.println("creating new square with color:"+color);
			square = new Square(color);
			objectMap.put(color, square);
		}else {
			System.out.println("returning existing cached circle");
		}
		return square;
	}
}

FlyWeightPatternDemo.java

public class FlyWeightPatternDemo {

	public FlyWeightPatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		SquareFactory factory = new SquareFactory();
		
		//get me red square
		System.out.println("requested red square : "+factory.getSquare("red"));
		System.out.println("--------------------------------------------------");
		
		System.out.println("requested red square : "+factory.getSquare("yellow"));
		System.out.println("--------------------------------------------------");

		System.out.println("requested red square : "+factory.getSquare("green"));
		System.out.println("--------------------------------------------------");
		
		System.out.println("requested red square : "+factory.getSquare("red"));
		System.out.println("--------------------------------------------------");

	}

}

Output

Categories
Algorithms & Design Design Pattern

Facade Pattern

Shape.java

public abstract class Shape {

	public Shape() {
		// TODO Auto-generated constructor stub
	}

	public abstract void draw();
}

HexagonShape.java

public class HexagonShape extends Shape {

	public HexagonShape() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("drawing the hexagon..");
	}

}

PentagonShape.java

public class PentagonShape extends Shape {

	public PentagonShape() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("Drawing the pentagon..");
	}

}

SquareShape.java

public class SquareShape extends Shape {

	public SquareShape() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("Drawing the square..");
	}

}

ShapeMaker.java

public class ShapeMaker {
	private Shape square, pentagon, hexagon;
	
	public ShapeMaker() {
		// TODO Auto-generated constructor stub
		this.square=new SquareShape();
		this.pentagon=new PentagonShape();
		this.hexagon=new HexagonShape();
	}

	public void drawSquare(){
		square.draw();
	}
	
	public void drawPentagon(){
		pentagon.draw();
	}
	
	public void drawHexagon(){
		hexagon.draw();
	}
}

FacadePatternDemo.java

public class FacadePatternDemo {

	public FacadePatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		ShapeMaker maker = new ShapeMaker();
		
		//get me square shape
		maker.drawSquare();
		
		//get me hexagon
		maker.drawHexagon();
		
		//get me pentagon
		maker.drawPentagon();
	}

}

Output

Categories
Algorithms & Design Design Pattern

Decorator Pattern

Square.java

public class Square {

	public Square() {
		// TODO Auto-generated constructor stub
	}
	
	public void draw(){
		System.out.println("Drawing the square..");
	}

}

ColorDecorator.java

public abstract class ColorDecorator {

	protected Square square;
	public ColorDecorator(Square square) {
		// TODO Auto-generated constructor stub
		this.square=square;
	}

	public abstract void applyColor();
	public void draw(){
		this.square.draw();
		this.applyColor();
	}
}

RedColorDecorator.java

public class RedColorDecorator extends ColorDecorator {

	public RedColorDecorator(Square square) {
		// TODO Auto-generated constructor stub
		super(square);
	}

	@Override
	public void applyColor() {
		// TODO Auto-generated method stub
		System.out.println("applying Red color");
	}
}

GreenColorDecorator.java

public class GreenColorDecorator extends ColorDecorator {

	public GreenColorDecorator(Square square) {
		// TODO Auto-generated constructor stub
		super(square);
	}

	@Override
	public void applyColor() {
		// TODO Auto-generated method stub
		System.out.println("applying green Color");
	}

}

DecoratorPatternDemo.java

public class DecoratorPatternDemo {

	public DecoratorPatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		//Create Square
		Square square = new Square();
		
		//Decorate Square with red color
		ColorDecorator redSquare = new RedColorDecorator(square);
		redSquare.draw();
		
		//Decorate Square with green color
		ColorDecorator greenSquare = new GreenColorDecorator(square);
		greenSquare.draw();
				

	}

}

Output

Categories
Algorithms & Design Design Pattern

Composite Pattern

Employee.java

import java.util.ArrayList;

public class Employee {

	private String firstName, lastName;
	private String department;
	private ArrayList subordinates;
	
	public Employee(String firstName, String lastName, String department) {
		// TODO Auto-generated constructor stub
		this.firstName=firstName;
		this.lastName=lastName;
		this.department=department;
		subordinates = new ArrayList();
	}

	public void addSubordinateEmployee(Employee emp){
		this.subordinates.add(emp);
	}
	
	public ArrayList getSubordinates(){
		ArrayList subordinatesList = new ArrayList();
		subordinatesList.addAll(this.subordinates);
		for(Employee  emp: this.subordinates){
			subordinatesList.addAll(emp.getSubordinates());
		}
		return subordinatesList;
	}
	
	public String toString(){
		return firstName+", "+lastName+", "+department;
	}
}

CompositePatternDemo.java

import java.util.ArrayList;

public class CompositePatternDemo {

	public CompositePatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		Employee ManagingDirector = new Employee("Jane", "Dsouza","Management");
		Employee CEO = new Employee("Hana", "Fujita", "Management");
		
		Employee PM1  = new Employee("Sara", "Mehta", "Management");
		Employee Lead1 = new Employee("Saran","Kumar", "Technical");
		Employee Developer1 = new Employee("Raja", "Moni", "Technical");
		
		Employee PM2  = new Employee("Hara", "Mehta", "Management");
		Employee Lead2 = new Employee("Karan","Kumar", "Technical");
		Employee Developer2 = new Employee("Kajol", "Soni", "Technical");
		
		
		ManagingDirector.addSubordinateEmployee(CEO);
		CEO.addSubordinateEmployee(PM1);
		CEO.addSubordinateEmployee(PM2);
		
		PM1.addSubordinateEmployee(Lead1);
		Lead1.addSubordinateEmployee(Developer1);
		
		PM2.addSubordinateEmployee(Lead2);
		Lead2.addSubordinateEmployee(Developer2);
		
		
		
		//print hierarachy of ManagingDirector
		ArrayList ManagindDirectorHierarchy = ManagingDirector.getSubordinates();
		System.out.println("ManagingDirectorHierarchy :"+ManagindDirectorHierarchy);
		
		//print hierarchy of PM2
		ArrayList PM2Hierarchy = PM2.getSubordinates();
		System.out.println("PM2Hierarchy :"+PM2Hierarchy);
		
	}

}

Output

Categories
Algorithms & Design Design Pattern

Criterion Pattern

Criteria.java

import java.util.List;

public interface Criteria {

	public List meetCriteria(List inputList);
}

Person.java

public class Person {
	private boolean isMarried;
	private String name;
	private boolean isEngineer;
	
	public Person(String name, boolean isMarried,boolean isEngineer) {
		// TODO Auto-generated constructor stub
		this.name=name;
		this.isMarried=isMarried;
		this.isEngineer=isEngineer;
	}

	public boolean isMarried() {
		return isMarried;
	}

	public void setMarried(boolean isMarried) {
		this.isMarried = isMarried;
	}

	public String getName() {
		return name;
	}

	public boolean isEngineer() {
		return isEngineer;
	}

	public void setEngineer(boolean isEngineer) {
		this.isEngineer = isEngineer;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String toString(){
		return name+","+isMarried+","+isEngineer;
	}
}

IsEngineerCriteria.java

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class IsEngineerCriteria implements Criteria {

	public IsEngineerCriteria() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List meetCriteria(List inputList) {
		// TODO Auto-generated method stub
		List filteredList = new ArrayList();
		Iterator itr = inputList.iterator();
		while(itr.hasNext()){
			Person person  = (Person)itr.next();
			if((person).isEngineer()) filteredList.add(person);
		}
		return filteredList; 
	
	}

}

MarriedCriteria.java

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class MarriedCriteria implements Criteria {

	public MarriedCriteria() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List meetCriteria(List inputList) {
		// TODO Auto-generated method stub
		List filteredList = new ArrayList();
		Iterator itr = inputList.iterator();
		while(itr.hasNext()){
			Person person  = (Person)itr.next();
			if((person).isMarried()) filteredList.add(person);
		}
		return filteredList; 
	}

}

AndCriteria.java

import java.util.List;

public class AndCriteria implements Criteria {

	private Criteria criteria1, criteria2;
	public AndCriteria(Criteria criteria1, Criteria criteria2) {
		// TODO Auto-generated constructor stub
		this.criteria1=criteria1;
		this.criteria2=criteria2;
	}

	@Override
	public List meetCriteria(List inputList) {
		List filteredList1 = criteria1.meetCriteria(inputList);
		return criteria2.meetCriteria(filteredList1);
	}

}

OrCriteria.java

import java.util.Iterator;
import java.util.List;

public class OrCriteria implements Criteria {
	private Criteria criteria1,criteria2;
	
	public OrCriteria(Criteria criteria1, Criteria criteria2) {
		// TODO Auto-generated constructor stub
		this.criteria1=criteria1;
		this.criteria2=criteria2;
	}

	@Override
	public List meetCriteria(List inputList) {
		// TODO Auto-generated method stub
		List filtered1 = criteria1.meetCriteria(inputList);
		List filtered2 = criteria2.meetCriteria(inputList);
		
		Iterator itr = filtered1.iterator();
		while(itr.hasNext()){
			Person person  = (Person)itr.next();
			if(!filtered2.contains(person))
				filtered2.add(person);
		}
		return filtered2;
	}

}

CriteriaPatternDemo.java

import java.util.ArrayList;
import java.util.List;

public class CriteriaPatternDemo {

	public CriteriaPatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		
		List  inputList = new ArrayList();
		inputList.add(new Person("Jane", false, false));
		inputList.add(new Person("Ken", true, false));
		inputList.add(new Person("Dan", false, true));
		inputList.add(new Person("Mana", true, true));
		inputList.add(new Person("Hana", false, false));
		inputList.add(new Person("Tana", false, true));
		inputList.add(new Person("Mika", true, false));

		//get All married
		Criteria married = new MarriedCriteria();
		System.out.println("All married : "+ married.meetCriteria(inputList).toString());
		
		//get all engineer
		Criteria eng = new IsEngineerCriteria();
		System.out.println("All engineer : "+ eng.meetCriteria(inputList).toString());
		
		//get married and engineer
		Criteria and = new AndCriteria(married,eng);
		System.out.println("All married and engineer: "+ and.meetCriteria(inputList).toString());
		
		//get married or engineer
		Criteria or = new OrCriteria(married,eng);
		System.out.println("All married and engineer: "+ or.meetCriteria(inputList).toString());
				
		System.out.println("All people :" +inputList);
	}

}

Output

Categories
Algorithms & Design Design Pattern Design Pattern

Bridge Pattern

Shape.java

public abstract class Shape {

	protected ColorAPI colorHandle;
	public Shape(ColorAPI colorHandle){
		this.colorHandle=colorHandle;
	}
	
	public abstract void draw();
}

ColorAPI.java

public interface ColorAPI {

	public void applyColor();
}

Square.java

public class Square extends Shape {

	private int x,y;
	
	public Square(int x, int y, ColorAPI colorHandler) {
		// TODO Auto-generated constructor stub
		super(colorHandler);
		this.x=x;
		this.y=y;
		
	}

	@Override
	public void draw() {
		// TODO Auto-generated method stub
		System.out.println("Drawing shape of square from co-ordinates x:" + this.x + ", y:"+this.y);
		this.colorHandle.applyColor();
	}

}

GreenColor.java

public class GreenColor implements ColorAPI {

	public GreenColor() {
		// TODO Auto-generated constructor stub
		super();
	}

	@Override
	public void applyColor() {
		// TODO Auto-generated method stub
		System.out.println("applying green color");
	}

}

RedColor.java

public class RedColor implements ColorAPI {

	public RedColor() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void applyColor() {
		// TODO Auto-generated method stub
		System.out.println("Applying red color");
	}

}

BridgePatternDemo.java

public class BridgePatternDemo {

	public BridgePatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		
		//Draw the Red square
		ColorAPI colorHandleRed = new RedColor();
		Shape redSquare = new Square(10, 10, colorHandleRed);
		redSquare.draw();
		
		//Draw the Green Square
		ColorAPI colorHandleGreen = new GreenColor();
		Shape greenSquare = new Square(20, 20, colorHandleGreen);
		greenSquare.draw();
		
		
	}

}

Output

Categories
Algorithms & Design Design Pattern Design Pattern

Adapter Pattern

MediaPlayer.java

public interface MediaPlayer {
public void playMP3File(String fileName);
}

AdvancedMediaPlayer.java

public interface AdvancedMediaPlayer {
public void playVLCFile(String fileName);
public void playMP4File(String fileName);
}

WindowsMP3Player.java

public class WindowsMP3Player implements MediaPlayer {

	public WindowsMP3Player() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void playMP3File(String fileName) {
		// TODO Auto-generated method stub
		System.out.println("playing mp3 file::"+fileName);
	}

}

VLCAdvancedMediaPlayer.java

public class VLCAdvancedMediaPlayer implements AdvancedMediaPlayer {

	public VLCAdvancedMediaPlayer() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void playVLCFile(String fileName) {
		// TODO Auto-generated method stub
		System.out.println("playing VLC file : "+fileName);
	}

	@Override
	public void playMP4File(String fileName) {
		// TODO Auto-generated method stub
		//do nothing
	}

}

MP4AdvancedMediaPlayer.java

public class MP4AdvancedMediaPlayer implements AdvancedMediaPlayer {

	public MP4AdvancedMediaPlayer() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public void playVLCFile(String fileName) {
		// TODO Auto-generated method stub
		//do nothing
	}

	@Override
	public void playMP4File(String fileName) {
		// TODO Auto-generated method stub
		System.out.println("playing MP4 file ::"+fileName);
	}

}

AdvancedMediaAdapter.java

public class AdvancedMediaAdapter implements MediaPlayer {
	private AdvancedMediaPlayer advanced;
	
	public AdvancedMediaAdapter(String advancedPlayerName) {
		// TODO Auto-generated constructor stub
		if("VLC".equals(advancedPlayerName)){
			advanced = new VLCAdvancedMediaPlayer();
		} else {
			advanced = new MP4AdvancedMediaPlayer();
		}
	}
	
	public void playFile(String fileName, String fileType){
		if("mp3".equalsIgnoreCase(fileType))
			playMP3File(fileName);
		else if(advanced instanceof VLCAdvancedMediaPlayer)
			advanced.playVLCFile(fileName);
		else if(advanced instanceof MP4AdvancedMediaPlayer)
			advanced.playMP4File(fileName);
	}

	@Override
	public void playMP3File(String fileName) {
		// TODO Auto-generated method stub
		System.out.println("playing Mp3 file by media adapter ::" + fileName);
	}

}

AdapterPatternDemo.java

public class AdapterPatternDemo {

	public AdapterPatternDemo() {
		// TODO Auto-generated constructor stub
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub

		//I want to play Mp3 file by Mp3 player
		MediaPlayer mp3player = new WindowsMP3Player();
		mp3player.playMP3File("Sarara.mp3");
		
		//I want to play mp3, vlc, mp4 file by MediaAdapter
		AdvancedMediaAdapter adapter = new AdvancedMediaAdapter("VLC");
		adapter.playFile("Ararra.mp3", "mp3");
		adapter.playFile("Ararra.vlc", "VLC");
		adapter.playFile("Ararra.mp4", "VLC");
	}

}

Output

Design a site like this with WordPress.com
Get started