Notes on C# Design Pattern Abstract Factory

Notes on C# Design Pattern Abstract Factory

C# design patterns are time tested methods in writing code to efficiently create all kinds of applications (i.e. Desktop and Web Applications)

No alt text provided for this image


I will append more information to this page as the content becomes available.?My focus on studying the C# design patterns was to break down the process step by step.?Then testing the understanding with different scenarios.

The Abstract Factory design pattern is written of as one of the most versatile, and flexible.

DoFactory.com is a good design pattern resource.

https://www.dofactory.com/net/abstract-factory-design-pattern

Abstract Factory is remarked as one of the most popular of the C# Design Patterns.

It appears the Abstract Factory is highly proficient, efficient and/or?more flexible in creating objects that are able to be changed taking full advantage of Polymorphism.

Here my notes on the break-down of the C# Design Pattern?Abstract Factory.

Starting with the?public static void Main()

public static void Main()
{
     AbstractMaker maker1 = new SolidMaker1();
}        

AbstractMaker is defined.

abstract?class?AbstractMaker
{
????public?abstract?AbstractItemA?CreateItemA();
????public?abstract?AbstractItemB?CreateItemB();
}        

Define classes?AbstractItemA and?AbstractItemB.

abstract?class?AbstractItemA
{
}
 
abstract?class?AbstractItemB
{
????public?abstract?void?Interact(AbstractItemA?a);
}        

?

Then,?SolidMaker1?is defined.

class?SolidMaker1?:?AbstractMaker
{
????public?override?AbstractItemA?CreateItemA()
????{
????????return?new?ItemA1();
????}
 
????public?override?AbstractItemB?CreateItemB()
????{
????????return?new?ItemB1();
????}
}        

ItemA1 and?ItemB1 are defined.

class?ItemA1?:?AbstractItemA
{
}
 
class?ItemB1?:?AbstractItemB
{
????public?override?void?Interact(AbstractItemA?a)
????{
????????Console.WriteLine(this.GetType().Name?+
????????"?interacting?with?"?+?a.GetType().Name);
????}
}        

At this point, the source code will compile.?

using?System;
 
namespace?AbstractFactoryDemonstration
{
????class?MainApp
????{
????????public?static?void?Main()
????????{
????????????AbstractMaker?maker1?=?new?SolidMaker1();
????????}
????}
 
????abstract?class?AbstractMaker
????{
????????public?abstract?AbstractItemA?CreateItemA();
????????public?abstract?AbstractItemB?CreateItemB();
????}
 
????abstract?class?AbstractItemA
????{
????}
 
????abstract?class?AbstractItemB
????{
????????public?abstract?void?Interact(AbstractItemA?a);
????}
 
????class?SolidMaker1?:?AbstractMaker
????{
????????public?override?AbstractItemA?CreateItemA()
????????{
????????????return?new?ItemA1();
????????}
 
????????public?override?AbstractItemB?CreateItemB()
????????{
????????????return?new?ItemB1();
????????}
????}
 
????class?ItemA1?:?AbstractItemA
????{
????}
 
????class?ItemB1?:?AbstractItemB
????{
????????public?override?void?Interact(AbstractItemA?a)
????????{
????????????Console.WriteLine(this.GetType().Name?+
????????????"?interacting?with?"?+?a.GetType().Name);
????????}
????}
}        
No alt text provided for this image


Adding on to the Main method.

public?static?void?Main()
{
    AbstractMaker?maker1?=?new?SolidMaker1();
    Client client1 = new Client(maker1);
    client1.Run();
}        

Client class is defined.

class Client
{
    private AbstractItemA _abstractItemA;
    private AbstractItemB _abstractItemB;

    //Constructorpublic Client(AbstractMaker maker)
    {
        _abstractItemB = maker.CreateItemB();
        _abstractItemA = maker.CreateItemA();
    }

    public void Run()
    {
        _abstractItemB.Interact(_abstractItemA);
    }
}        


No alt text provided for this image

?

Start creating the second?Abstract Maker and building the?Main method.

public?static?void?Main()
{
    AbstractMaker?maker1?=?new?SolidMaker1();
    Client client1 = new Client(maker1);
    client1.Run();

    AbstractMaker maker2 = new SolidMaker2();
    Client client2 = new Client(maker2);
    client2.Run();
}        

Then, build the class derived from abstract class?AbstractMaker.

class SolidMaker2 : AbstractMaker
{
    public override AbstractItemA CreateItemA()
    {
        return new ItemA2();
    }

    public override AbstractItemB CreateItemB()
    {
        return new ItemB2();
    }
}        

Define?ItemA2 and?ItemB2.

class ItemA2 : AbstractItemA
{
}

class ItemB2 : AbstractItemB
{
    public override void Interact(AbstractItemA a)
    {
????????????Console.WriteLine(this.GetType().Name?+
 ???????????"?interacting?with?"?+?a.GetType().Name);
    }
}        

After this addition the complete source will successfully compile with this outline.


No alt text provided for this image

要查看或添加评论,请登录

社区洞察

其他会员也浏览了