Creational Patterns : Factory pattern

Creational Patterns : factory pattern in Java:-


Definition:-
Factory pattern is comes under the creational pattern. In the Factory pattern, There is a class which manage the creation of object according to the situation is called factory.
    There is an example which describe the Factory pattern.



public class course {
   private int courseCode;
   private String CourseName;

   public int getCourseCode() {
       return courseCode;
   }



   public void setCourseCode(int courseCode) {
       this.courseCode = courseCode;
   }

   public String getCourseName() {
       return CourseName;
   }

   public void setCourseName(String courseName) {
       CourseName = courseName;
   }
}



public class Java extends course {
   // some code here
}


public class Php extends course {
   // some code here
}


public class CourseFactory {
   public course getCourse(String courseName) {
       if (courseName.equals("Java")) {
           return new Java();
       } else if (courseName.equals("Php")) {
           return new Php();
       } else {
           return null;
       }
   }
   
   public static void main(String[] args) {
       CourseFactory courseFactory = new CourseFactory();
       courseFactory.getCourse("Java");
   }
}

Comments

Recent Post

Recent Posts Widget

Popular posts from this blog

Capture image from webcam java code, examples

Shallow Copy Deep Copy Java example

Database Connection using NODE JS example