抽象类C#

叶夫根尼·库兹涅佐夫(Evgeny Kuznetsov)

我有一个界面

using ClassAbstractFactory;
public interface IPlugin
{
    AbstractFactory GetFactory();  
}

和一个AbstractFactory

public abstract class AbstractFactory
{
    public abstract AbstractCake CreateCake();
    public abstract AbstractBox CreateBox();
}

public abstract class AbstractCake
{

    public abstract void Interact(AbstractBox box);

}

public abstract class AbstractBox
{

}

我有继承了AbstractCake的.dll

 public class ChocolateCake : AbstractCake
{

    private bool _isPacked;
    private bool _isDecorated;
    private string _nameOfCake;

    public ChocolateCake()
    {
        _isPacked = false;
        _isDecorated = false;
        _nameOfCake = "Шоколадный";
    }


   public bool IsPacked
   {
        get { return _isPacked; }
   }

    public bool IsDecorated
    {
        get { return _isDecorated; }

    }

    public string NameOfCake { get; set; }


    public override void Interact(AbstractBox box)
    {     
            _isPacked = true;       
    }


}

我像这样加载dll:

 public IPlugin LoadAssembly(string assemblyPath)
    {
        Assembly ptrAssembly = Assembly.LoadFile(assemblyPath);

        foreach (Type item in ptrAssembly.GetTypes())
        {
            if (!item.IsClass) continue;
            if (item.GetInterfaces().Contains(typeof(IPlugin)))
            {
                return (IPlugin)Activator.CreateInstance(item);
            }
        }
        throw new Exception("Invalid DLL, Interface not found!");
    }

        List<IPlugin> list = new List<IPlugin>();
        foreach (var assemblyPath in GetPathsListToDll())
        {
            list.Add(LoadAssembly(assemblyPath));
        }

我如何在ChocolateCake中访问属性,如

foreach (var str in list)
        {
            Boolean a = str.GetFactory().GetCake().CreateCake().IsPacked;
        }

或像这样

string a = str.GetFactory().GetCake().CreateCake().NameOfCake;

或像这样

str.GetFactory().GetCake().CreateCake().NameOfCake("Something");

或像这样

str.GetFactory().GetCake().CreateCake().IsDecorated(true);
斯图尔特

这里的问题是,该AbstractFactory方法具有返回的方法AbstractCake,而AbstractCake它本身根本没有任何属性。就目前而言,在访问其任何属性之前,您需要将Cake(直接或使用as关键字)转换为ChocolateCake,这确实很麻烦:

string a = (ChocolateCake)(str.GetFactory().CreateCake()).NameOfCake;

以下是一些注意事项:

  1. 此举是为所有类型的蛋糕到性能AbstractCake,例如NameOfCakeIsPackedIsDecorated
  2. 鉴于AbstractFactoryandAbstractCake类根本没有任何实现,请考虑将其更改为接口而不是抽象类,即ICakeFactoryand ICake具体实现将ChocolateCakeFactoryChocolateCake以前一样。
  3. 工厂和蛋糕的消费者应该只能访问什么暴露在接口(ICakeFactoryICakeIBox),并且不需要做任何停机铸造或做任何假设约蛋糕等的实际具体类型

IE

public interface ICake
{
    void Interact(IBox box);
    bool IsPacked { get; }
    bool IsDecorated { get; }
    string NameOfCake { get; set; }
}

public class ChocolateCake : ICake
{
    private bool _isPacked;
    private bool _isDecorated;
    private string _nameOfCake;

    public ChocolateCake() // ctor is not on the interface and is implementation detail
    {
        _isPacked = false;
        _isDecorated = false;
        _nameOfCake = "Шоколадный";
    }       

    public void Interact(IBox box) {...}
    public bool IsPacked { get { return _isPacked; } }
    public bool IsDecorated { get { return _isDecorated; } }
    // ...
}

public interface ICakeFactory
{
    ICake CreateCake();
    IBox CreateBox();
}

public class ChocolateCakeFactory : ICakeFactory
{
    public ICake CreateCake()   {return new ChocolateCake();}
    public IBox CreateBox() {return new ChocolateCakeBox();} 
}

回复:用法

您极不可能这样做:

string a = str.GetFactory().GetCake().CreateCake().NameOfCake;
str.GetFactory().GetCake().CreateCake().NameOfCake = "Something"; // Prop setter

因为这样每次都会创建一个新的Cake实例(并丢弃该实例)。怎么样:

class Bakery
{
   private readonly ICakeFactory _cakeFactory;
   public Bakery(ICakeFactory cakeFactory)
   {
       Contract.Requires(cakeFactory != null);
       cakeFactory = _cakeFactory;
   }

   bool BakeStuff()
   {
       var cake = _cakeFactory.CreateCake();
       cake.NameOfCake = "StackOverflow";
       return cake.IsDecorated && cake.IsPacked;
   }
}

编辑,重新引发变更事件

这涉及实现INotifyPropertyChanged

public interface ICake : INotifyPropertyChanged

然后可以提高可变属性的价值,例如

public string NameOfCake
{
    get { return _nameOfCake} ;
    set { 
        var propChanged = PropertyChanged;
        if (propChanged != null && value != _nameOfCake)
        {
            propChanged(this, new PropertyChangedEventArgs("NameOfCake"));
        }
        _nameOfCake = value;
    }
}

像这样订阅

var cake = new ChocolateCake();
cake.PropertyChanged += (sender, eventArgs) 
     => Console.WriteLine("Property {0} has changed", eventArgs.PropertyName);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

抽象类字段冗余C#

来自分类Dev

抽象类单例C#

来自分类Dev

抽象类事件C#

来自分类Dev

抽象类字段冗余C#

来自分类Dev

抽象类C ++

来自分类Dev

C ++:抽象类

来自分类Dev

C#抽象类具有另一个抽象类对象

来自分类Dev

可以抽象类继承另一个抽象类C#

来自分类Dev

可以抽象类继承另一个抽象类C#

来自分类Dev

界面与抽象类C ++

来自分类Dev

C#查找使用抽象类的位置

来自分类Dev

抽象类不能用C#密封吗?

来自分类Dev

“深度C#”中的抽象类实例化

来自分类Dev

如何从C#中的抽象类实现ObservableCollection <InterfaceType>?

来自分类Dev

C#:使用抽象类强制构造函数签名?

来自分类Dev

如何实例化抽象类C#?

来自分类Dev

在C#中使用抽象类的困惑

来自分类Dev

在PowerShell中提供C#抽象类的实现

来自分类Dev

“深度C#”中的抽象类实例化

来自分类Dev

抽象类不能用C#密封吗?

来自分类Dev

C#:使用抽象类强制构造函数签名?

来自分类Dev

c#和php抽象类实现的区别

来自分类Dev

调用抽象类类型C#的子实现

来自分类Dev

C#:调用通过另一个抽象类实现的抽象类方法

来自分类Dev

C ++中抽象类的函数定义

来自分类Dev

C ++抽象类析构函数

来自分类Dev

抽象类中的c ++克隆函数

来自分类Dev

C ++中抽象类(接口)的数组

来自分类Dev

抽象类的函数定义中的C ++