public static void Demo() { AbstractFactory shapeGen = FactoryProducer.Generator("Shape"); Factory.IShape c = shapeGen.ShapeCreate("Circle"); c.Draw(); Factory.IShape s = shapeGen.ShapeCreate("Square"); s.Draw(); AbstractFactory colorGen = FactoryProducer.Generator("Color"); IColor r = colorGen.ColorCreate("Red"); r.Fill(); IColor b = colorGen.ColorCreate("Blue"); b.Fill(); Console.ReadLine(); }
public MainWindow() { InitializeComponent(); this.Height = 950; this.Width = 925; this.GuiUC.DefineText = "In Factory pattern, we create object without exposing the creation logic to the client and refer to newly created object using a common interface."; StringBuilder stringBuilder = new StringBuilder(); ShapeFactory shapeFactory = new ShapeFactory(); // get an object of Circle and call its draw method. Factory.IShape shape1 = shapeFactory.GetShape("Circle"); // call draw method of Circle stringBuilder.Append(shape1.Draw() + "\n"); // get an object of Rectangle and call its draw method. IShape shape2 = shapeFactory.GetShape("Rectangle"); // call draw method of Rectangle stringBuilder.Append(shape2.Draw() + "\n"); // get an object of Square and call its draw method. IShape shape3 = shapeFactory.GetShape("Square"); // call draw method of circle stringBuilder.Append(shape3.Draw() + "\n"); this.GuiUC.TextBlockText = stringBuilder.ToString(); this.GuiUC.TextBlockConsequence = @" Provides hooks for subclasses • Creating objects inside a class with a factory method is always more flexible than creating an object directly • Gives subclasses a hook for providing an extended version of an object Connects parallel class hierarchies • In the examples we've considered so far, the factory method is only called by Creators. But this doesn't have to be the case; clients can find factory methods useful, especially in the case of parallel class hierarchies"; }
public void DrawSquare() { square.Draw(); }
public void DrawCircle() { circle.Draw(); }