2007年5月15日星期二

Hide concrete domain pojo

//Domain object pojo impl that should not be visible to other layers of app.//hence declared as package protected.package org.xwt.domain.generalclass AddressImpl implements Address {private String street1;private String state;AddressImpl(String street1, String state){this.street1 = street1;this.state = state;} public String getStreet1() {return street1;}public String getState() {return state;}}//Here is the public interface, which defines the contract.package org.xwt.domain.generalpublic interface Address {public String getStreet1();public String getState();//other methods//Interface provides a factory to create the pojos of different types potentially.public static class Factory {private Factory(){} //provide static methods for constructorspublic static Address getAddress(String street, String state) {return new AddressImpl(street, state);}public static Address getUSAddress(String street, String state) {return new USAddressImpl(street, state);}}}//I don't need a central DomainObjectFactory which will become cluttered with alot of static methods.usage:package org.xwt.app.uiimport org.xwt.domain.general;public class MyController {public void execute(HttpServletRequest request){Address address = Address.Factory.getAddress(request.getParameter("street"),request.getParameter("state"));}}

没有评论: