FactoryControl control = new CommentControl();
            FactoryInfomation info = new FactoryInfomation(control);
            control.Insert(info);
            //等价于CommentControl.Insert(new Comment());看代码基本上能看明白吧,就是用抽象控制类实例一个控制子类,抽象产品类实例一个产品子类,
执行控制子类的方法,传参数产品子类,可是这样,我就得在控制子类里继续沿用抽象控制类里的参数类型!即抽象产品类!可我想在控制子类中用产品子类做参数!不明求解!

解决方案 »

  1.   

    如电脑 a = new 方正电脑();
    电脑桌 b = new 电脑桌(a);//返回一个[方正电脑桌,假设已有此子类]
    a.方法(b);因为抽象电脑类定义方法时,参数类型是抽象电脑桌类;
    那么我在电脑的所有子类里,都必须沿用这个类型?即抽象电脑桌类;如果,还有一层,如DAL层,则继续将此类型的参数往后传?
    我想在,电脑的子类方法里就改回来,改成接收[电脑桌]子类,
    因为我知道,方正电脑就是配套方正电脑桌,已提前预知;
    如何实现!
      

  2.   

    FactoryInfomation也是个抽象类,而且必然有一个子类继承它。
    当你new一个CommentControl并传入后,面向对象机制就会自动用FactoryInfomation的一个子类来创建这个对象,并返回一个CommentControl的实例。FactoryControl也是个抽象类,当你control.Insert(info);时,会调用相应的子类来插入所以上面的代码实际上就是在控制子类中用产品子类做参数,你自己不用管了
      

  3.   

    to time_is_life:
    你说得正是我的本意!可是,我在定义FactoryControl类时,使用FactoryInfomation类型做为参数,
    那么我在CommentControl的定义中,override Insert方法时(父类中使用的抽象类做参数,即FactoryInfomation)
    我继续沿用父类的参数类型??像这样?
            public override int Insert(FactoryInfomation info){
                return db.Insert(info);
            }然后在DB中继续沿用
            public virtual int Insert(FactoryInfomation info)
            {
                return 0;
            }就这样?总感觉不对!以上两个方法,我想传入他们各自的子类类型做参数!而不是继续使用抽象产品类!~
      

  4.   

    在DB中的代码:
            public override int Insert(FactoryInfomation info)
            {
                info = new Comment();//或者强制转换一下?Comment oInfo = (Comment)info;此处郁闷!
                return base.Insert(info);
            }
      

  5.   

    你的子类中少了一个factory()方法,所以你不会调用了,看看下面的代码你就明白了。
    public abstract class Create
    {
        public abstract VideoWiring factory();
    }public class DVDCreate: Create 
    {
        public override VideoWiring factory()
        {
            return new DVD();
        }
    }public class VCDCreate: Create 
    {
        public override VideoWiring factory()
        {
            return new VCD();
        }
    }        //调用的代码
            private void PlayVideo()
            {
                VideoWiring dvd,vcd;
                Create dvdCreate,vcdCreate;
                dvdCreate=new DVDCreate();
                dvd=dvdCreate.factory();
                Play(dvd);
                vcdCreate=new VCDCreate();
                vcd=vcdCreate.factory();
                Play(vcd);
            }Play函数的参数应该固定的。这也是抽象工厂模式的适用条件。使用抽象工厂模式的条件:
    1 一个系统不应依赖于产品如何被创建,组合和表达的细节。
    2 有多个产品族,而系统只消费其中一个族中的产品
    3 同属于一个产品族的产品是在一起使用的。
    4 系统提供一个产品的库,所有产品都是以同样的接口实现。
      

  6.   

    我感觉你的需求用Provider模式更合适
      

  7.   

    我的子类中有此方法
    FactoryInfomation info = new FactoryInfomation(control);//这就是根据不同的Control返回不同的子类实例
    另外,你的Play方法接受的参数类型是什么?是抽象产品类还是产品子类?
      

  8.   

    Play方法接受的类型就是一个播放字符串,也就是无论vcd还是dvd都播放同一个字符串。
    所以你传给Play的参数必须是同一个类型。比如Play( MyClass1 )
    但是不能既传给MyClass1又传给MyClass2.
    你看看抽象工厂模式的条件的第4条:
    4   系统提供一个产品的库,所有产品都是以同样的接口实现。
      

  9.   

    在你的这段代码中:
    public override int Insert(FactoryInfomation info)
            {
                info = new Comment();//或者强制转换一下?Comment oInfo = (Comment)info;此处郁闷!
                return base.Insert(info);
            }你可以给FactoryInfomation一个type属性,根据不同的type来进行不同的操作,这就是产品库的作用。
      

  10.   

    CSDN技术中心(C#)利用反射动态调用类成员
    (C#)利用反射动态调用类成员. 使用反射动态调用类成员,需要Type类的一个方法:InvokeMember。对该方法的声明如下(摘抄于MSDN):. public object InvokeMember( ...是这个嘛?
      

  11.   

    >Play方法接受的类型就是一个播放字符串,也就是无论vcd还是dvd都播放同一个字符串。 
    不好意思,这句话说错了,应该是:
    Play方法接受的类型就是一个播放字符串,也就是无论vcd还是dvd传送给Play的都是字符串,可以借助字符串的不同来识别是vcd还是dvd,
    比如vcd打出的字符串为:正在播放vcd。 
        vcd打出的字符串为:正在播放dcd
      

  12.   

                            VideoWiring   dvd,vcd;
                            Create   dvdCreate,vcdCreate;
                            dvdCreate=new   DVDCreate();
                            dvd=dvdCreate.factory();
                            Play(dvd);
                            vcdCreate=new   VCDCreate();
                            vcd=vcdCreate.factory();
                            Play(vcd); 
    就是说,你这Play()方法接收VideoWiring类型的参数是吧!
    Play()是谁的方法呢?
    假如是Create类的方法,
    那么Create的子类再override Play方法时,继续沿用VideWiring类型的参数?
      

  13.   

    VideoWiring就是一个独立的类。
    你Create出来一个VideoWiring类的实例,
    然后交给Play去运行。不同的Create产生不同的VideoWiring的实例。
    但是Play只接收VideoWiring类型的实例。
    就像我们做蛋糕的时候,要做一个新形状的蛋糕时,就先产生一个模子,这个VideoWiring就是模子,
    把这个模子交到厨房,厨房不用管模子是什么形状的,向里面填满面就烤。
    但是如果你给的不是模子,而是一副图纸,那厨房可就不干了
      

  14.   

    像你的这种情况,你可以在FactoryInfomation类型的实例中指定类型,然后
    public   override   int   Insert(FactoryInfomation   info) 
                    { 
                            switch( info.type )
                            {
                              case 1:
                                 //一种处理
                                 break;
                              case 2:
                                 //另一种处理
                                 ....
                            }
                    } 
      

  15.   

    当然你也那种继续override的思路,那么就是:
    public   abstract   class   VideoWiring

            public   abstract   Play();
    } public   class VCD : VideoWiring

            public override Paly()
            {
               //播放VCD
            }
    } public   class DVD : VideoWiring

            public override Paly()
            {
               //播放DVD
            }
    } 那么调用代码就改成:
                            VideoWiring   dvd,vcd;
                            Create   dvdCreate,vcdCreate;
                            dvdCreate=new   DVDCreate();
                            dvd=dvdCreate.factory();
                            dvd.Play();
                            vcdCreate=new   VCDCreate();
                            vcd=vcdCreate.factory();
                            vcd.Play(); 
      

  16.   

    //使用时想这样使用
            protected void Page_Load(object sender, EventArgs e)        
            {
                FactoryControl control = new CommentControl();
                FactoryInfomation info = new FactoryInfomation(control);
                control.Insert(info);
                //CommentControl.Insert(new Comment());
            }
    抽象产品类
        public class FactoryInfomation
        {
            public FactoryInfomation() { }        public FactoryInfomation(FactoryControl control) {
                control.Create();
            }
        }
    抽象产品子类
        public class Dict : FactoryInfomation{}
        public class Comment : FactoryInfomation{}
        public class Topic : FactoryInfomation{}
    抽象控制类
        public class FactoryControl
        {
            public FactoryControl() { }        public virtual FactoryInfomation Create() {
                return null;
            }        public virtual int Insert(FactoryInfomation info){
                return 0;
            }

        }
    控制子类
        public class CommentControl : FactoryControl
        {
            private FactoryDataBase db;
            public CommentControl() {
                db = new CommentDb();
            }        /// <summary>
            /// 创建实例
            /// </summary>
            /// <returns></returns>
            public override FactoryInfomation Create(){
                return new Comment();
            }        public override int Insert(FactoryInfomation info){
                return db.Insert(info);
            }
        }
    控制子类
        public class CommentDb:FactoryDataBase
        {
            public CommentDb() { }        public override int Insert(FactoryInfomation info)
            {
                //info = new Comment();
                return base.Insert(info);
            }        public override bool Update(FactoryInfomation info)
            {
                info = new Comment();
                return base.Update(info);
            }
        }我能不能在控制子类里,不再继续沿用FactoryInfomation info
    而是使用FactoryInfomation的子类,如Comment ,Dict等,
    //在使用时我可以这样写,
                FactoryControl control = new CommentControl();
                FactoryInfomation info = new FactoryInfomation(control);
                control.Insert(info);
    //也可以这样写
                FactoryControl control = new CommentControl();
                //FactoryInfomation info = new FactoryInfomation(control);
                Comment info = new Comment();
                control.Insert(info);
    //问题出来了,如果我此时Dict info = new Dict()再执行CommentControl的Insert()方法时,肯定要出错!
    //但是编译时却不报错,想避免此种情况
    我怎么就说不明白呢?就是工具类的子类的方法,使用产品类的子类做为参数,而不是抽象产品类做为参数
      

  17.   

    参看
    http://terrylee.cnblogs.com/archive/2005/12/13/295965.html最经典的.net 抽象工厂模式 关键只有一句话。下面标红的那句,反射原理,可以任意调用工厂。 1using System;
     2using System.Reflection;
     3
     4namespace AbstractFactory
     5{
     6    /**//// <summary>
     7    /// AbstractFactory类
     8    /// </summary>
     9    public abstract class AbstractFactory
    10    {
    11        public static AbstractFactory GetInstance()
    12        {
    13            string factoryName = Constant.STR_FACTORYNAME.ToString();
    14
    15            AbstractFactory instance;
    16
    17            if(factoryName != "")
    18                instance = (AbstractFactory)Assembly.Load(factoryName).CreateInstance(factoryName);
    19            else
    20                instance = null;
    21
    22            return instance;
    23        }
    24
    25        public abstract Tax CreateTax();
    26
    27        public abstract Bonus CreateBonus();
    28    }
    29}
      

  18.   

    你把insert方法移动到   Dict ,Comment ,Topic  FactoryInfomation类中去,问题就解决了。
                FactoryControl control = new CommentControl();
                FactoryInfomation info = new FactoryInfomation(control);
                info.insert();            FactoryControl control = new DictControl();
                FactoryInfomation info = new FactoryInfomation(control);
                info.insert();
     
                .....
    多少个FactoryInfomation的子类都没有问题
      

  19.   

    //你把insert方法移动到       Dict   ,Comment   ,Topic     FactoryInfomation类中去,问题就解决了。
    这三个类是纯定义啊,只是定义数据成员,没有任何方法滴,
    分别有三个带Control后缀的控制类执行相关的Insert方法,该方法的参数与上述三个类一一对应,要解决就是从抽象产品类到产品子类的过渡...