我正在尝试在Java中创建一个非常简单的Factory Method设计模式示例。我真的不懂Java,我不熟悉编程,但我需要提出一个在java中实现的基本FactoryMethod示例。以下是我提出的建议。我确定有很多错误,我显然缺少一些构造函数,而且我对抽象类和接口感到困惑。你能指出我的错误并纠正我的代码以及解释吗?提前感谢您的时间和帮助。
public abstract class Person
{
public void createPerson(){ }
}
public class Male extends Person
{
@Override
public void createPerson()
{
System.out.print("a man has been created");
}
}
public class Female extends Person
{
@Override
public void createPerson()
{
System.out.print("a woman has been created");
}
}
public class PersonFactory
{
public static Person makePerson(String x) // I have no Person constructor in
{ // the actual abstract person class so
if(x=="male") // is this valid here?
{
Male man=new Male();
return man;
}
else
{
Female woman=new Female();
return woman;
}
}
}
public class Test
{
public static void main(String[] args)
{
Person y= new Person(makePerson("male")); // definitely doing smth wrong here
Person z= new Person(makePerson("female")); // yup, here as well
}
}
答案 0 :(得分:8)
简而言之,您的版本中有几个问题已在下面更正:
createPerson
方法没用。==
代替.equals
。我已经增强了你的Person类,以添加一个由Male和Female类共享的成员字段,以演示如何共享一个共同的抽象构造函数。
public abstract class Person {
protected final String name;
public Person(String name) {
this.name = name;
}
}
public class Male extends Person {
public Male(String name) {
super(name);
}
}
public class Female extends Person {
public Female(String name) {
super(name);
}
}
public class PersonFactory
{
public static Person makePerson(String gender, String name)
{
if(gender.equals("male"))
{
Male man=new Male(name);
return man;
}
else
{
Female woman=new Female(name);
return woman;
}
}
}
public class Test
{
public static void main(String[] args)
{
Person y= PersonFactory.makePerson("male", "bob"));
Person z= new PersonFactory.makePerson("female", "janet"));
}
}
答案 1 :(得分:2)
应该是:
public class Test
{
public static void main(String[] args)
{
Person y= PersonFactory.makePerson("male");
Person z= PersonFactory.makePerson("female");
}
}
原因是
PersonFactory.makePerson("male")
是因为你有一个静态方法makePerson(这意味着你没有'new'类来调用方法,因为它不是一个实例方法),即一个类中的静态变量可用于所有的实例类。静态方法称为TheClassItIsDefinedIn.TheMethodSignature
此外:
public abstract class Person
{
public void createPerson(){ }
}
应该是一个接口,因为你没有在派生类型之间共享的功能代码(这里没有显示),这里是抽象和接口的外行解释:
interface:一个合同,声明'我有方法blah和blah2所以任何实现我的类必须有一个该名称和签名的方法(并且只要它匹配返回类型等,它可以在那里拥有它喜欢的任何代码)'
abstract:一个抽象类可以像接口一样拥有方法签名但是它通常也会在所有派生类型中具有通用的具体实现
答案 2 :(得分:2)
我建议你在开始应用设计模式之前掌握java编程。 设计模式通常需要面向对象的原则,因此如果你缺乏良好的编程实践,那就没有多大意义了。
尽管如此,Factory方法的定义在意义的上下文中看起来是正确的,但在可用性方面却看起来不正确。 Factory方法需要创建一个对象,因此返回对创建的对象的引用。此外,创建的对象通常与实现工厂方法模式的类不同,因为可以设想创建的实例将由对象使用,在本例中为Person。
所以这里有一个抽象的例子,说明你可能如何使用工厂模式。
public abstract class Student {
}
public class PrimarySchoolStudent extends Student {
}
public class HighSchoolStudent extends Student {
}
public abstract class ClassRoom {
private List<Student> students;
void enrollStudent(String studentId) {
Student student = newStudent();
students.add(student);
}
abstract Student newStudent();
}
public class HighSchoolClassRoom extends ClassRoom {
@Override
Student newStudent() {
return new HighSchoolStudent();
}
}
public class PrimarySchoolClassRoom extends ClassRoom {
@Override
Student newStudent() {
return new PrimarySchoolStudent();
}
}
答案 3 :(得分:0)
基于@maress代码的完整示例:
<form name="tab-Registration.form">
<div class="list" align="left">
<label class="all_page_bgColor" align="left">
<input type="text" name="FName" placeholder="First Name" ng-model="Model.FName" class="all_page_bgColor" align="left">
</label>
<label class="all_page_bgColor" align="left">
<input type="text" name="LName" placeholder="Last Name" ng-model="Model.LName" class="all_page_bgColor" align="left">
</label>
<label class="all_page_bgColor" align="left"> <!-- class="item item-input" -->
<input type="text" name="Email" placeholder="Email" ng-model="Model.Email" class="all_page_bgColor" align="left">
</label>
<label class="all_page_bgColor" align="left">
<input type="text" name="PhoneNo" placeholder="Phone" ng-model="Model.PhoneNo" class="all_page_bgColor" align="left">
</label>
</div>
<div align = "center">
<button class="button button-assertive" ng-click="submit()">Submit</button>
</div>
</form>
答案 4 :(得分:0)
重构了上面的代码。
import java.util.HashMap;
class Person {
String name;
public Person(String name){
this.name = name;
}
public void setName(String name){
this.name = name;
}
public String getName(){
return name;
}
}
class Male extends Person {
public Male(String name) {
super(name);
System.out.println("a man has been created with name:"+name);
}
}
class Female extends Person{
public Female(String name) {
super(name);
System.out.print("a woman has been created with name:"+name);
}
}
class PersonFactory {
private static HashMap<String,Person> factory = new HashMap<String,Person>();
static {
factory.put("male", new Male ("Trump"));
factory.put("female", new Female ("Theresa May"));
}
public static Person makePerson(String type) {
return factory.get(type);
}
}
public class Test {
public static void main(String[] args){
Person y= PersonFactory.makePerson("male");
Person z= PersonFactory.makePerson("female");
}
}
主要注意事项:
在静态块中创建对象(Male
和Female
)一次。
并非每次都创建新对象。创作只发生一次。每个makePerson
调用都会从工厂返回现有对象。
删除了createPerson
方法
看看相关帖子@