There is a company which is does the BCG of persons. They have
vast database of blacklisted persons and prisoners etc.
public class OnlineBGCServices {
private BGCGuide bgcform;
public OnlineBGCServices(BGCGuide bgcform) {
super();
this.bgcform = bgcform;
}
public String performBGC() {
System.out.println("check
in Police Database for fname :"
+ bgcform.getFname());
System.out.println("check
in Prisoners Database for lname :"
+ bgcform.getLname());
System.out.println(bgcform.getAddressLine1());
System.out.println(bgcform.getAddressLine2());
System.out.println(bgcform.getCity());
System.out.println(bgcform.getState());
System.out.println(bgcform.getPin());
// check in Police Database
// check in Prisoners Database
// check in BlackList Persons Database
// ....
// Return pass/fail
return null;
}
}
If someone needs a BGC . he has to call performBGC()
method .This program accepts the person detail object
which implements BCGGuide Inteface, as follows:
public interface BGCGuide {
public String getFname();
public String getLname();
public String getDob();
public String getAddressLine1();
public String getAddressLine2();
public String getState();
public String getCity();
public String getPin();
}
Following is the simple implementation,
as follows:
(Note: This class is not
required to run the program)
class BCGform implements BGCGuide {
private String fname;
private String lname;
private String dob;
private String addressLine1;
private String addressLine2;
private String state;
private String city;
private String pin;
public String getFname() {
return fname;
}
public void setFname(String fname) {
this.fname = fname;
}
public String getLname() {
return lname;
}
public void setLname(String lname) {
this.lname = lname;
}
public String getDob() {
return dob;
}
public void setDob(String dob) {
this.dob = dob;
}
public String getAddressLine1() {
return addressLine1;
}
public void setAddressLine1(String addressLine1) {
this.addressLine1 = addressLine1;
}
public String getAddressLine2() {
return addressLine2;
}
public void setAddressLine2(String addressLine2) {
this.addressLine2 = addressLine2;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getPin() {
return pin;
}
public void setPin(String pin) {
this.pin = pin;
}
}
My company XYZ decides to use their services ...In my company we
use a data base customer and we already use a customerDetails Object.Below
is bean we are using in our program:
class CustomerDetails {
private String name;
private String age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
Now, We need to create a adaptor which can convert the
CustomerDetails object to BGCGuide Object in order to call there services
We can write a interface customer and our CustomerDetails object implemets this.
interface Customer {
public String getName();
public void setName(String name);
public String getAge();
public void setAge(String age);
public String getAddress();
public void setAddress(String address);
}
class CustomerDetails implements Customer {
private String name;
private String age;
private String address;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}
But we have CustomerDetails Object which implements Customer
interface. But To call there service we need to pass an object which implements
BGCGuide.
We can write a adaptor which implements BGCGuide will take Customer object . Now We need to create a adaptor
which can convert the Customer object to BGCGuide Object.
class MyAdpater implements BGCGuide {
Customer customer;
public MyAdpater(Customer customer) {
super();
this.customer = customer;
}
@Override
public String getFname() {
// TODO
Auto-generated method stub
return customer.getName().split(" ")[0];
}
@Override
public String getLname() {
// TODO
Auto-generated method stub
return customer.getName().split(" ")[1];
}
@Override
public String getDob() {
// TODO
Auto-generated method stub
return null;
}
@Override
public String getAddressLine1() {
// TODO
Auto-generated method stub
return customer.getAddress().split(",")[0];
}
@Override
public String getAddressLine2() {
// TODO
Auto-generated method stub
return customer.getAddress().split(",")[1];
}
@Override
public String getState() {
// TODO
Auto-generated method stub
return customer.getAddress().split(",")[2];
}
@Override
public String getCity() {
// TODO
Auto-generated method stub
return customer.getAddress().split(",")[3];
}
@Override
public String getPin() {
// TODO
Auto-generated method stub
return customer.getAddress().split(",")[4];
}
}
Finally we can call BGCServices by our old Object as follows :
public class CallBGCServices {
public static void main(String[]
args) {
Customer customer = new CustomerDetails();
customer.setName("PARIKSHIT SHARMA");
customer.setAddress("HOUSE NO 30,RELAABLE WOODS,BANGALORE ,KARNATAKA,560006");
BGCGuide bgcGuide = new MyAdpater(customer);
OnlineBGCServices
bgcServices = new
OnlineBGCServices(bgcGuide);
bgcServices.performBGC();
}
}
No comments:
Post a Comment