`

浅谈门面模式

阅读更多
举一个通俗的例子,购买衣服、电器、首饰
首先建立相应的物品类

//衣服类
public class Clothing
{
    public Clothing getClothing()
    {
        return Clothing;
    }
}

//电器类
public class Ele
{
    public Ele getEle()
    {
        return Ele;
    }
}

//首饰类
public class Jewelry
{
    public Jewelry getJewelry()
    {
        return Jewelry;
    }
}

/**--------------------------按照传统方式---------------------------*/
public class Buy
{
    //你需要到服饰店
    Clothing c = new Clothing();
    c.getClothing();  //购买到衣服

     //接着去电器店
     Ele c = new Ele();
    c.getEle();  //购买到电器

     //然后去首饰店
     Jewelry c = new Jewelry();
    c.getJewelry();  //购买到首饰
}

这样客户类和后台交互很频繁,很枯燥,而且你需要和一个一个商店类打交道,很麻烦,那就换一种方式
/**--------------------------按照门面模式---------------------------*/

//创建购物中心类
public class ShopingCenter
{
    //到购物中心的服饰店购买
    public void clothingStore()
    {
        Clothing c = new Clothing();
        c.getClothing();  //购买到衣服
    }

    //到购物中心的电器店购买
     public void EleStore()
    {
        Ele c = new Ele();
        c.getEle();  //购买到电器
    }
   
    //到购物中心的首饰店购买
    public void JewelryStore()
    {
        Jewelry c = new Jewelry();
        c.getJewelry();  //购买到首饰
    }
}



好了,现在直接去购物中心,不用一家一家跑了
   ShopingCenter sc = new ShopingCenter();
   sc.clothingStore();
   sc.EleStore();
   sc.JewelryStore();

  • 描述: 门面模式
  • 大小: 47.4 KB
  • 描述: 传统方式
  • 大小: 48.8 KB
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics