设计模式系列漫谈 - 圣诞专题

   值此圣诞节来临之际,观察者小姐 策略小姐 装饰小姐 迭代器小姐 代理小姐 职责链小姐祝全国人民圣诞节快乐!!

 

 

public class Client
{
      
public static void Main()
      { 
            Console.WriteLine(
"祝圣诞节快乐");
      }
}


       我一直在杭州工作,猛一回头都快8年。同事,同学,生意上的合作伙伴,朋友交了不少。我想,他们这个时候他们一定在撮骂我,李华星这小子太不够意思了,圣诞节来了,也不表示一下。等等,正准备着呢······

public interface IFriend
{
      
void Say();
}
public class Fellow : IFriend
{
      
private string _Name;
      
public Fellow(string name)
      {
            
this._Name=value;
      }
      
public void Say()
      {
            Console.WriteLine(
this._Name+",在下祝你圣诞节快乐");
      }
}
public class Classmate : IFriend
{
      
private string _Name;
      
public Classmate(string name)
      {
            
this._Name=value;
      }
      
public void Say()
      {
            Console.WriteLine(
this._Name+",在下祝你圣诞节快乐");
      }
}
public class Partner : IFriend
{
      
private string _Name;
      
public Partner(string name)
      {
            
this._Name=value;
      }
      
public void Say()
      {
            Console.WriteLine(
this._Name+",在下祝你圣诞节快乐");
      }
}
public class Mobile
{
         
private ArrayList friends= new ArrayList();
         
public void Add(IFriend f)
         {
              friends.Add(f)
         }
         
private void SayBless()
        {
            
for (int i = 0; i < friends.Count; i++)
            {
                 IFriend friend
=(IFriend )friends[i];
                 friend.Say();
            }
        }
}



public class Client

{

      
public static void Main()

      { 

            Mobile mobile
=new Mobile();
            mobile.Add(
new Fellow("许小丹"));
            mobile.Add(
new Classmate("谢春晖"));
            mobile.Add(
new Partner("刘协伟"));

            mobile.SayBless();

      }

}
//此为观察者模式

 

       3月起,我担任公司技术总监一职。经过近一年的努力,整个公司的技术团队已经慢慢壮大,公司技术实力已经逐渐走上成熟。在这里,我要感谢所有与我一起工作、一起奋斗的同事们。另外,我还要感谢我的老婆一年来对我工作的支持,感谢我的特别可爱的宝贝儿子,感谢所有关心我的人······

public interface IPeople
{
      
void Say();
}
public class Fellow : IPeople
{
      
public void Say()
      {
            Console.WriteLine(
"感谢大家对我工作的支持,祝圣诞节快乐");
      }
}
public class Wife: IPeople
{
      
public void Say()
      {
            Console.WriteLine(
"亲爱的,你辛苦了,祝圣诞节快乐,永远爱你");
      }
}
public class Son: IPeople
{
      
public void Say()
      {
            Console.WriteLine(
"宝贝,爸爸祝你圣诞节快乐,永远开心");
      }
}
public class SayContext
{
        IPeople _people
= null;
        
public void SetStrategy(IPeople  people)
        {

            
this._people= people;
        }
        
public void Say()
        {
            
return this._people.Say();
        }
}
public class Client
{
      
public static void Main()
      {
            SayContext context
=new SayContext();
            
            context.SetStrategy(
new Fellow());
            context.Say(); 

            context.SetStrategy(
new Wife());
            context.Say();
           
            context.SetStrategy(
new Son());
            context.Say();
      }

}
//此为策略模式

 

   男人给女人送花一般会选择玫瑰。但是我老婆不喜欢玫瑰,她喜欢百合,她对我说百合是象征着纯洁的爱情。说来惭愧,记得最后一次给我妻子送百合还是她三年前的生日,后来也没送她什么礼物了,为此,她不时拿这个来说事儿。这次,我希望向妻子表示感谢的同时,我想给她送上一束百合花,宝贝儿子不能落下,给他送个玩具警车(我儿子特别喜欢车)。

public interface IPeople
{
      
void Say();
}

public interface IGift
{
     
void Send();
}

public class Wife: IPeople
{
      
public void Say()
      {
            Console.WriteLine(
"亲爱的,你辛苦了,祝圣诞节快乐,爱你的华星");
      }
}

public class Son: IPeople
{
      
public void Say()
      {
            Console.WriteLine(
"宝贝,爸爸祝你圣诞节快乐");
      }
}

