清單 1. 隱藏已不使用的方法 public Person getDirector() { // We can't call getDirector() any more on the entity bean // Call the new method, through this manager List directors = getDirectors(); // Return the first one in the list return (Person)directors.item(0); }
清單 2. 用會(huì)話 bean 掩蓋數(shù)據(jù)結(jié)構(gòu) public List getCrew(String movieName) throws NamingException, RemoteException { List crew = new LinkedList(); EJBHomeFactory f = EJBHomeFactory.getInstance(); MovieHome movieHome = (MovieHome)f.lookup("java:comp/env/ejb/Movie", MovieHome.class); Movie movie = movieHome.findByName(movieName); crew.add(movie.getDirectors()); crew.add(movie.getProdUCers()); crew.add(movie.getExecutiveProducers()); // and so on... return crew; } public List getCast(String movieName) throws NamingException, RemoteException { List cast = new LinkedList(); EJBHomeFactory f = EJBHomeFactory.getInstance(); MovieHome movieHome = (MovieHome)f.lookup("java:comp/env/ejb/Movie", MovieHome.class); Movie movie = movieHome.findByName(movieName); crew.add(movie.getActors()); crew.add(movie.getStandIns()); // and so on... return cast; }
通過分離不同的應(yīng)用程序?qū)樱约笆褂脴I(yè)務(wù)邏輯來處理數(shù)據(jù)操作,您既阻止了對實(shí)體 bean 直接而且可能不安全的訪問,又為您的 Web 層創(chuàng)建了更有意義的方法集。本例中,會(huì)話虛包充當(dāng)了實(shí)體 bean 的封裝器以及真正的業(yè)務(wù)邏輯單元,從而將原始數(shù)據(jù)轉(zhuǎn)變成有意義的信息。