/**
* 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 ());
}
}
2 comments:
thanks for this information .
you can find factory design pattern in simple and easy to understand language in a detail on
http://www.itsoftpoint.com/?page_id=2666
This is a great post. I like this topic.This site has lots of advantage.I found many interesting things from this site. It helps me in many ways.Thanks for pinternship for web development | internship in electrical engineering | mini project topics for it 3rd year | online internship with certificate | final year project for cse osting this again.
Post a Comment