反射反射,程序员的快乐?手表自动贩卖机中应用反射!

需求分析:
写一个自动贩卖牌子手表的程序。
测试例子:
两个牌子,一个CASIO,一个SWATCH
(仅程序中示例,方便理解。没有什么映射牌子的含义……)
晕。“免责声明”比上面两个说明都长……

=====================================================

类分析:
解决方案一共有两个项目,
一个是assembly
Program.cs //main程序。
App.config //

一个是DBHelper(无缘无故就改了这个名字,估计是看书妄想症……)
absBannerFactory.cs
absBranner.cs
BannerFactory.cs
Casio.cs
Swatch.cs

=======================
重点说明的,就是dbhelper啦!
先说说关系:(直接上源文件了)
//absBannerFactory.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace DBHelper
{
         public abstract class absBannerFactory
        {
                 public abstract absBranner GetBanner();
        }
}


//=====
//absBranner.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace DBHelper
{
         public abstract class absBranner
        {
                 private string bnis;

                 public string Bnis
                {
                        get { return bnis; }
                        set { bnis = value; }
                }
                 private DateTime timeNow = DateTime.Now;

                 public DateTime TimeNow
                {
                        get { return timeNow; }
                }
                 public abstract DateTime GetTimeNow();

                 public abstract string GetBanner();
        }
}

//====
//BannerFactory.cs

using System;
using System.Collections.Generic;
using System.Text;
using System.Configuration;
using System.Reflection;
namespace DBHelper
{
         public class BannerFactory : absBannerFactory
        {
                 public string Bn = ConfigurationManager.AppSettings[ "bn"].ToString();
                 public string ablyName = "DBHelper";            
                
                

                 public override absBranner GetBanner()
                {
                         string typename = ablyName + "." + Bn;
                         return (absBranner)Assembly.Load(ablyName).CreateInstance(typename);
                }

                
        }
}


//=====
//Casio.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace DBHelper
{
         public class Casio:absBranner
        {
                 public override DateTime GetTimeNow()
                {
                         return TimeNow;
                }
                 public override string GetBanner()
                {
                         return Bnis = "Casio";
                }
        }
}


//===
//Swatch.cs
using System;
using System.Collections.Generic;
using System.Text;

namespace DBHelper
{
         public class Swatch:absBranner
        {

                 public override string GetBanner()
                {
                         return Bnis = "Swatch";
                }

                 public override DateTime GetTimeNow()
                {
                         return TimeNow;
                }
        }
}

这里说明一下,方便看(UML不是很会,我编辑也很烂)
casio.cs
swatch.cs
是实体的,继承自absBranner
BannerFactory.cs
是实体的,继承自absBannerFactory
那两个abs的就是抽象类咯……
========好了,这DBHelper咯,下面说assembly。
//program.cs
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using System.Configuration;
using DBHelper;
namespace assembly
{
         class Program
        {
                 static void Main( string[] args)
                {

                        Console.WriteLine( "=================我要看手表了==================");
                        absBannerFactory bfy = new BannerFactory();
                        absBranner b = bfy.GetBanner(); //哈哈,看不到我要看什么牌子的


                        Console.WriteLine( "我要看的牌子是{0}", b.GetBanner().ToString());
                        Console.WriteLine( "那现在的时间是{0}",b.GetTimeNow().ToString());

                        Console.ReadKey();


                }
        }
}

//App.config
<? xml version ="1.0" encoding ="utf-8" ?>
< configuration >
     < appSettings >
        <!-- <add key="Bn" value="Casio"/>-->
         < add key ="Bn" value ="Swatch" />
     </ appSettings >
</ configuration >

最后,要说明的就是符合需求么?
假如真的要生产。program是不用动的,多增加几个牌子的实体类就行,另外通过app.config文件就可以控制当前program卖的是哪个牌子的手表咯!方便不?

算了,还是别说那种荣耀的话了,各位,拍砖吧……
另外上一句名言:
程序员的三美德---急躁,懒惰和自大

你可能感兴趣的:(反射,程序员,自动,快乐,手表)