English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

O método fábrica estático cria objetos usando a palavra-chave new no Java?

工厂模式是一种设计模式(创意模式),用于根据我们提供的数据创建多个对象。在其中,我们创建了一个抽象创建过程的对象。

Exemplo

下面给出了工厂模式的示例实现。在这里,我们有一个名为Employee和3类的接口:Student,讲师,NonTeachingStaff,实现了它。我们使用名为的方法创建了一个工厂类(EmployeeFactory)getEmployee()。Este método aceita um valor String e retorna o objeto de uma das classes com base no valor String fornecido。

import java.util.Scanner;
interface Person {
   void dsplay();
}
class Student implements Person {
   public void dsplay() {
      System.out.println("This is display method of the Student class");
   }
}
class Lecturer implements Person {
   public void dsplay() {
      System.out.println("This is display method of the Lecturer class");
   }
}
class NonTeachingStaff implements Person {
   public void dsplay() {
      System.out.println("This is display method of the NonTeachingStaff class");
   }
}
class PersonFactory {
   public Person getPerson(String empType) {
      if(empType == null) {
         return null;
      }
      if(empType.equalsIgnoreCase("student")){
         return new Student();
      } else if(empType.equalsIgnoreCase("lecturer")){
         return new Lecturer();
      } else if(empType.equalsIgnoreCase("non teaching staff")){
         return new NonTeachingStaff();
      }
      return null;
   }
}
public class FactoryPattern {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Insira o tipo do objeto que deseja: (student, lecturer, non teaching staff)");
      String type = sc.next();
      PersonFactory obj = new PersonFactory();
      Person emp = obj.getPerson(type);
      emp.dsplay();
   }
}

Resultados de Saída

Insira o tipo do objeto que deseja: (student, lecturer, non teaching staff)
lecturer
Este é o método de exibição da classe Lecturer

Método de fábrica estático

Embora se diga que há cinco métodos para criar objetos em Java-

  • Usando a palavra-chave new.

  • Usando o método de fábrica.

  • Usando clone().

  • Usando Class.forName().

  • Usando a deserialização de objetos.

O único método para criar objetos em Java é usando a palavra-chave new, todas as outras métodos são abstrações对此对象的。Todas essas métodos usam completamente a palavra-chave new internamente.

Exemplo

import java.util.Scanner;
interface Employee {
   void dsplay();
}
class Student implements Employee {
   public void dsplay() {
      System.out.println("This is display method of the Student class");
   }
}
class Lecturer implements Employee {
   public void dsplay() {
      System.out.println("This is display method of the Lecturer class");
   }
}
class NonTeachingStaff implements Employee {
   public void dsplay() {
      System.out.println("This is display method of the NonTeachingStaff class");
   }
}
class EmployeeFactory {
   public static Employee getEmployee(String empType) {
      if(empType == null) {
         return null;
      }
      if(empType.equalsIgnoreCase("student")){
         return new Student();
      } else if(empType.equalsIgnoreCase("lecturer")){
         return new Lecturer();
      } else if(empType.equalsIgnoreCase("non teaching staff")){
         return new NonTeachingStaff();
      }
      return null;
   }
}
public class FactoryPattern {
   public static void main(String args[]) {
      Scanner sc = new Scanner(System.in);
      System.out.println("Insira o tipo do objeto que deseja: (student, lecturer, non teaching staff)");
      String type = sc.next();
      EmployeeFactory obj = new EmployeeFactory();
      Employee emp = EmployeeFactory.getEmployee(type);
      emp.dsplay();
   }
}

Resultados de Saída

Insira o tipo do objeto que deseja: (student, lecturer, non teaching staff)
lecturer
Este é o método de exibição da classe Lecturer
Você também pode gostar