1

Lets assume, i have a student.xhtml form that has radiolists&dropdownmenu populated from its @ManagedBean Student(). As u know, in order to populate form from managedbean i need to have List<Object> fields in Student class. But i also want my Student class to be pure meaning it should have fields only related to itself, not the possible values it can get (i mean List<>). So i want to seperate my Student class from @ManagedBean. So i will have two classes at the end one of is pure Student class and StudentBean class which controls the view.

So my question is, is it good practice have two classes like below or i should go with one class? Two classes method duplicates fields so i don't know whether it affects performance to a bad extent.. What do you suggest?

Not wanted BeanClassWithStudent Pattern;

 import java.util.ArrayList;
 import java.util.List;

 import javax.annotation.PostConstruct;
 import javax.faces.bean.*;

  @ManagedBean
  public class Student {

private String firstName;
private String lastName;
private String country;
private String favLanguage;

private List<String> countryList;
private List<String> favLanguageList;

@PostConstruct // generate DropDownList Combobox and radiobuttons From class fields
public void init() {
    generateCountries();
    generateFavLanguages();
}

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public String getFavLanguage() {
    return favLanguage;
}

public void setFavLanguage(String favLanguage) {
    this.favLanguage = favLanguage;
}
public List<String> getCountryList() {
    return countryList;
}

public List<String> getFavLanguageList() {
    return favLanguageList;
}

private void generateCountries(){
    countryList = new ArrayList<>();
    countryList.add("Turkey");
    countryList.add("France");
    countryList.add("Senegal");
    countryList.add("USA");
}

private void generateFavLanguages(){
    favLanguageList = new ArrayList<>();
    favLanguageList.add("Java");
    favLanguageList.add("Ruby");
    favLanguageList.add("C++");
    favLanguageList.add("Visual Basic");
}

}

My wanted seperate classes; Student.class

  public class Student {

private String firstName;
private String lastName;
private String country;

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}

Wanted StudentControllerBean;

 import java.util.ArrayList;
 import java.util.List;

 import javax.annotation.PostConstruct;
 import javax.faces.bean.*;

  @ManagedBean
  public class StudentBean {

private String firstName;
private String lastName;
private String country;

private List<String> countryList;
private List<String> favLanguageList;

@PostConstruct // generate DropDownList Combobox and radiobuttons From    class fields
public void init() {
    generateCountries();
    generateFavLanguages();
}

public String getFirstName() {
    return firstName;
}
public void setFirstName(String firstName) {
    this.firstName = firstName;
}
public String getLastName() {
    return lastName;
}
public void setLastName(String lastName) {
    this.lastName = lastName;
}
public String getCountry() {
    return country;
}
public void setCountry(String country) {
    this.country = country;
}
public List<String> getCountryList() {
    return countryList;
}

public List<String> getFavLanguageList() {
    return favLanguageList;
}

private void generateCountries(){
    countryList = new ArrayList<>();
    countryList.add("Turkey");
    countryList.add("France");
    countryList.add("Senegal");
    countryList.add("USA");
}

private void generateFavLanguages(){
    favLanguageList = new ArrayList<>();
    favLanguageList.add("Java");
    favLanguageList.add("Ruby");
    favLanguageList.add("C++");
    favLanguageList.add("Visual Basic");
}

}

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
Mert Serimer
  • 1,217
  • 2
  • 16
  • 38
  • @javaguy I dont have any, this is a design pattern question. – Mert Serimer Oct 31 '16 at 19:41
  • 1
    read this to: http://stackoverflow.com/questions/7223055/making-distinctions-between-different-kinds-of-jsf-managed-beans/7223910#7223910 and http://stackoverflow.com/questions/13011392/jsf-service-layer and and http://stackoverflow.com/questions/30639785/jsf-controller-service-and-dao of which this is a duplicate – Kukeltje Nov 01 '16 at 07:21
  • @Kukeltje It is not duplicate, it is entirely different question. Others are talking about 2 seperate bean class while i am talking one controller class and one pure object class. – Mert Serimer Nov 01 '16 at 08:32
  • it is all in the duplicate... imo (and not only mine, since it actually now is marked as a duplicate by BalusC) – Kukeltje Nov 01 '16 at 09:04

1 Answers1

3

It is always better to maintain two separate Beans one for the presentation layer (@ManagedBean) and the other one (called as Business/Entiry Bean) for the Business tier (services layer) i.e., it is not a good idea to mix up both the presentation tier (Managed) beans with the Business beans rather you need to separate them like how you did.

The request flow between the J2EE tiers goes as follows:

HTML/JSP -> ManagedBean -> Service -> DAO -> Database

You need to convert the presentation bean data to the Business bean in the Action classes and then pass that to Business Bean Object to the Services layer. Service layer uses this Business Bean to interact with DAO classes which persist or do some transactions with the database.

This concept is applicable not only for JSF, but all other J2EE web frameworks (like Struts, Spring MVC, etc..).

You can find more details here on this concept.

Community
  • 1
  • 1
Vasu
  • 21,832
  • 11
  • 51
  • 67
  • thanks for the answer. Your answer seemed to cover up everything up to top complex java ee applications. If we think for small enterprises, can i call services layer from ManagedBean directly? Such as moderate-small websites? – Mert Serimer Oct 31 '16 at 20:05
  • I have updated my answer with request flow, it is applicable for all enterprises (small/medium/large) – Vasu Oct 31 '16 at 20:18
  • thank you for the answer – Mert Serimer Oct 31 '16 at 20:46