Abstract Factory-Design Model

Source: Internet
Author: User

1. Understanding Abstract Factory design patterns the book says: it provides an interface for creating a series of related or mutually dependent objects without having to define their specific classes. 2. view the class chart
COMMENTATOR: The picture consists of two parts: the ifacloud family on the left and the IProduct family on the right. 1. Check the IProduct family first. Level 1: customer requirements. We can see what products we need, which is determined by the client. We need to design the second-level products in advance. Level 2: ProductA and ProductB. This series of products should be determined based on actual conditions. The second layer should be noted that the attributes of this specific product remain unchanged, and the various operations we need to perform on this product are changed, in order to comply with the open-closed, and encapsulation principles. When modifying a method, attributes are not affected. We divide this product class into two parts. One part is a specific noun class, which does not contain any methods, but only attributes of this class; the other part is the abstract interface: defines the operation methods for this class. In this way, we can perform separate operations on the attributes or methods of a class without affecting each other and implement object-oriented encapsulation. They are dependent on specific classes. This internal relationship has not been realized yet.
That is to say, the third layer implements the interface method of the second layer, and the access and operation are specific classes of the second layer. Layer 3: A1 and A2 are specific implementation classes. They are the specific implementation process of ProductA. There may be multiple implementations, SO and multiple implementation classes. Of course, a unified interface is also required for unified management. The interface here is located at the second layer. The powerful second layer interface can be expanded on the third layer, which is also an encapsulation. Principles embodied in openness-the principle of closure: openness to expansion, and closure to modification. On this layer-3 diagram, there are two interfaces, that is, two areas that may change. One is the database choice and the other is the database table access choice. Dependency reversal principle: Abstract Factory design is gradually abstracted from top to bottom, and it is for interface programming. The specific implementation is considered at the last layer, which improves code reuse, they respectively reflect that high-level modules do not depend on low-level modules; abstraction does not depend on details. Single principle: each class on the third layer implements a single function and accesses a database table. For example, if you need to access the database, the second layer is the database table, which of the following databases is available; extended from the first layer to the second layer in order to access more or other database tables in the future. Now we want to access the user table, or in the future, we need to access the company department table, or the company's financial table; the third layer should consider the specific database Access and operation implementation. The extensible child classes include SQL server, Access or oracle. These are all different implementations, but they belong to the implementation level. Access may be used now, and SQL server or Oracle will be used in the future. Therefore, we need to prepare the interface on the second layer for multiple implementations on the third layer. 2. Check the ifacloud family. The factory here is not a specific manufacturing product factory, but an intermediary office. Why? The first layer defines an interface for creating a series of related or mutually dependent objects. For example, multiple database tables to be accessed are defined in them. This is also a difference from the factory model. The factory model is for one type, while the abstract factory is for a series of products. Layer 2: The specific access methods help users find the Layer 3 address in the Iproduct family based on the objects instantiated by the client. Trainer: ACCESS user tables and department tables in SQL server and ACCESS databases. [Csharp] // defines the User table class -- defines the User table attribute class User {private int id; public int ID {get {return id ;} set {id = value ;}} private string name; public string Name {get {return name ;}set {name = value ;}}} // define the Database User table interface -- defines the operation interface IUser {void Insert (user User); user GetUser (int id );} // define the sqldatabase table class SqlserverUser: IUser {public void Insert (User user User) {Console. writeLine ("add a user in SQL Record ");} public User GetUser (int id) {Console. writeLine ("Get a User record by ID in SQL"); return null ;}// define the Access database table class AccessUser: IUser {public void Insert (user User) {Console. writeLine ("add a User record in Access");} public User GetUser (int id) {Console. writeLine ("Get a user record by ID in Access"); return null ;}// define the database Department table -- attribute class Department {private int id; public int ID {get {return id;} set {id = v Alue ;}} private string dname; public string DName {get {return dname ;}set {dname = value ;}}} // define the Operation Department interface -- operation interface IDepartmnet {void Insert (department Department); department GetDepartment (int id);} // define the SQL implementation class SQLdepartment for Department access: IDepartmnet {public void Insert (Department department Department) {Console. writeLine ("insert a record in SQL! ");} Public Department GetDepartment (int id) {Console. WriteLine (" retrieve the Department by ID! "); Return null ;}// defines the Access implementation class Accessdepartment: IDepartmnet {public void Insert (Department department Department) {Console. writeLine ("insert a record in SQL! ");} Public Department GetDepartment (int id) {Console. WriteLine (" retrieve the Department by ID! "); Return null ;}}// define the factory interface to access the database table ifacloud {IUser CreateUser (); IDepartmnet CreatDepartment () ;}// instantiate the SQL factory interface class SqlFactory: ifacloud {public IUser CreateUser () {return new SqlserverUser ();} public IDepartmnet CreatDepartment () {return new SQLdepartment () ;}// instantiate the Access factory Interface class AccessFactory: ifacloud {public IUser CreateUser () {return new AccessUser ();} public IDepartmnet CreatDepartment () {return new Accessdepartment () ;}} static void Main (string [] args) {User user = new User (); Department department = new Department (); IFactory factory = new SqlFactory (); IUser iu = factory. createUser (); iu. insert (user); iu. getUser (2); // any int type can be ifacw.factory1 = new AccessFactory (); IUser au = factory1.CreateUser (); au. insert (user); au. getUser (2); // any int type can be ifac1_factoryd = new SqlFactory (); IDepartmnet id = factoryd. creatDepartment (); id. insert (department); id. getDepartment (1); ifacw.factorya = new AccessFactory (); IDepartmnet ad = factoryd. creatDepartment (); ad. insert (department); ad. getDepartment (1); www.2cto.com // IUser au = new AccessUser (); // au. insert (user); // au. getUser (2); // any int type can be Console. read ();}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.