Factory pattern is one of the most used design patterns in java with the help of this pattern you can create object easily by using this concept of oops.
Example of Factory pattern :
1. Create an interface
2. Create class that 'll override interface method draw
3. Create factory class that 'll generate concrete class object
public interface shape{
public void draw();
}
public class Rectangle implements shape{
@Override
public void draw() {
Log.e(" inside :" , " Rectangel shape");
}
}
public class Circle implements shape{
@Override
public void draw() {
Log.e(" inside :" , " ccircle shape");
}
}
public class MYFactory{
public Shape getShape(String type){
if (type == null){
return null;
}else if (type.equalsIgnoreCase("rectangle")){
return new Rectangle();
}else if (type.equalsIgnoreCase("circle")){
return new Circle();
}
}
}
4. call from main activity like this :
MYFactory myFactory = new MYFactory();
myFactor.getShape("rectangle");
myFactory.draw();
myFactor.getShape("circle");
myFactory.draw();
0 Comment(s)