外观设计模式,《大话设计模式》第103页详细讲解,不记得这块知识的小伙伴可以翻阅翻阅,看过设计模式,敲过书上的例子,只是学习的第一步,接着,如果在我们的项目中灵活应用,把设计模式用出花儿来,才是王道,有人总说,纸上得来终觉浅,绝知此事要躬行,可是真到了躬行的时候,总是行不通,语言倒不过来,设计模式是c#语言的,机房收费是vb.net版本的,书上的模式和机房联系不起来,不知道怎么应用,没办法,憋呗,看博客,问人,上网查资料,就这样,憋出一个外观,虽不太完美,但却异常宝贵,因为那是我纠结好长时间从尘埃里开出的花,就外观模式的应用在这里小小的总结一下。

就机房收费中的一个注册功能来说,着手之前,我们需要做以下思考工作:

a、从学生表里查询,是否存在该学号;

b、从卡表里查询,是否存在该卡号;

c、在学生表里插入一条记录;

d、在卡表里插入一条记录;

e、在充值表里插入一条记录;

在外观层我的代码如下:(该博文,重点阐述外观层的写法,其她层再此不再赘述)

'**********************************************
'文 件 名: registerfacade
'命名空间: facade
'内    容: 从卡表和学生表里面查询是否存在该卡号和学号,存在,给出提示,不存在,注册成功之后,一次向卡表,学生表和充值表中写入相关信息
'功    能: 注册
'文件关系:
'作    者:丁国华
'小    组:宝贝计划
'生成日期: 2014/7/17 15:06:56
'版本号:v2.0
'修改日志:
'版权说明:
'**********************************************

public class registerfacade
    '/// 
    '/// depiction:
    '/// 
    '/// 
    '/// 
    '///
    '/// 
    public function querystudentno(byval studentno as string) as list(of entity.studententity)
        dim studentbll as new bll.t_studentbll
        dim mylist as list(of entity.studententity)

        mylist = studentbll.querystudentno(studentno)
        if mylist.count > 0 then
            throw new exception("该学号已经存在")
        else
            return mylist
        end if
    end function
    '/// 
    '/// depiction:
    '/// 
    '/// 
    '/// 
    '///
    '/// 
    public function querycardno(byval cardno as string) as list(of entity.cardentity)
        dim cardbll as new bll.t_cardbll
        dim mylist as list(of entity.cardentity)

        mylist = cardbll.querycardno(cardno)
        if mylist.count > 0 then
            throw new exception("该卡号已经存在")
        else
            return mylist
        end if
    end function

    '/// 
    '/// depiction:
    '/// 
    '/// 
    '/// 
    '///
    '/// 
    public function insertstudent(byval enstudent as entity.studententity) as boolean
        dim studentbll as new bll.t_studentbll
        dim flag as boolean

        flag = studentbll.insertstudent(enstudent)
        return flag
    end function
    '/// 
    '/// depiction:
    '/// 
    '/// 
    '/// 
    '///
    '/// 
    public function insertcard(byval encard as entity.cardentity) as boolean
        dim cardbll as new bll.t_cardbll
        dim flag as boolean

        flag = cardbll.insertcard(encard)
        return flag
    end function
   
    '/// 
    '/// depiction:
    '/// 
    '/// 
    '/// 
    '///
    '/// 
    public function insertrecharge(byval enrecharge as entity.rechargeentity) as boolean
        dim rechargebll as new bll.t_rechargebll
        dim flag as boolean

        flag = rechargebll.insertrecharge(enrecharge)
        return flag

    end function

end class

接着,我们再来看下一个充值的功能,着手之前,我们需要做一下思考工作:

a、从卡表里面查询,是否存在该卡号;

b、在充值表里面插入一条充值记录;

c、更新卡表中的余额

和上述的注册功能相比较,两个功能都需要从卡表里面查询和向充值表里面插入一条记录。所以充值这个功能,她的接口层(idal),d层(dal),factory-工厂层,bll-业务逻辑层,她的代码写法和上述的注册功能的写法都是一样的,我们就不需要在写一次,直接调用就可以了,但是外观层要怎么写呢,写着写着就写不下去了,按着以前的写法,只需要在外观层写一个更新卡表中余额的方法就行了,从卡表中查询和在充值表中插入一条充值记录,只需要调用注册的外观就可以了,可是,这样写的话,u层就调用了两个外观层,那还是外观层么?显然不是,看人程杰老师的大话设计模式中,外观的写法是把小的方法汇总成一个总的方法,写在一个大的方法里面,那充值的外观到底如何写呢?如下:

'**********************************************
'文 件 名: rechargefacade
'命名空间: facade
'内    容:
'功    能:
'文件关系:
'作    者:丁国华
'小    组:宝贝计划
'生成日期: 2014/7/18 22:18:04
'版本号:v2.0
'修改日志:
'版权说明:
'**********************************************

public class rechargefacade
    '/// 
    '/// depiction:
    '/// 
    '/// 
    '/// 
    '///
    '/// 
    public function querycard(byval cardno as string) as list(of entity.cardentity)
        dim cardbll as new bll.t_cardbll
        dim mylist as list(of entity.cardentity)

        mylist = cardbll.querycardno(cardno)
        if mylist.count = 0 then
            throw new exception("该卡号不存在")
        else
            return mylist
        end if

    end function

    '/// 
    '/// depiction:
    '/// 
    '/// 
    '/// 
    '///
    '/// 
    public function recharge(byval encard as entity.cardentity, byval enrecharge as entity.rechargeentity) as boolean
        dim cardbll as new bll.t_cardbll
        dim rechargebll as new bll.t_rechargebll
        dim flag(2) as boolean

        flag(0) = rechargebll.insertrecharge(enrecharge)
        flag(1) = cardbll.updatecard(encard)


        if flag(0) and flag(1) then
            return flag(0)
        else
            return false
        end if
    end function
end class

小伙伴肯定有疑问,上面注册的外观层不是仍然有两个方法?简单,解释一下,因为,查询卡号的时候,我们需要返回一个卡表的实体,然后从里面找到以前的余额,然后还要加上充值的金额,形成一个新的余额,更新的时候才能有一个卡表实体,下面recharge的那个方我们的返回值boolean,true为充值成功,一个方法不可能有两个返回值,所以写成两个方法,总的来说就是具体外观层有几个方法,是由返回值定的。第二遍机房收费系统,未完,待续……