JSP---网上商城->单例模式

创建一个管理类

 1 public class CustomerMgr {
 2     private static CustomerMgr mgr = null;//声明一个静态的该类的对象
 3 
 4     private CustomerMgr() {//私有的构造方法,只能在类的内部构造对象
 5     }
 6 
 7     public static CustomerMgr getInstance() {
 8         if (mgr == null) {
 9             mgr = new CustomerMgr();
10         }
11         return mgr;
12     }
13 
14     public boolean add(Customer customer) {

例如:调用add方法,可以使用Customer.getInstance().add(customer)

如果不使用单例模式,则需要这样调用

CustomerMgr mgr=new CustomerMgr();

mgr.add(customer);

posted @ 2014-12-04 10:18  框框A  阅读(319)  评论(0编辑  收藏  举报