Abstract Factory mode example (using the Locki class library)

xiaoxiao2021-03-06  27

This article tries to apply the Abstract Factory mode with the Singleton component and the AbstractFactory component in the LOKI class library. The sample code is as follows:

#include

#include

#include "abstractFactory.h"

#include "singleton.h"

Using namespace std;

Using namespace loki;

// Abstract Products a and b

Struct a {Virtual ~ a () {}};

Struct B {Virtual ~ b ()}};

// Concrete Products A1 and B1

Struct A1: Public A

{

A1 () {cout << "a1 :: ctor" << endl;}

}

Struct B1: PUBLIC B

{

B1 () {cout << "b1 :: ctor" << endl;}

}

// Concrete Products A2 and B2

Struct A2: Public A

{

A2 () {cout << "a2 :: ctor" << endl;}

}

Struct B2: PUBLIC B

{

B2 () {cout << "b2 :: ctor" << endl;}

}

// Traditional Method Begins

Class AbstractFactory11

{

PUBLIC:

Virtual a * cretea () = 0;

Virtual b * createb () = 0;

}

Class Concretefactory1: Public AbstractFactory11

{

PUBLIC:

Virtual a * cretea () {return new a1 ();

Virtual b * createb () {return new b1 ();

Static Concretefactory1 & Instance () {

Static contradefactory1.

Return Instance;

}

protected:

CONCRETEFACTORY1 () {}

Private:

ConcreteFactory1 (Const Concretefactory1 &);

Concretefactory1 & Operator = (const concretefactory1);

}

Class Concretefactory2: Public AbstractFactory11

{

PUBLIC:

Virtual a * cretea () {return new a2 ();

Virtual b * createb () {Return New B2 ();

Static contRetefactory2 & instance () {

Static contradefactory2 instance;

Return Instance;

}

protected:

ConcreteFactory2 () {}

Private:

ConcreteFactory2 (Const Concretefactory2 &);

Concretefactory2 & operator = (const concretefactory2);

}

// Traditional Method Ends

// Loki Method Begins

TypeDef AbstractFactory AF;

Typedef SingletonHolder > SCF1;

Typedef Singletonhold SCF2;

// Loki Method Ends

void main ()

{

Cout << "Traditional Method: << ENDL;

// Create All Concrete Products Using Traditional Method

Auto_PTR PTA1 (Concretefactory1 :: Instance (). Createa ());

Auto_PTR PTB1 (ConcreteFactory1 :: Instance (). Createb ());

A * PTA2 = Concretefactory2 :: Instance (). Createa ();

Delete PTA2;

B * PTB2 = ConcreteFactory2 :: Instance (). Createb ();

DELETE PTB2;

COUT << "Loki Method:" << Endl;

// Create All Concrete Products USING LOKI METHOD

Auto_PTR PLA1 (scf1 :: instance (). Create ());

Auto_PTR PLB1 (SCF1 :: Instance (). Create ());

A * PLA2 = SCF2 :: Instance (). Create ();

DELETE PLA2;

B * PLB2 = SCF2 :: Instance (). Create ();

Delete PLB2;

}


New Post(0)