The Bridge Pattern is used to separate out the interface from its implementation. Doing this gives the flexibility so that both can vary independently.
public class PersonViewAction extends ActionBase
{
public ActionForward execute(ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception
{
if (form.isBtnSave())
{
PersonHelper helper = new PersonHelper(form.getPerson);
helper.doUpdate();
Person person = helper.getPerson();
}
}
}
public class PersonHelper implements PersonHelperAbstractIF
{
private Person person = new Person();
private PersonAccessorIF personDelegate = new PersonAccessorXML();
public PersonHelper()
{
super();
}
public PersonHelper(Person person){
this.person = person;
}
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public List doSearch()throws Exception{
return personDelegate.getPersonSearchResults(this.person);
}
public void doRetrieve()throws ProcessingException{
this.person = personDelegate.retrievePersonProfile(this.person.getId);
}
public void doAdd() throws ProcessingException{
this.person = personDelegate.addPersonProfile(this.person)
}
public void doUpdate()throws ProcessingException{
this.person = personDelegate.updatePersonProfile(this.person);
}
public boolean doDelete(){
personDelegate.deletePersonProfile();
}
}
public interface PersonAccessorIF {
public Person retrievePersonProfile(Integer personId)throws ProcessingException;
public List getPersonSearchResults(Person person)throws Exception;
public Person addPersonProfile(Person person)throws ProcessingException;
public Person updatePersonProfile(Person person)throws ProcessingException;
}
public class PersonAccessorXML implements PersonAccessorIF{
public PersonAccessorXML(){
super();
}
public Person retrievePersonProfile(Integer personId)throws ProcessingException
{
// actual implementation - maybe send a XML message to another system
}
public List getPersonSearchResults(AgentProfile agent, MemberCustomerProfileSearchBean searchBean)throws ProcessingException, IOException{
return null;
}
public Person addPersonProfile(AgentProfile agent, MemberCustomerProfile customer){
return null;
}
public Person updatePersonProfile(AgentProfile agent, MemberCustomerProfile customer){
return null;
}
}
public class PersonAccessorDB implements PersonAccessorIF{
public Person retrievePersonProfile(AgentProfile agent, Integer customerId, Integer memberProfileId){
// different kind of implementation to PersonAccessorXML - maybe access the database to get the result
}
public List getPersonSearchResults(AgentProfile agent, MemberCustomerProfileSearchBean searchBean)throws ProcessingException, IOException{
return null;
}
public Person addPersonProfile(AgentProfile agent, MemberCustomerProfile customer){
return null;
}
public Person updatePersonProfile(AgentProfile agent, MemberCustomerProfile customer){
return null;
}
}
Now if we were to change the backend processing (say from sending XML message to another system TO say database access, we can easily change the functionality in the PersonHelper class by just modifying a line of code like below:
private PersonAccessorIF personDelegate = new PersonAccessorDB();
No comments:
Post a Comment