Php design patterns-Abstract factory patterns

Source: Internet
Author: User
Php design patterns-Abstract factory patterns

Provides an interface for creating a set of related or interdependent objects without specifying their specific classes.

Features

Abstract Factory is a common software design model that provides a unified creation interface for a product family. When you need a series of product families, you can create a specific factory class for these product families.

Primary role

Abstract Factory role: it declares an interface for creating Abstract product objects. It is usually implemented using an interface or abstract class. all factory classes must implement this interface or inherit this class.

Concrete Factory role: create a product object. The client directly calls this role to create a product instance. This badge contains the logic for selecting the right product object. It is usually implemented using a specific class.

Abstract Product role: declares the interfaces of a Product. It is the parent class of the object created in the factory method mode, or the interfaces they share.

Concrete Product role: implements the interface defined by the abstract Product role and defines a Product object that will be created by a specific factory. It contains the business logic of the application.

Advantages and disadvantages

Advantages of the Abstract factory model:

1. separated specific classes

2. make it easy to add or replace product families

3. conducive to product consistency

Disadvantages of the Abstract factory model:

It is difficult to support new types of products. This is because the AbstractFactory interface determines the product set that can be created. To support various new products, you need to extend the access factory interface, resulting in changes to the AbstractFactory class and all its subclasses.

Abstract factory supports the addition of new products in a skewed manner. it facilitates the addition of new product families, rather than providing convenience for the addition of new product level structures.

Applicability

1. a system should not depend on the details of how product instances are created, combined, and expressed. this is important for all forms of factory models.

2. there are more than one product family in this system, and the system only consumes products of one of them.

3. products belonging to the same product family are used together. this constraint must be reflected in the system design.

4. The system provides a product library. all products use the same interface, so that the client does not rely on the implementation.

Abstract Factory mode instance

 CreateCat (); $ cat-> Voice (); $ dog = $ AnimalFactory-> createDog (); $ dog-> Voice () ;}} Client :: main ();?>

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.