Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • What is Factory Pattern?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 672
    Comment on it

    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)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: