Abstract Factory Pattern - Structural Example

xiaoxiao2021-03-05  43

UML Class Diagram

using System; // "AbstractFactory" abstract class AbstractFactory {// Methods abstract public AbstractProductA CreateProductA (); abstract public AbstractProductB CreateProductB ();} // "ConcreteFactory1" class ConcreteFactory1: AbstractFactory {// Methods override public AbstractProductA CreateProductA () { return new ProductA1 ();} override public AbstractProductB CreateProductB () {return new ProductB1 ();}} // "ConcreteFactory2" class ConcreteFactory2: AbstractFactory {// Methods override public AbstractProductA CreateProductA () {return new ProductA2 ();} override public AbstractProductB CreateProductB () {return new ProductB2 ();}} // "AbstractProductA" abstract class AbstractProductA {} // "AbstractProductB" abstract class AbstractProductB {// Methods abstract public void Interact (AbstractProductA a);} // "ProductA1 "Class ProductA1: AbstractProducta {} //" ProductB1 "Class ProductB1: AbstractProductB {// Methods Override Public Void Interact (AbstractProduct) A a) {Console.WriteLine (this "interacts with" a);}} // "ProductA2" class ProductA2: AbstractProductA {} // "ProductB2" class ProductB2: AbstractProductB {// Methods override public void Interact (AbstractProductA a) {Console.WriteLine (this "interacts with" a);}} // "Client" - the interaction environment of the productsclass Environment {// Fields private AbstractProductA AbstractProductA; private AbstractProductB AbstractProductB; // Constructors public Environment ( AbstractFactory Factory) {AbstractProductB = Factory.createProductB (); AbstractProducta = Factory.createProducta ();

转载请注明原文地址:https://www.9cbs.com/read-33927.html

New Post(0)