(JAVA) I have to implement this design using Factory method, singleton and state
ID: 3602207 • Letter: #
Question
(JAVA)
I have to implement this design using Factory method, singleton and state and I was wondering if anyone can help start with this
status Student tuition: float name: String yrResidence: int 20: int StudentStatus tuition: float getTuition): float tuition Student(name: Str, yrResidence: int, i20: in status: StudentStatus) createlnStateStudent name: Stri createOutStateStudent(name: Strin createlntIStudent(name: Strin setStatus(status:Status): void OutState -OutState(...) rResidence: int InState Intl -InState(...) IntState(...) rResidence: int getInstance getlnstance etlnstance etTuition(): float getTuition): float getTuition): floa 20: int etTuition: float return new Student (name, yrResidence) return status.getluition () Student sl - Student.createInStateStudent("John Smith", 18 s1.getTuition ();Explanation / Answer
Before you proceede to solve this problem, be sure with the terms mentioned above.
Factory method: In Object Oriented Programming, the factory method pattern is a way of dealing the problems with creation of objects. Typically, in this method one cannot directly create the object of any class by using new operator. Innstead, one need to use the public and static method defined in that class which creates an object of that class and returns to us. Such methods which returns objects of class are called as Factory Methods. Constructors in such class are defined private.
Singleton class: Single ton class is a class for which only one object is created and all the user of that class can get access to that object using the factory method defined in that class.
Student.java
package pkg;
public class Student {
//Student is a class having factory methods
//declaring all the private variables
private float tution;
private String name;
private int yrResidence;
private int i20;
private StudentStatus status;
//private constructor
private Student(String name,int yrResidence,int i20,float tution, StudentStatus status){
this.tution = tution;
this.name = name;
this.yrResidence = yrResidence;
this.i20 = i20;
this.status = status;
}
//returns Student object with status type InState
public static Student CreateInstateStudent(String name, int yrResidence,int i20,float tution){
return (new Student(name,yrResidence,i20,tution,InState.getInstance()));
}
//returns Student object with status type OutState
public static Student CreateOutstateStudent(String name, int yrResidence,int i20,float tution){
return (new Student(name,yrResidence,i20,tution,OutState.getInstance()));
}
//returns Student object with status type Intl
public static Student CreateIntlStudent(String name, int yrResidence,int i20,float tution){
return (new Student(name,yrResidence,i20,tution,Intl.getInstance()));
}
//returns tution
public float getTution() {
return tution;
}
//sets status of type Instate
public void setStatus(InState status) {
this.status = status;
tution = status.getTution();
}
//sets status of type Instate
public void setStatus(OutState status) {
this.status = status;
tution = status.getTution();
}
//sets status of type Instate
public void setStatus(Intl status) {
this.status = status;
tution = status.getTution();
}
}
StudentStatus.java
package pkg;
//abstract class of student status
public abstract class StudentStatus {
}
InState.java
package pkg;
//Student status of type InState
//NOTE InState is a singleton class
public class InState extends StudentStatus{
//private object created
private static InState obj = new InState();
//variable declaration
private float tution;
//private constructor
private InState(){}
//method that returns the object of this singleton class
//NOTE same object is returned always
public static InState getInstance(){return obj;}
//getters and setters for tution
public float getTution() {
return tution;
}
public void setTution(float tution) {
this.tution = tution;
}
}
OutState.java
package pkg;
//Student status of type OutState
//NOTE OutState is a singleton class
public class OutState extends StudentStatus {
//private object created
private static OutState obj = new OutState();
private float tution;
public OutState(){}
public static OutState getInstance(){return obj;}
public float getTution() {
return tution;
}
public void setTution(float tution) {
this.tution = tution;
}
}
Intl.java
package pkg;
//Student status of type Intl
//NOTE Intl is a singleton class
public class Intl extends StudentStatus{
private static Intl obj = new Intl();
private float tution;
public Intl(){}
public static Intl getInstance(){return obj;}
public float getTution() {
return tution;
}
public void setTution(float tution) {
this.tution = tution;
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.