Home  >  Article  >  Backend Development  >  PHP object-oriented advanced design patterns: Appearance pattern usage examples

PHP object-oriented advanced design patterns: Appearance pattern usage examples

巴扎黑
巴扎黑Original
2017-05-22 14:06:401776browse

What is appearance mode?

The facade design pattern hides the complexity from the calling object by creating a facade interface in front of a collection of necessary logic and methods.

Why use the Facade pattern:

The reason for using objects based on the Facade pattern is to interface with third-party solutions. It is important to remember that we constantly emphasize that object-oriented projects should know a collection of associated objects. Given this architecture, the lead programmer may find it wiser to use third-party objects.

Suppose you want to provide a search web page for an application. The page first finds all data that matches the search term itself. If the number of results is less than 10, then a third-party service is called to retrieve additional results, which are added to the end of the results found internally by the application. The Search appearance object returns the results to the calling view object. Internally, the Search facade object calls methods that query the internal database. Then, it determines whether it needs to call Google through a web service. If necessary, all results are also analyzed to return a homogeneous result set.

In order to hide the complex methods and logical groups required to perform a certain step of the business process, classes based on the appearance design pattern should be used.

UML

This UML diagram details a class design using the facade design pattern.

PHP object-oriented advanced design patterns: Appearance pattern usage examples

The following is a description of the above picture:

1.MyObject class contains a public method named doSomethingRequiresAandB(), This method is just one step in the execution of the MyObject class. The doSomethingRequiresAandB() method creates a new instance of the object LogicFacade, which calls a public method named callRequiredLogic() that is abstract enough for MyObject.

2. The callRequiredLogic() method inside the LogicFacade class is then responsible for creating an instance of LogicObjectA and calling the doSomethingA() method. It is also responsible for creating an instance of LogicObjectB and calling the doSomethingB() method.

3. All the above actions are passed back through the LogicFacade class so that they can be used by MyObject.


Use example code to show:

header("Content-type:text/html;Charset=utf-8");

//子系统角色
class SubSystemOne{
   function mothedOne(){
       echo "方法一<br>";
   }
}
class SubSystemTwo{
   function mothedTwo(){
       echo "方法二<br>";
   }
}
class SubSystemThree{
   function mothedThree(){
       echo "方法三<br>";
   }
}

//外观角色
class Facade{
    private $subSystemOne = null;
    private $subSystemTwo = null;
    private $subSystemThree =null;

    function __construct(){
        $this->subSystemOne =new SubSystemOne();
        $this->subSystemTwo =new SubSystemTwo();
        $this->subSystemThree =new SubSystemThree();
    }

    function mothedA(){
        $this->subSystemOne->mothedOne();
    }
    function mothedB(){
        $this->subSystemTwo->mothedTwo();
    }
    function mothedC(){
        $this->subSystemThree->mothedThree();
    }
    function mothedD(){
        $this->subSystemOne->mothedOne();
        $this->subSystemTwo->mothedTwo();
        $this->subSystemThree->mothedThree();
    }
}

//测试
$facade = new Facade();
$facade->mothedA();
$facade->mothedB();
$facade->mothedC();
$facade->mothedD();


The above is the detailed content of PHP object-oriented advanced design patterns: Appearance pattern usage examples. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn