PHP Object-oriented Advanced design pattern: example of use of appearance patterns

Source: Internet
Author: User
What is the appearance mode?

By creating a façade interface before the set of required logic and methods, the design pattern hides the complexity from the calling object.

Why Use Design Patterns:

The reason for using an object based on the design pattern is the interface third-party solution. It is important to remember that we are constantly emphasizing that object-oriented projects should be a collection of knowledge-related objects. Given this architecture, the chief programmer may find it more sensible to use third-party objects.

Suppose you want to provide a search Web page for an application. The page first finds all the data that matches the search item. If the number of results is less than 10, the third-party service is called to retrieve additional results, which are added to the end of the results that the application discovers internally. The search skin object returns the result to the calling view object. Internally, the search skin object invokes methods that query the internal database. It then determines whether you need to call Google through a Web service. If necessary, all results are also parsed to return a homogeneous result set.

To hide complex methods and logical groups that are required to perform a step in a business process, you should use a class that is based on the design pattern.

Uml

This UML diagram details a class design that uses a design pattern.

Here is a description of the pair:

The 1.MyObject class contains a public method named Dosomethingrequiresaandb (), which is just a step in the MyObject class execution. The Dosomethingrequiresaandb () method creates a new instance of the object Logicfacade that calls a public method that is sufficiently abstract for MyObject, called Callrequiredlogic ().

The Callrequiredlogic () method inside the 2.LogicFacade class is then responsible for creating an instance of the Logicobjecta and calling the Dosomethinga () method. It is also responsible for creating an instance of LOGICOBJECTB and calling the DOSOMETHINGB () method.

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


Use the instance code to show:

Header ("content-type:text/html;   Charset=utf-8 ");//Subsystem Role class subsystemone{function Mothedone () {echo" method one <br> ";   }}class subsystemtwo{function Mothedtwo () {echo "method two <br>";   }}class subsystemthree{function Mothedthree () {echo "Method III <br>";    }}//appearance role 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 (); }}//TestTrial $facade = new facade (); $facade->motheda (); $facade->mothedb (); $facade->mothedc (); $facade->mothedd (); 

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.