public class Flower: IGift
{
      
public void Send()
      {
            Console.WriteLine(
"百合花");
      }
}

public class Toy: IGift
{
      
public void Send()
      {
            Console.WriteLine(
"玩具警车来了");
      }
}

public abstract class PeopleDecorator:IPeople
{
       
private IPeople _people;
       
public IPeople people
       {
          
get{return _people;}
       }

       
public PeopleDecorator(IPeople p)
       {
          
this._people=p;
       }

       
public virtual void Say()
       {
           _people.Say();
       }
}

public class GiftPeopleDecorator:PeopleDecorator
{
       
private IGift _gift;
       
public ISound gift
       {
          
get{return _gift;}
          
set{_gift=value;}
       }

       
public GiftPeopleDecorator(IPeople p):base(p)
       {
       }

       
public override void Say()
       {
              _gift.Send();
              
base.Say();
       }
}

public class Client
{
      
public static void Main()
      { 
            GiftPeopleDecorator gp
=new GiftPeopleDecorator(new Wife());
            gp.IGift
=new Flower();
            gp.Say();

            gp
=new GiftPeopleDecorator(new Son());
            gp.IGift
=new Toy();
            gp.Say();

      }

}}
//此为装饰模式


     
     
一切准备就绪。全国人民也祝了,朋友也表示了,老婆儿子也感谢了,还有送了礼物。
      我怎么办呢?忘了告诉大家一件事情,知道为什么我这么多年没给我老婆送礼物吗?一方面是我工作确实很忙,没提前准备好。等真要送礼物那天,都来不及了。更重要的原因是我们两地分居,想送也送不了,真是可怜。在此,望老婆大人见谅。
      看来忙完这个,真的没事干吗? 我得再整出点事来。明天圣诞节总不能又去吃快餐吧,有没有哪个朋友有空请我吃饭啊? 我马上得逐个问一下。

public interface IFriend
{
      
bool HasFree();
 }
public class FriendA: IFriend
{
      
private string _Name;
      
public FriendA(string name)
      {
            
this._Name=value;
      }
      
public bool HasFree()
      {
            Console.WriteLine(
this._Name+"回家陪老婆去");
            
return false;
      }
}
public class FriendB: IFriend
{
      
private string _Name;
      
public FriendB(string name)
      {
            
this._Name=value;
      }
      
public bool HasFree()
      {
            Console.WriteLine(
this._Name+"回家陪老妈去");
            
return false;
      }
}
public class FriendC: IFriend
{
      
private string _Name;
      
public FriendC(string name)
      {
            
this._Name=value;
      }
      
public bool HasFree()
      {
            Console.WriteLine(
this._Name+"晚上要跟女朋友Happy去哦");
            
return false;
      }
}
public interface IListIterator
{
           
void First();
           
void Last();
           
bool MovePrevous();
           
bool MoveNext();
           
object Current{get;}
 }
public class FriendList : IListIterator
 {
           
private ArrayList friends= new ArrayList;//子对象列表
           private int index=-1;
           
public FriendList ()
           {
               
           }
           
//添加子对象
           public void Add(IFriend f)
           {
               friends.Add(f);
           }
           
public void RemoveBoy(IFriend f)
           {
               friends.Remove(f);
               index
--;
           }
           
public void First()
           {
               index
=0;
           }
           
public void Last()
           {
               index
=boys.Count;
           }
           
public bool MovePrevious()
           {
               index
--;
               
return index>-1;
           }
           
public bool MoveNext()
           {
               index
++;
               
return index<friends.Count;
           }
           
public object Current
           {
               
get {return this.friends[index];}
           }
}



public class Client

{

      
public static void Main()

      { 

            IListIterator iterator
=new FriendList();
            iterator.Add(
new FriendA("谢春晖"));           
            iterator.Add(
new FriendB("许小丹")); 
            iterator.Add(
new FriendC("刘有发"));
            iterator.Add(
new FriendC("闵现瑞"));

            
while (iterator.MoveNext())
            {
                   IFriend friend
=(IFriend )iterator.Current;
                  
if (friend.HasFree())
                      Console.WriteLine(
"今天晚上终于有着落了");
            }


      }

}}
//此为迭代器模式


      
我晕!?,忙了半天,连一顿饭都没着落。算了,还是继续到楼下阿姨那里吃快餐去,呜。。。呜。。。。吃完快餐,继续折腾我我的Design Parttern。我也不想问再问其他朋友有没有空请我吃饭了,也不一一单独祝福这些朋友。但是,朋友还要问候一下的。全部默认 " 圣诞节快乐!",除非遇到我以前的初恋情人。

