public Client() { Factory = new T(); _productA = Factory.CreateVegetables(); _productB = Factory.CreateFruits(); }
//UNIT TEST public void Testc() { IAbstractFactory factory = new ConcreteFactory2(); IProductA productA = factory.CreateProductA(); IProductB productB = factory.CreateProductB(); //。。。。。。。。。。。。 //虽然客户端依赖抽象的IAbstractFactory,但是为了获得IAbstractFactory,需要new ConcreteFactory2(),逻辑上和具体的工厂类型耦合在一起,“依赖注入”非常适用于结局这个问题,比如可以把ConcreteFactory2写在配置文件里,通过读取配置文件动态构造IAbstractFactory。 }
public Promotion(IProductA productA, IProductB productB, IProductC productC, IProductD productD) { _productA = productA; _productB = productB; _productC = productC; _productD = productD; }
public void AbstractFactoryTest() { IAbstractFactory factory = new ConcreteFactory1(); IProductA productA = factory.CreateProductA(); IProductB productB = factory.CreateProductB(); Assert.AreEqual(typeof(ProductA1), productA.GetType()); Assert.AreEqual(typeof(ProductB1), productB.GetType()); }
public void Test() { IAbstractFactory factory = new ConcreteFactory2(); IProductA productA = factory.CreateProducctA(); IProductB productB = factory.CreateProducctB(); Assert.AreEqual <Type>(typeof(ProductA2Y), productA.GetType()); Assert.AreEqual <Type>(typeof(ProductB2), productB.GetType()); }
public void Test() { IAbstractFactory factory = AssemblyFactory(); IProductA productA = factory.Create <IProductA>(); IProductB productB = factory.Create <IProductB>(); Assert.AreEqual <Type>(typeof(ProductA1), productA.GetType()); Assert.AreEqual <Type>(typeof(ProductB1), productB.GetType()); }
public void Test1() { IAbstractFactory factory = AssemblyFactory(); IProductA productA = factory.Create <IProductA>(); IProductB productB = factory.Create <IProductB>(); Assert.IsInstanceOf(typeof(ProductA1), productA); Assert.IsInstanceOf(typeof(ProductB1), productB); }
/// <summary> /// Конструктор класса /// </summary> /// <param name="factory">Объект который реализует интерфейс IFactory</param> public Client(IFactory factory) { if (factory == null) { throw new ArgumentNullException(); } a = factory.GetProductA(); b = factory.GetProductB(); }
public static void GetDependentInstance() { var container = Utilities.UnityConfig.Register(); _promotion = container.Resolve <IPromotion>(); _productA = container.Resolve <IProductA>(); _productB = container.Resolve <IProductB>(); _productC = container.Resolve <IProductC>(); _productD = container.Resolve <IProductD>(); }
public void ProduceTest() { IAbstractFactory factory = (new Assembler()).Create <IAbstractFactory>();//通过依赖注入,从配置文件生成 IProductA product1 = factory.CreateProductA(); Assert.AreEqual("A1", product1.Type); IProductB product2 = factory.CreateProductB(); Assert.AreEqual("B1", product2.Name); }
static void Main() { IFactory factory = new ConcreteFactory1(); IProductA productA = factory.CreateProductA(); productA.ShowA(); IProductB productB = factory.CreateProductB(); productB.ShowB(); Console.ReadKey(); }
public void AbstractFactoryWithMapperTest() { IDictionary <Type, Type> dictionary = new Dictionary <Type, Type>(); dictionary.Add(typeof(IProductA), typeof(ProductA1)); dictionary.Add(typeof(IProductB), typeof(ProductB1)); IAbstractFactoryWithMapper factory = new ConcreteFactory(dictionary); IProductA productA = factory.Create <IProductA>(); IProductB productB = factory.Create <IProductB>(); Assert.AreEqual(typeof(ProductA1), productA.GetType()); Assert.AreEqual(typeof(ProductB1), productB.GetType()); }
private static void Test() { _firstFactory = new FirstFactory((string Text) => Console.WriteLine(Text)); _secondFactory = new SecondFactory((string Text) => Console.WriteLine(Text)); IProductA firstProductA = _firstFactory.CreateA(); IProductA secondProductA = _secondFactory.CreateA(); IProductB firstProductB = _firstFactory.CreateB(); IProductB secondProductB = _secondFactory.CreateB(); firstProductA.OperationA(); firstProductB.OperationB(); secondProductA.OperationA(); secondProductB.OperationB(); }
static void Main(string[] args) { Console.WriteLine("Use Products from Factory 1"); IAbstractFactory factory = new ConcreteFactory1(); IProductA productA1 = factory.GetProductA(); IProductB productB1 = factory.GetProductB(); productA1.MethodX(); productA1.MethodY(); productB1.Method1(); productB1.Method2(); Console.WriteLine("Use Products from Factory 2"); factory = new ConcreteFactory2(); IProductA productA2 = factory.GetProductA(); IProductB productB2 = factory.GetProductB(); productA2.MethodX(); productA2.MethodY(); productB2.Method1(); productB2.Method2(); Console.ReadLine(); }
static void TestAbstractFabric() { Console.Write("Select category first or second:"); int category = int.Parse(Console.ReadLine()); IAbstractFactory factory; if (category == 1) { factory = new FactoryFirstClass(); } else { factory = new FactorySecondClass(); } IProductA productA = factory.CreateProductA(); IProductB productB = factory.CreateProductB(); Console.WriteLine(productA.OperationA()); Console.WriteLine(productB.OperationB()); Console.WriteLine(productB.OperationWithProductA(productA)); }
public string AnotherUsefulFunctionB(IProductA collaborator) { var result = collaborator.UsefulFunctionA(); return($"The result of the collaboration with the ({result})"); }
public void Interact(IProductA productA) { // Product B interacts with Product A }
public void Init() { this.a = this.f.CreateProductA(); this.b = this.f.CreateProductB(); }
// Ürün B2 varyantı, yalnızca Ürün A2 varyantıyla doğru şekilde çalışabilir. // Bununla birlikte, ProductA'nın herhangi bir örneğini bir argüman olarak kabul eder. public string AnotherFunctionB(IProductA collaborator) { var result = collaborator.FunctionA(); return($"The result of the B2 collaborating with the ({result})"); }
public string OperationWithProductA(IProductA product) { return($"{this.OperationB()} AND {product.OperationA()}"); }
public ClientCode(IAbstractFactory factory) { productA = factory.CreateProductA(); productB = factory.CreateProductB(); }
public void Test() { IAbstractFactoryGai factory = (new Assembler()).AssembleyFactory(); IProductA productA = factory.Create <IProductA>(); IProductB productB = factory.Create <IProductB>(); }
public Client(IFactory factory) { productA = factory.CreateProductA(); productB = factory.CreateProductB(); }
public Client(IAbstractfactory factory) { _iProductA = factory.createProductA(); _iProductB = factory.createProductB(); }
public void Interact(IProductA a) { Console.WriteLine(this.GetType().Name + " interacts with " + a.GetType().Name); }