我有一个界面
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;
以下是一些注意事项:
AbstractCake
,例如NameOfCake
,IsPacked
和IsDecorated
AbstractFactory
andAbstractCake
类根本没有任何实现,请考虑将其更改为接口而不是抽象类,即ICakeFactory
and ICake
。具体实现将ChocolateCakeFactory
与ChocolateCake
以前一样。ICakeFactory
,ICake
和IBox
),并且不需要做任何停机铸造或做任何假设约蛋糕等的实际具体类型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;
}
}
编辑,重新引发变更事件
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] 删除。
我来说两句