自己练习的工厂模式

不知对不对,感觉还可以。

 

首先是一个 Person.java 接口,将来要有 Student.java 和 Teacher.java实现它。 另外有一个总工厂 PersonFactory.java, 分别有  StudentFactory.java 和 TeacherFactory.java 实现它。最后是测试类 FactoryTest.java。

具体内容如下:

==========Person.java==============

 

package com.Java程序设计入门.factoryPatternSelfPractice;
public interface Person {
    String getName();
    String getIDNumber();
}
==========Student.java==============

 

package com.Java程序设计入门.factoryPatternSelfPractice;

public class Student implements Person{
    private String strName = "";
    private String strIDNumber = "";
    private String strSchoolName = "";
    @Override
    public String getName() {
        return strName;
    }
    @Override
    public String getIDNumber() {
        return strIDNumber;
    }
    public void setName(String strName) {
        this.strName = strName;
    }
    public void setStrIDNumber(String strIDNumber) {
        this.strIDNumber = strIDNumber;
    }
    public String getStrSchoolName() {
        return strSchoolName;
    }
    public void setStrSchoolName(String strSchoolName) {
        this.strSchoolName = strSchoolName;
    }
    public Student(String name, String IDNumber)
    {
        this.strName = name;
        this.strIDNumber = IDNumber;
    }
    public String toString()
    {
        String information = "Name: " + strName + "; ID: " + strIDNumber;
        if(!strSchoolName.equalsIgnoreCase(""))
        {
            information += "; School: " + strSchoolName;
        }
        return information;
    }
}

 

 

==========Teacher.java==============
 
package com.Java程序设计入门.factoryPatternSelfPractice;
public class Teacher implements Person{
    private String strName = "";
    private String strIDNumber = "";
    private String strSchool = "";
    private int workingYears = 0;
    @Override
    public String getName() {
        return strName;
    }
    @Override
    public String getIDNumber() {
        return strIDNumber;
    }
    public Teacher(String name, String IDNumber, int workingYears)
    {
        this.strName = name;
        this.strIDNumber = IDNumber;
        this.workingYears = workingYears;
    }
    public void setStrName(String strName) {
        this.strName = strName;
    }
    public void setStrIDNumber(String strIDNumber) {
        this.strIDNumber = strIDNumber;
    }
    public String getStrSchool() {
        return strSchool;
    }
    public void setStrSchool(String strSchool) {
        this.strSchool = strSchool;
    }
    public int getWorkingYears() {
        return workingYears;
    }
    public void setStrWorkingYears(int workingYears) {
        this.workingYears = workingYears;
    }
    public String toString()
    {
        String information = "Name: " + strName + "; ID: " + strIDNumber + "; 工龄(年): " + workingYears;
        if(!strSchool.equalsIgnoreCase(""))
        {
            information += "; School: " + strSchool;
        }
        return information;
    }
}

 

 

==========PersonFactory.java==============

 

package com.Java程序设计入门.factoryPatternSelfPractice;
public interface PersonFactory {
    Person getPerson();
}

 

 

==========StudentFactory.java==============

 

package com.Java程序设计入门.factoryPatternSelfPractice;
public class StudentFactory implements PersonFactory{
    private String strName = "";
    private String strID = "";
    @Override
    public Person getPerson() {
        return new Student(strName, strID);
    }
    public StudentFactory(String name, String ID)
    {
        this.strName = name;
        this.strID = ID;
    }
}

 

 

 

==========TeacherFactory.java==============

 

package com.Java程序设计入门.factoryPatternSelfPractice;
public class TeacherFactory implements PersonFactory{
    private String strName = "";
    private String strID = "";
    private int workingYears = 0;
    @Override
    public Person getPerson() {
        return new Teacher(strID, strID, workingYears);
    }
    public TeacherFactory(String name, String id, int year)
    {
        this.strName = name;
        this.strID = id;
        this.workingYears = year;
    }
}

 

 

 

==========FactoryTest.java==============
 
package com.Java程序设计入门.factoryPatternSelfPractice;
public class FactoryTest {
    public static void main(String[] args) {
        PersonFactory pf = null;
        //Student
        pf = new StudentFactory("Alex", "101");
        Person student = pf.getPerson();
        System.out.println(student.toString());
        
        //Teacher
        pf = new TeacherFactory("Allen", "102", 3);
        Person teacher = pf.getPerson();
        System.out.println(teacher.toString());
    }
}

 

posted @ 2019-05-02 07:44  allenbackpacker  阅读(136)  评论(0编辑  收藏  举报