public abstract Friend
{
         
public bool  isFirstLover=false;
         
public string strBless="";
         
public virtual void Say()
         {
                Console.WriteLine(strBless);
         }
}
public class GoodFriend: Friend
{
      
public override void Say()
      {
            
base.Say();            
      }
}
public class ProxyGoodFriend: Friend
{
      
private GoodFriend _gf;
      
public ProxyGoodFriend(GoodFriend gf)
      {
            
this._gf=gf;
      }
      
public override void Say()
      {
            
if (base.isFirstLover)
                  
this._gf.Say();
            
else
                  
base.Say();            
      }
}
public class Mobile
{
         
private ArrayList friends= new ArrayList();
         
private string _defaultBless;

         
public Mobile(string defaultBless)
         {
               
this._defaultBless=defaultBless;
         }
         
public void Add(IFriend f)
         {
              friends.Add(f)
         }

         
private void SayBless(bool isFirst)
        {
            
for (int i = 0; i < friends.Count; i++)
            {
                  IFriend friend
=(IFriend )friends[i];
                  friend.isFirstLover
=isFirst;
                  friend.strBless
=this._defaultBless;
                  friend.Say();
            }
        }

        
//当是初恋情人
        public void OnCheck()
        {
             
this._defaultBless="你在他乡还好吗?";
             Notify(
true);
        }
}



GoodFriend gf
=new GoodFriend();

Mobile mobile
=new Mobile("圣诞节快乐!")
mobile.AddBoy(gf);}
//此为代理模式



一旦是初恋情人,就mobile.OnCheck(); 千万别被我老婆知道了!:)

   过个圣诞节真够累的。过几天又是元旦,再过一个多月春节又来了,元宵,情人节,端午,儿童节,中秋...... 为了下次过节轻松一点,我的提前准备一下。
   对于节日,我认为至少有两点不同。一是不同的节日有不同时间,有些是阳历,有些是阴历;二是不同节日有不同庆祝方式。再者,节日是年复一年,不断循环。想知道今天是什么节日吗?想知道用什么方式庆祝吗?下面我们来解决这个问题。

public abstract class Festival
{
   
protected Festival _festival;
   
public void SetFestival(Festival festival)
   {
      
this._festival = festival;
   }
   
public abstract string Celebrate(string strDate,string strBless);
}

public class Solar:Festival //阳历
{
   
public override string Celebrate(string strDate,string strBless);
   {
      
if (System.DateTime.Now.Date.ToString("MM月dd日")==strDate)
      {
         Console.WriteLine(strBless);
      }
      
else
      {
         
ifbase._festival != null )
            
base._festival.Celebrate(strDate,strBless);
      }
   }
}

public class Lunar:Festival //阴历
{
   
public override string Celebrate(string strDate,string strBless);
   {
      
//ChineseCalendarInfo阴历阳历互相转化的类,实现方法请见键盘敲击者cncxz中的 一个阴历阳历互相转化的类(c#源码) 

      ChineseCalendarInfo ccInfo
= new ChineseCalendarInfo(System.DateTime.Now.Date);

      
if (ccInfo.LunarMonthText+""+ccInfo.LunarDayText+""==strDate)
      {
         Console.WriteLine(strBless);
      }
      
else
      {
         
ifbase._festival != null )
            
base._festival.Celebrate(strDate,strBless);
      }
   }
}

public class Client
{
  
public static void Main()
  {
    Festival f1 
= new Solar();
    Festival f2 
= new Lunar();
    f1.SetFestival(f2);
    
    
string[] aryDate={"12月25日","一月一日","02月14日","五月五日","八月十五日"};
    
string[] aryCelebration={"圣诞节快乐","新年好","愿天下有情人终成眷属","端午节别忘了吃棕","中秋团圆"};


    
for(int i=0;i<aryDate.Length;i++)
       h1.SendBless(aryDate[i],aryCelebration[i]);
  }
}}
//此为职责链模式



圣诞专题到此结束,本想继续, 太晚了,明天还得上班. 感谢各位阅读,不妥之处,若您忍不住想砸砖,看在圣诞爷爷的份上,请轻点,顺祝各位工作顺利,圣诞节快乐。最后,由于引用了以下两名作者大作内容,特此表示感谢。

伍迷家园         键盘敲击者cncxz

posted @ 2007-12-24 20:05  李华星  阅读(1775)  评论(10编辑  收藏  举报