PHP Design Pattern---Abstract mode mode

Source: Internet
Author: User

Code structure diagram:



Abstract Factory Definition: Abstract Factory mode provides an interface to create a series of related or interdependent objects without specifying their specific class (official definition look at the real egg hurt yourself Baidu understand ^_^)
Requirements: Different database SQL syntax, implementation from different databases to get users and new users, access to departments and new departments

User Table Abstract Product A
Class user{

}
Department Table abstract Product B
Class department{

}

Interface for decoupling the definition of a specific abstract product
Interface iuser{
Public function Insert (user $user);
Public Function GetUser ($id);
}

Class Mysqluser implements iuser{
Public function Insert (user $user) {
Echo ' Adds a record <br/> ' to the user table in MySQL;
}
Public Function GetUser ($id) {
Echo ' Gets a record of the user table in MySQL by id <br/> ';
}
}

Class Sqlserveruser implements iuser{
Public function Insert (user $user) {
Echo ' Adds a record <br/> ' to the user table in Sqlserve;
}
Public Function GetUser ($id) {
Echo ' Gets a record of the user table in the Sqlserve by ID <br/> ';
}
}


Department decoupling defines interfaces for specific abstract products
Interface idepartment{
Public Function Insert (department $d);
Public Function getdepartment ($id);
}
Class Mysqldepartment implements idepartment{
Public Function Insert (department $d) {
Echo ' Adds a record <br/> ' to the department table in MySQL;
}
Public Function Getdepartment ($id) {
Echo ' Gets a record of the department table in MySQL based on id <br/> ';
}
}

Class Sqlserverdepartment implements idepartment{
Public Function Insert (department $d) {
Echo ' Adds a record <br/> ' to the Department table in SQL Server;
}
Public Function Getdepartment ($id) {
Echo ' Gets a record of the department table in SQL Server based on ID <br/> ';
}
}

Factory method
Interface factory{
Public function CreateUser ();
Public function createdepartment ();
}

Specific Factory class
Class Mysqlfactory implements factory{
Public Function CreateUser () {
return new Mysqluser ();
}
Public Function createdepartment () {
return new Mysqldepartment ();
}
}


Specific Factory class
Class Sqlserverfactory implements factory{
Public Function CreateUser () {
return new Sqlserveruser ();
}
Public Function createdepartment () {
return new Sqlserverdepartment ();
}
}

Client
Class client{
Public function Show () {
$user = new User (); User table
$dept = new Department (); Department table

$factory = new Mysqlfactory (); MySQL specific factory class It's just so easy to change the factory class here.
$u = $factory->createuser ();
$u->insert ($user);
$u->getuser (1);

$d = $factory->createdepartment ();
$d->insert ($dept);
$d->getdepartment (1);
}
}

$n = new Client ();
$n->show ();

All the original blog has been verified by me, I Caishuxueqian, just started to write a blog I hope you have a lot of support problems, please point out that I promptly correct





PHP design Mode---Abstract mode mode

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.