How to realize real estate agency with PHP

Time:2022-1-5

Intermediary model

What is the intermediary model? Actually, it’s easy to understand. That’sintermediary, the intermediary may not be unfamiliar to everyone, and the real estate intermediary has received a lot of calls; The intermediary model acts as a real estate intermediary, just like if we want to rent a house, we only need to establish contact with the intermediary, while the house source and the owner do not need us to establish contact.

The mediator model is mainly used to decouple many to many relationships. The previous model without a mediator is similar to a mesh structure. Each object needs to be connected. If it needs to be changed, all objects may need to be changed, which will affect the whole body.

Below is the mallpurchasesalestockAs an example, the structure of decoupling without mediator mode is as follows:

How to realize real estate agency with PHP

After using the mediator mode, the coupling between objects can be reduced, and all objects are handed over to the mediator to contact. The structure becomes like this:

How to realize real estate agency with PHP

Compare the following UML class diagrams:

  • Mediator pattern class diagram:
    How to realize real estate agency with PHP

  • Class diagram of non mediator mode:

    How to realize real estate agency with PHP

The comparison between the two shows that without the mediator mode, the object coupling is serious, and each needs to establish a connection.

Definition of mediator pattern

Mediator mode: encapsulate a series of object interactions with a mediation object. The mediator makes the objects interact without display, so as to make them loosely coupled, and can change their interactions independently.

The mediator model consists of the following parts:

  1. Abstract mediator

    Abstract mediators are abstract classes of mediators that provide abstract methods for executing colleague object information.

  2. Specific intermediary

    The method of abstract mediator is implemented to coordinate the interaction between various colleague roles.

  3. Abstract colleague class

    Abstract colleague class, which saves mediator objects and provides abstract methods for colleague object interaction.

  4. Specific colleagues

    The implementer of the abstract colleague class. If you need to interact with other colleague objects, the mediator object is responsible for the interaction.

code implementation

Mentioned abovepurchasesalestockExample mediator implementation code,See GitHub for detailed code
Do not use the mediator pattern to implement the code,See GitHub for detailed code

The following code is implemented as general code:

Abstract mediator:

/**
 *Abstract mediation
 *
 * Interface AbstractMediator
 * @package DesignPatterns\AbstractMediator
 */
abstract class AbstractMediator
{
    /**
     *Event method to handle the relationship between multiple objects
     *
     *@ param string $classalias class alias
     *@ param string $method method name
     *@ param array $args parameter
     * @return mixed
     */
    abstract public function execute(string $classAlias, string $method, ...$args);

    /**
     *Set colleague class
     *
     *@ param abstractcollague $collague colleague class
     *@ param string $classalias alias alias
     */
    abstract public function setColleague(AbstractColleague $colleague, string $classAlias);
}

Specific Intermediaries:

/**
 *Specific intermediary class
 *
 * Class ConcreteAbstractMediator
 * @package DesignPatterns\Mediator
 */
class ConcreteMediator extends AbstractMediator
{
    /**
     *Event method to handle the relationship between multiple objects
     *
     * @param string $classAlias
     *@ param string $method method name
     * @param array $args
     * @return mixed|void
     */
    public function execute(string $classAlias, string $method, ...$args)
    {
        return $this->{$classAlias}->{$method}(...$args);
    }
}

Abstract colleague class:

/**
 *Abstract colleague
 *
 * Class AbstractColleague
 * @package DesignPatterns\AbstractMediator
 */
abstract class AbstractColleague
{
    /**
     * @var AbstractMediator $mediator
     */
    protected $mediator;

    /**
     *Set mediator
     *
     *@ param abstractmediator $mediator mediator
     */
    public function setMediator(AbstractMediator $mediator)
    {
        $this->mediator = $mediator;
    }
}

Specific colleagues:

/**
 *Specific colleagues
 *
 * Class ConcreteColleague
 * @package DesignPatterns\Mediator
 */
class ConcreteColleague extends AbstractColleague
{
    /**
     * do something
     */
    public function doSomething()
    {
        // TODO
    }
}

This work adoptsCC agreement, reprint must indicate the author and the link to this article

lemon_lyue