import
java.util.List;
import
java.util.ArrayList;
interface
ContactList
{
public
List<Employee> getEmployeeList();
}
class
Company {
String companyName;
String companyAddress;
String companyContactNo;
ContactList contactList;
public
Company(String companyName, String companyAddress,
String companyContactNo, ContactList contactList)
{
this
.companyName = companyName;
this
.companyAddress = companyAddress;
this
.companyContactNo = companyContactNo;
this
.contactList = contactList;
}
public
String getCompanyName()
{
return
companyName;
}
public
String getCompanyAddress()
{
return
companyAddress;
}
public
String getCompanyContactNo()
{
return
companyContactNo;
}
public
ContactList getContactList()
{
return
contactList;
}
}
class
ContactListImpl
implements
ContactList {
public
List<Employee> getEmployeeList()
{
return
getEmpList();
}
private
static
List<Employee> getEmpList()
{
List<Employee> empList =
new
ArrayList<Employee>(
5
);
empList.add(
new
Employee(
"Lokesh"
,
2565.55
,
"SE"
));
empList.add(
new
Employee(
"Kushagra"
,
22574
,
"Manager"
));
empList.add(
new
Employee(
"Susmit"
,
3256.77
,
"G4"
));
empList.add(
new
Employee(
"Vikram"
,
4875.54
,
"SSE"
));
empList.add(
new
Employee(
"Achint"
,
2847.01
,
"SE"
));
return
empList;
}
}
class
ContactListProxyImpl
implements
ContactList {
private
ContactList contactList;
public
List<Employee> getEmployeeList()
{
if
(contactList ==
null
) {
System.out.println(
"Fetching list of employees"
);
contactList =
new
ContactListImpl();
}
return
contactList.getEmployeeList();
}
}
class
Employee {
private
String employeeName;
private
double
employeeSalary;
private
String employeeDesignation;
public
Employee(String employeeName,
double
employeeSalary, String employeeDesignation)
{
this
.employeeName = employeeName;
this
.employeeSalary = employeeSalary;
this
.employeeDesignation = employeeDesignation;
}
public
String getEmployeeName()
{
return
employeeName;
}
public
double
getEmployeeSalary()
{
return
employeeSalary;
}
public
String getEmployeeDesignation()
{
return
employeeDesignation;
}
public
String toString()
{
return
"Employee Name: "
+ employeeName + ",
EmployeeDesignation :
" + employeeDesignation + "
,
Employee Salary : " + employeeSalary;
}
}
class
LazyLoading {
public
static
void
main(String[] args)
{
ContactList contactList =
new
ContactListProxyImpl();
Company company =
new
Company
(
"Geeksforgeeks"
,
"India"
,
"+91-011-28458965"
, contactList);
System.out.println(
"Company Name: "
+ company.getCompanyName());
System.out.println(
"Company Address: "
+ company.getCompanyAddress());
System.out.println(
"Company Contact No.: "
+ company.getCompanyContactNo());
System.out.println(
"Requesting for contact list"
);
contactList = company.getContactList();
List<Employee> empList = contactList.getEmployeeList();
for
(Employee emp : empList) {
System.out.println(emp);
}
}
}