Home  >  Article  >  Java  >  Java factory method pattern learning

Java factory method pattern learning

黄舟
黄舟Original
2017-10-13 10:25:051530browse

This article mainly introduces the factory method pattern of java design pattern learning, which has certain reference value. Interested friends can refer to

Factory Method Pattern (Factory Method): Define a user An interface for creating objects, letting subclasses decide which class to instantiate. Factory methods enable deferring instantiation of a class to its subclasses.

Factory method pattern structure diagram:

Project requirements, create a Lei Feng factory, where college students can help others, and volunteers can also help others do things.

1: Create a Lei Feng class with the functions of helping others sweep the floor, do laundry, and buy rice.


package FactoryMethodModel;

public class LeiFeng {

 public void Sweep(){
  System.out.println("扫地");
 }
 
 public void Wash(){
  System.out.println("洗衣");
 }
 
 public void BuyRice(){
  System.out.println("买米");
 }
}

2: Create a class for college students who learn from Lei Feng, inherit the Lei Feng class, and add your own functions.


package FactoryMethodModel;

/**
 * 学雷锋的大学生
 * @author 我不是张英俊
 *
 */
public class UniversityStudent extends LeiFeng {
//里面可以增加专属大学生的功能
}

3: Create a community volunteer class that inherits the Lei Feng class.


package FactoryMethodModel;

/**
 * 学雷锋的社区人员
 * @author 我不是张英俊
 *
 */
public class Volunteer extends LeiFeng {

}

4: Create a Lei Feng factory interface.


package FactoryMethodModel;

/**雷锋工厂的总接口。
 * @author 我不是张英俊
 *
 */
interface LeiFengFactory {
 LeiFeng CreatLenFeng();
}

5: Create a factory for college students who learn from Lei Feng.


package FactoryMethodModel;

/**
 * 学雷锋的大学生工厂
 * @author 我不是张英俊
 *
 */
public class UniversityStudentFactory implements LeiFengFactory {

 @Override
 public LeiFeng CreatLenFeng() {
  return new UniversityStudent();
 }

 
}

6: Create a volunteer factory that learns from Lei Feng.


package FactoryMethodModel;

/**
 * 学雷锋的社区制志愿者
 * @author 我不是张英俊
 *
 */
public class VolunteerFactory implements LeiFengFactory {

 @Override
 public LeiFeng CreatLenFeng() {
 // TODO Auto-generated method stub
 return new Volunteer();
 }

 
}

7: Test class


package FactoryMethodModel;

/**
 * 建立一个雷锋工厂,大学生可以以雷锋的名义起帮助别人,社区志愿者也可以。
 * 工厂化模式:定义一个用于创建对象的接口,让子类决定实例化哪一个类。
 * 工厂方法使一个类的实例化延迟到其子类。
 * @author 我不是张英俊
 *
 */
public class Test {

 public static void main(String[] args) {
 
 LeiFengFactory factory=new UniversityStudentFactory();
 LeiFeng student =factory.CreatLenFeng();
 
 student.BuyRice();
 student.Sweep();
 student.Wash();
 }

}

8: Console

买米
sweep the floor
washing

Summary: The simple factory method violates the open-closed principle, and the factory method overcomes this problem. When new ones need to be added, such as when primary school students learn from Lei Feng, the factory method Just add a new primary school student factory, and then call it in the test class.

The factory method pattern also maintains the advantage of encapsulating the object creation process. This enables the object to be replaced without making any changes, which reduces the coupling between the client program and the product object. The factory method pattern is a further abstraction and promotion of the simple factory pattern. Due to the use of polymorphism, the factory pattern saves the advantages of the simple factory pattern and overcomes the shortcomings. But the disadvantage is that without adding a product, a factory class must be added, which increases the amount of additional development.

The above is the detailed content of Java factory method pattern learning. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn