Factory method, what is factory Method

Source: Internet
Author: User

Factory method, what is factory Method

1. Overview

Defines an interface used to create objects, so that the subclass determines which class to instantiate. FactoryMethod delays the instantiation of a class to its subclass

2. Applicability

1) when a class does not know the class of the object it must create

2) When a class wants its subclass to specify the object it creates

3) when the class delegates the responsibility of the created object to one of the multiple help subclasses, and you want to localize the information of which help subclass is the proxy

3. Participants

1) Product defines the interface of the object created by the factory Method

2) ConcreteProduct is used to implement the Product Interface

3) Creator declares the factory method, which returns an object of the Product type. Creator can also define the default implementation of a factory method, which returns a default ConcreteProduct object. You can call the factory method to create a Product object.

4) ConcreteCreator redefines the factory method to return a ConcreteProduct instance

Example:

Product Package:

1 package product;2 3 public interface Work {4     void doWork();5 }

Concreteproduct package:

1 package concreteproduct; 2 3 import product. work; 4 5 public class StudentWork implements Work {6 7 @ Override 8 public void doWork () {9 System. out. println ("students are doing homework"); 10} 11 12}
1 package concreteproduct; 2 3 import product. work; 4 5 public class TeacherWork implements Work {6 7 @ Override 8 public void doWork () {9 System. out. println ("instructor review assignment"); 10} 11 12}

Creator package:

1 package creator;2 3 import product.Work;4 5 public interface IWorkFactory {6     Work getWork();7 }

Concretecreator package:

 1 package concretecreator; 2  3 import concreteproduct.StudentWork; 4 import product.Work; 5 import creator.IWorkFactory; 6  7 public class StudentWorkFactory implements IWorkFactory { 8  9     @Override10     public Work getWork() {11         // TODO Auto-generated method stub12         return new StudentWork();13     }14 15 }
 1 package concretecreator; 2  3 import concreteproduct.TeacherWork; 4 import product.Work; 5 import creator.IWorkFactory; 6  7 public class TeacherWorkFactory implements IWorkFactory { 8  9     @Override10     public Work getWork() {11         // TODO Auto-generated method stub12         return new TeacherWork();13     }14 15 }

Test package:

 1 package test; 2  3 import concretecreator.StudentWorkFactory; 4 import concretecreator.TeacherWorkFactory; 5 import creator.IWorkFactory; 6  7 public class Test { 8     public static void main(String args[]){ 9         IWorkFactory studentWorkFactory = new StudentWorkFactory();10         studentWorkFactory.getWork().doWork();11         12         IWorkFactory teacherWorkFactory = new TeacherWorkFactory();13         teacherWorkFactory.getWork().doWork();14     }15 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.