In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what are the design patterns commonly used in php". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Singleton model
1, meaning
There is only one instance of a class, and an instance of that class is automatically instantiated and provided to the entire system.
That is, only one instance of this class exists in the application.
Commonly used in database operations, thread pools, caches, printers.
2, characteristics
There is only one instance, and there must be a constructor marked as private
Provide other members with static methods to access this instance
You must create this instance yourself and have a static member variable that holds the instance of the class.
3, code
Class SingleCase {private static $_ instance; private function _ construct () {echo'i has been';} public static function getInstance () {if (! isset (self::$_instance)) {self::$_instance = new self ();} return self::$_instance;} private function _ clone () {}}
Remarks: it is difficult to debug and hides dependencies.
Second, factory model
1, meaning
The factory pattern is a class that contains methods whose function or function is to instantiate objects.
The factory pattern is usually used to return different classes that match similar interfaces
2, characteristics
Factory patterns generally have a static method named Factory
This static method takes a parameter
This static method returns different object instances based on the parameters.
3, code
Class Factory {static public function getUser ($type) {if ($type = = 1) {return new A ();} return new B ();}} interface FetchName {public function getName ()} class An implements FetchName {public function getName ()} class B implements FetchName {public function getName ()} / use $user = Factory::getUser (1); $user- > getName ()
Note: what's the use?
If a system, new User () is used in many places. One day new User ('A'); adds a parameter that needs to be changed everywhere.
If a system uses the factory pattern User::getObj ('1'); if parameter 1 returns the object instance new User (); one day new User () adds a parameter, I just need to change the class instance in the factory.
III. Observer model
1, meaning
The Observer pattern provides you with another way to avoid tight coupling between components
It is divided into subject and observer, and both need to implement interfaces and methods.
The topic registers the observer and sets a method that calls the observer's method to notify the observer
2, characteristics
Programming for interfaces, not for implementation
The observer pattern defines one-to-many dependencies of objects
When an object changes its state, all its dependents are notified and updated automatically
3, code
Class Paper {private $myObj = []; public function register ($sub) {$this- > myObj [] = $sub;} public function setMessage () {if ($this- > myObj) {foreach ($this- > myObj as $obj) {$obj- > update ();}} interface Observerable {public function update () } class Subscriber implements Observerable {public function update () {echo "Callback\ n";}}
Note:
$paper = new Paper (); $paper- > register (new Subscriber ()); / / $paper- > register (new Subscriber1 ()); $paper- > setMessage ()
Call the setMessage () method as long as any class is registered. Other registered classes are notified by the update () method.
This is the end of the content of "what are the common design patterns of php". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.