3. Abstract Factory Model
Zookeeper
1. the core content of the abstract factory model is:
A: different consumers can be created at will.
B: different operations
C: create different consumers and corresponding operations based on different factories.
2. Abstract The role of the factory model: different people, different operations can meet the needs of different people and create different operations through the factory.
3. Description of the abstract factory Model
Factory model: the customer class and the factory class are separated.
A consumer needs a product at any time and only requests from the factory.
Consumers can accept new products without modification. The disadvantage is that when the product is modified,
The factory class must also be modified accordingly. For example, how to create and provide data to clients.
It's easy to catch up with MM. McDonald's chicken wings and KFC's chicken wings are what MM loves to eat,
Although the taste is different, whether you take MM to McDonald's or KFC,
Just say "four chicken wings" to the waiter. McDonald's and KFC are factories that produce chicken wings.
The consumer is not fixed, and the factory is not fixed (the factory acts according to the consumer)
Implements the consumer abstraction base class. The implementation of the consumer derived class, instantiation is the consumer
The abstract base class of an operation to implement various operations and instantiate operations of a derived class.
The abstract class of the factory. The abstract class contains two abstract class interfaces (consumer, operation)
Abstract classes implement factory class abstraction, instantiate the derived class, implement factory,
Set the user according to the user and perform operations according to the operation.
4. Rule mode class diagram
5. Code:
# Include
# Include
Usingnamespacestd;
// Factory mode: the customer class and the factory class are separated.
// The consumer needs a product at any time and only requests from the factory.
// The consumer can accept new products without modification. The disadvantage is that when the product is modified,
// Modify the factory class. For example, how to create and provide data to clients.
//
// Try to catch up with MM. The chicken wings of McDonald's and KFC are what MM loves to eat,
// Although the taste is different, whether you take MM to McDonald's or KFC,
// Just say "four chicken wings" to the waiter. McDonald's and KFC are factories that produce chicken wings.
// The consumer is not fixed, and the factory is not fixed (the factory acts according to the consumer)
// Implement the consumer abstraction base class. The consumer derived class is implemented, and the instantiation is the consumer
// The abstract base class of the Operation to implement various operations and instantiate operations of the derived class
// Factory abstract class. The abstract class contains two abstract class interfaces (consumer, operation)
// The abstract class implements the abstract of the factory class, instantiate the derived class, and implement the factory,
// Set the user based on the user and perform operations according to the operation.
ClassIUser
{
Public:
// Pure Virtual Interface Class, abstract class
VirtualvoidgetUser () = 0;
VirtualvoidsetUser () = 0;
};
// Inherit the abstraction to instantiate the sqldatabase user
ClassSqlUser: publicIUser
{
Public:
VoidgetUser ()
{
Cout <"Return user in SQL" <endl;
}
VoidsetUser ()
{
Cout <"set user in SQL" <endl;
}
};
// Inherit abstraction to instantiate access Data Users
ClassAccessUser: publicIUser
{
Public:
VoidgetUser ()
{
Cout <"Return user in Access" <endl;
}
VoidsetUser ()
{
Cout <"set user in Access" <endl;
}
};
// Abstract class that provides Interfaces
ClassIDepartment
{
Public:
VirtualvoidgetDepartment () = 0;
VirtualvoidsetDepartment () = 0;
};
// SQL operation implementation
ClassSqlDepartment: publicIDepartment
{
Public:
VoidgetDepartment ()
{
Cout <"Return Department in SQL" <endl;
}
VoidsetDepartment ()
{
Cout <"set Department in SQL" <endl;
}
};
// Access operation implementation
ClassAccessDepartment: publicIDepartment
{
Public:
VoidgetDepartment ()
{
Cout <"Department returned in Access" <endl;
}
VoidsetDepartment ()
{
Cout <"set Department in Access" <endl;
}
};
// Abstract Factory
ClassIFactory
{
Public:
Required aliuser * createUser () = 0;
Required alidepartment * createDepartment () = 0;
};
// An implementation of the abstract factory
ClassSqlFactory: publicifacloud
{
Public:
IUser * createUser ()
{
ReturnnewSqlUser ();
}
IDepartment * createDepartment ()
{
ReturnnewSqlDepartment ();
}
};
// An implementation of the abstract factory
ClassAccessFactory: publicifacloud
{
Public:
IUser * createUser ()
{
ReturnnewAccessUser ();
}
IDepartment * createDepartment ()
{
ReturnnewAccessDepartment ();
}
};
// Implements static classes in disguise
ClassDataAccess
{
Private:
Staticstringdb;
// String db = "access ";
Public:
StaticIUser * createUser ()
{
If (db = "access ")
{
ReturnnewAccessUser ();
}
Elseif (db = "SQL ")
{
ReturnnewSqlUser ();
}
}
StaticIDepartment * createDepartment ()
{
If (db = "access ")
{
ReturnnewAccessDepartment ();
}
Elseif (db = "SQL ")
{
ReturnnewSqlDepartment ();
}
}
};
StringDataAccess: db = "SQL ";
Intmain ()
{
// IFactory * factory = new SqlFactory ();
IFactory * factory; // Abstract factory
IUser * user; // abstract consumer
IDepartment * department; // operation provided
Factory = newAccessFactory (); // the pointer to the base class refers to the object of the derived class
User = factory-> createUser (); // the pointer of the base class points to the object of the derived class
Department = factory-> createDepartment (); // the pointer of the base class points to the object of the derived class
User-> getUser ();
User-> setUser (); // access the acesss Interface
Department-> getDepartment ();
Department-> setDepartment (); // Interface
Cout <"------------------" <
User = DataAccess: createUser ();
Department = DataAccess: createDepartment ();
User-> getUser ();
User-> setUser ();
Department-> getDepartment ();
Department-> setDepartment ();
Cin. get ();
Return 0;
}
The running result is as follows: