C#设计模式-Builder生成器模式

      生成器模式的定义:将一个复合对象的结构和对象的呈现分开,这样相同的结构可以生成不同的显示。

     考虑下面这个例子,每个国家都有它自己的饮食习惯,运动习惯,娱乐习惯,导游(Direct)会根据自己的顾客国籍安排不同的菜谱,推荐不同的运动项目和娱乐节目,从而生成不同的活动清单。(Product)

     生成活动清单接口前,先定义一个国家类

     public   class  Country
        
{
            
public string foodcustomer;//饮食习惯
            public string sportcustomer;//运动
            public string funcustomer;//娱乐
            
//----------
            public void setfoodcustomer(string str)
            
{
                foodcustomer
=str;
            }

            
public void setsportcustomer(string str)
            
{
                sportcustomer
=str;
            }

            
public void setfuncustomer(string str)
            
{
                funcustomer
=str;
            }

            
//显示
            public void show(Label lb)
            
{
                lb.Text
="饮食习惯 "+foodcustomer+"<BR>运动习惯 "+sportcustomer+"<BR>娱乐习惯 "+funcustomer;
            }

        }

 接着定义接口

 

// 定义生成活动清单的接口
         public   interface  BuilderIntface
        
{
            
void BuildFood();//创建饮食习惯
            void BuildSport();
            
void BuildFun();
            Country GetCountry();
        }

        
// 继承上面接口的两个国籍
         public   class  China:BuilderIntface
        
{
            
private Country china;
            
public void BuildFood()
            
{
                china
=new Country();
                china.setfoodcustomer(
"饺子");
            }

            
public void BuildSport()
            
{
                china.setsportcustomer(
"乒乓球");
            }


            
public void BuildFun()
            
{
                china.setfuncustomer(
"喜欢爬山");
            }

            
public Country GetCountry()
            
{
                
return china;
            }

        }

        
public   class  Ameirican:BuilderIntface
        
{
            
private Country american;
            
public void BuildFood()
            
{
                american
=new Country();
                american.setfoodcustomer(
"汉堡");
            }

            
public void BuildSport()
            
{
                american.setsportcustomer(
"蓝球");
            }


            
public void BuildFun()
            
{
                american.setfuncustomer(
"喜欢游泳");
            }

            
public Country GetCountry()
            
{
                
return american;
            }

        }

        
// 定义导游类
         public   class  Direct
        
{

            
public void setCountyStyle(BuilderIntface countyname)
            
{
                countyname.BuildFood();
                countyname.BuildSport();
                countyname.BuildFun();
            }

        }

最后按钮事件来实现

 

private   void  Button2_Click( object  sender, System.EventArgs e)
        
{
            Direct director
=new Direct();
            BuilderIntface b1
=new China();
            BuilderIntface b2
=new Ameirican();
            
//创建两个不同的活动清单
            director.setCountyStyle(b1);
            Country p1
=b1.GetCountry();
            p1.show(lbResult1);
            
//
            director.setCountyStyle(b2);
            Country p2
=b2.GetCountry();
            p2.show(lbResult2);
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值