Design Patterns in PHP - Facade Pattern_PHP Tutorial
One of the design patterns in php - facade pattern
<?php /** (1)外观模式(Facade)也叫门面模式,为子系统中的一组接口提供一个一致的界面,此模式定义了一个高层接口,这个接口使得这一子系统更加容易使用。 为一些复杂的子系统提供一组接口 (2)主要角色 门面(Facade)角色 ? 此角色将被客户端调用 ? 知道哪些子系统负责处理请求 ? 将用户的请求指派给适当的子系统 子系统(subsystem)角色 ? 实现子系统的功能 ? 处理由Facade对象指派的任务 ? 没有Facade的相关信息,可以被客户端直接调用 ? 可以同时有一个或多个子系统,每个子系统都不是一个单独的类,而一个类的集合。每个子系统都可以被客户端直接调用,或者被门面角色调 (3)使用情况 : 首先,在设计初期阶段,应该要有意识将不同的两个层分离,比如经典的三层架构,就需要考虑在数据访问层和业务逻辑层、 业务逻辑层和表示层的层与层之间建立外观Facade,这样可以为复杂的子系统提供一个简单的接口,使得耦合度大大降低。 其次,在开发阶段,子系统往往因为不断的重构演化而变得越来越复杂,大多数的模式使用时也都会产生很多很小的类,这本是好事, 但也给外部调用它们的用户程序带来了使用上的困难,增加外观Facade可以提供一个简单的接口,减少它们之间的依赖。 第三,在维护一个遗留的大型系统时,可能这个系统已经非常难以维护和扩展了,但因为它包含非常重要的功能,新的需求开发必须要依赖于它。 此时,用外观模式Facade也是非常合适的。你可以为新系统开发一个外观Facade类,来提供设计粗糙或高度复杂的遗留代码的比较清晰的简单接口, 让新系统与Facade对象交互,Facade与遗留代码交互所有复杂的工作。” (4) 具体事例 比如我关机,整个过程包括关闭显示器,关闭pc机器,关闭电源 ,关机的按钮就是门面的角色,其他为子系统 */ // 门面抽象接口 interface Facade{ public function turnOn() ; public function turnOff() ; } // (1) 关闭显示器 class PcLight { public function turnOn() {} public function turnOff() { echo 'turn off PcLight <br>' ; } } //(2) pc 机器 class Pcmachine { public function turnOn() {} public function turnOff() { echo 'turn off PcMathion <br>' ; } } // (3) 关闭电源 class Power { public function turnOn() {} public function turnOff() { echo 'turn off Power <br>' ; } } // 关机的门面角色 class PcFacade implements Facade{ private $PcLight ; private $Pcmachine ; private $Power ; public function __construct(){ $this->PcLight = new PcLight(); $this->Pcmachine = new Pcmachine(); $this->Power = new Power(); } // 门面角色的应用 public function turnOff() { $this->PcLight ->turnOff(); $this->Pcmachine ->turnOff(); $this->Power ->turnOff(); } public function turnOn() {} } // 应用 $button = new PcFacade(); $button ->turnOff(); /* 其实门面模式就是把几个子系统(实例或者类.统一一个统一的接口进行执行,客户端不用关注子系统,只用门面即可 )

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

In the Java framework, the difference between design patterns and architectural patterns is that design patterns define abstract solutions to common problems in software design, focusing on the interaction between classes and objects, such as factory patterns. Architectural patterns define the relationship between system structures and modules, focusing on the organization and interaction of system components, such as layered architecture.

The decorator pattern is a structural design pattern that allows dynamic addition of object functionality without modifying the original class. It is implemented through the collaboration of abstract components, concrete components, abstract decorators and concrete decorators, and can flexibly expand class functions to meet changing needs. In this example, milk and mocha decorators are added to Espresso for a total price of $2.29, demonstrating the power of the decorator pattern in dynamically modifying the behavior of objects.

1. Factory pattern: Separate object creation and business logic, and create objects of specified types through factory classes. 2. Observer pattern: allows subject objects to notify observer objects of their state changes, achieving loose coupling and observer pattern.

Design patterns solve code maintenance challenges by providing reusable and extensible solutions: Observer Pattern: Allows objects to subscribe to events and receive notifications when they occur. Factory Pattern: Provides a centralized way to create objects without relying on concrete classes. Singleton pattern: ensures that a class has only one instance, which is used to create globally accessible objects.

The Adapter pattern is a structural design pattern that allows incompatible objects to work together. It converts one interface into another so that the objects can interact smoothly. The object adapter implements the adapter pattern by creating an adapter object containing the adapted object and implementing the target interface. In a practical case, through the adapter mode, the client (such as MediaPlayer) can play advanced format media (such as VLC), although it itself only supports ordinary media formats (such as MP3).

TDD is used to write high-quality PHP code. The steps include: writing test cases, describing the expected functionality and making them fail. Write code so that only the test cases pass without excessive optimization or detailed design. After the test cases pass, optimize and refactor the code to improve readability, maintainability, and scalability.

The Guice framework applies a number of design patterns, including: Singleton pattern: ensuring that a class has only one instance through the @Singleton annotation. Factory method pattern: Create a factory method through the @Provides annotation and obtain the object instance during dependency injection. Strategy mode: Encapsulate the algorithm into different strategy classes and specify the specific strategy through the @Named annotation.

The advantages of using design patterns in Java frameworks include: enhanced code readability, maintainability, and scalability. Disadvantages include complexity, performance overhead, and steep learning curve due to overuse. Practical case: Proxy mode is used to lazy load objects. Use design patterns wisely to take advantage of their advantages and minimize their disadvantages.
