/**
* A simple implementation of Factory Method Pattern
* using a real time CricketPlayer example
*/
//Base
public interface CricketPlayer{
public String play();
}
//Subclass
class Bowler implements CricketPlayer{
public String play (){
return "takes a wicket";
}
}
//Subclass
class Batsman implements CricketPlayer{
public String play (){
return "hits a six";
}
}
//Factory class - contains the logic of instatiating players
public class CricketPlayerFactory{
CricketPlayer player;
public CricketPlayer getPlayerWho(String does){
if(does.equals("bats"))
player = new Batsman();
else if(does.equals("bowls"))
player = new Bowler();
return player;
}
}
//Factory method implementation
public class FactoryImplementation{
public static void main(String args){
//create a factory
CricketPlayerFactory factory = new CricketPlayerFactory();
//ask factory to instantiate the player
CricketPlayer player = factory.getPlayerWho("bats");
//use the player
System.out.prinln(player. play ());
}
}
* A simple implementation of Factory Method Pattern
* using a real time CricketPlayer example
*/
//Base
public interface CricketPlayer{
public String play();
}
//Subclass
class Bowler implements CricketPlayer{
public String play (){
return "takes a wicket";
}
}
//Subclass
class Batsman implements CricketPlayer{
public String play (){
return "hits a six";
}
}
//Factory class - contains the logic of instatiating players
public class CricketPlayerFactory{
CricketPlayer player;
public CricketPlayer getPlayerWho(String does){
if(does.equals("bats"))
player = new Batsman();
else if(does.equals("bowls"))
player = new Bowler();
return player;
}
}
//Factory method implementation
public class FactoryImplementation{
public static void main(String args){
//create a factory
CricketPlayerFactory factory = new CricketPlayerFactory();
//ask factory to instantiate the player
CricketPlayer player = factory.getPlayerWho("bats");
//use the player
System.out.prinln(player. play ());
}
}