search
HomeBackend DevelopmentPHP TutorialPHP Talk 'Refactoring - Improving the Design of Existing Code' Part 2 Moving Features Between Objects_PHP Tutorial

思维导图

索引:

Ø Move Method(搬移函数)
Ø Move Field (搬移值域)
Ø Extract Class (提炼类)
Ø Inline Class (将类内联化,就是把当前的类合并到其他类中)
Ø Hide Delegate (隐藏委托关系)
Ø Remove Middle Man ( 移除中间人)
Ø Introduce Foreign Method (引入外加函数)
Ø Introduce Local Extension (引入本地扩展)
 
 介绍
 
 承接上文PHP 杂谈《重构-改善既有代码的设计》之 重新组织你的函数 ,继续说重构方面的内容。
 

 专业术语
 
delegate:委托
encapsulate:封装
introduce:引入
wrapper:覆盖
 
 前言
 
”决定把责任放在哪里“——运用重构改变原先的设计。
 

解释:

  1、Class承担过多而臃肿不堪——Extract Class将一部分责任分离出去。

  2、Class没有承担足够多的责任,不再有单独存在的理由——Inline Class将它融入另一个Class。

  3、Class使用另一个Class——Hide Delegate隐藏关系。

  4、承接(3),如果Client通过Middle Man 调用很多的Delegate Class的函数(这里只是简单调用,只做跳转,而Middle Man没有做太多的业务逻辑,如10个Delegate Class中的Method对应10个Middle Man的Method)——Remove Middle Man,直接使用Delegate Class,可以部分使用Delegate Method。

 

 Move Method
 
如果一个类中的方法与另一个类有很多的交流,那么我们就在另一个类中建立一个有类似功能的新函数,将旧函数变成一个单纯的Delegating Method, 或是将旧函数移除。

 

类图:

 

动机:

  1、如果一个类与另一个类有高度耦合,我就会Move Method。——class更简单,更干净利落的实现系统交付的任务。

  2、移动一些值域,就要检查是否使用另一个类的次数必使用所驻对象的次数还多。

 Move Field
 
状况:你的class中的field被另一个class更多的用到。那么在另一个class里建立new field,修改旧的field。
 

 
 Extract Class
 
状况:一个类做了两个类做的事,那么建立一个新Class,将相关的Field和Method从旧Class移到新Class。

 

 

 Inline Class
 
状况:你的某个Class没有做太多事情(没有承担足够责任),那么将Class的所有特性搬移到另一个Class中,然后移除原Class。
动机:Inline Class与Extract Class相反。——把Extract Class例子反过去,因为PhoneNumber只用作读取code和number。
 
 Hide Delegate
 
状况:客户直接调用Server Object的Delegate Class的Method,那么在Server端建立客户所需的函数Method,用以隐藏委托关系。
 
 学过对象技术的人都知道,虽然php允许你将field声明为public,但你还应该隐藏field(private)。随着经验日渐丰富,有更多值得封装的东西。
 
看下面一个例子:

 
 $person->getDepartment()->getManager()明显揭露了,要想找到Xiaocai的领导,必须要经过department,所以我们要做的事隐藏department。——可以减少耦合性。
 

 

 Remove Middle Man
 
状况:如果某个Class做了过多的Simple Delegate,那么我们就直接调用Delegate Class。
动机:在Hide Delegate中的例子里当Department有更多新方法的时候,我们为了Hide Delegate,就要必须在Person里添加相应的方法做Delegate之用。这时候的Person完全变成了一个Middle Man,此时我们就应该直接调用Delegate Class——Department。
 重构的意义就在于:你永远不必说对不起,只要你把出问题的地方修补好就行了。
 
 
 

 

 Introduce Foreign Method
 
状况:有一个类Client需要使用的类PreviousEnd中一个额外函数,但你无法修改这个类PreviousEnd,那么你就在Client中建立一个函数,并以一个PreviousEnd实体做为参数。

 

 Introduce Local Extention
 
状况:你的Class需要一些额外函数,但你不能修改当前的类,那么建立一个新Class,使它包含这些函数。使用Subclass 或 Wrapper。——这个一般用于你无法修改源码的情况下使用。
 
 拿上面Introduce Foreign Method例子来说

使用Subclass方法
 

或使用Wrapper

 总结

 
需要注意一下,“Extract Class”和“Inline Class”,”Hide Delegate“和”Remove Middle Man",都是相反的过程,具体理解,可以看前言中的那张流程图。
 
“Hide Delegate"我们常用于使用少量的”Delegate Method“的时候,而”Remove Middle Man“,用于调用很多”Delegate Method“的时候,我们可以直接使用Delegate Class,进行调用,而有的Delegate Method我们视情况保留一部分。
 
"Extract Class" and "Inline Class", "Extract Class" is often used in a Class that takes on too many responsibilities and becomes bloated, while "Inline Class" is often used in the current class" which is too irresponsible " when used. ——I personally would rather "Extract Class" than "Inline Class".

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/325376.htmlTechArticleMind map index: Move Method (move function) Move Field (move value field) Extract Class (extract class) Inline Class (to inline a class is to merge the current class into other...
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
PHP Email: Step-by-Step Sending GuidePHP Email: Step-by-Step Sending GuideMay 09, 2025 am 12:14 AM

PHPisusedforsendingemailsduetoitsintegrationwithservermailservicesandexternalSMTPproviders,automatingnotificationsandmarketingcampaigns.1)SetupyourPHPenvironmentwithawebserverandPHP,ensuringthemailfunctionisenabled.2)UseabasicscriptwithPHP'smailfunct

How to Send Email via PHP: Examples & CodeHow to Send Email via PHP: Examples & CodeMay 09, 2025 am 12:13 AM

The best way to send emails is to use the PHPMailer library. 1) Using the mail() function is simple but unreliable, which may cause emails to enter spam or cannot be delivered. 2) PHPMailer provides better control and reliability, and supports HTML mail, attachments and SMTP authentication. 3) Make sure SMTP settings are configured correctly and encryption (such as STARTTLS or SSL/TLS) is used to enhance security. 4) For large amounts of emails, consider using a mail queue system to optimize performance.

Advanced PHP Email: Custom Headers & FeaturesAdvanced PHP Email: Custom Headers & FeaturesMay 09, 2025 am 12:13 AM

CustomheadersandadvancedfeaturesinPHPemailenhancefunctionalityandreliability.1)Customheadersaddmetadatafortrackingandcategorization.2)HTMLemailsallowformattingandinteractivity.3)AttachmentscanbesentusinglibrarieslikePHPMailer.4)SMTPauthenticationimpr

Guide to Sending Emails with PHP & SMTPGuide to Sending Emails with PHP & SMTPMay 09, 2025 am 12:06 AM

Sending mail using PHP and SMTP can be achieved through the PHPMailer library. 1) Install and configure PHPMailer, 2) Set SMTP server details, 3) Define the email content, 4) Send emails and handle errors. Use this method to ensure the reliability and security of emails.

What is the best way to send an email using PHP?What is the best way to send an email using PHP?May 08, 2025 am 12:21 AM

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

Best Practices for Dependency Injection in PHPBest Practices for Dependency Injection in PHPMay 08, 2025 am 12:21 AM

The reason for using Dependency Injection (DI) is that it promotes loose coupling, testability, and maintainability of the code. 1) Use constructor to inject dependencies, 2) Avoid using service locators, 3) Use dependency injection containers to manage dependencies, 4) Improve testability through injecting dependencies, 5) Avoid over-injection dependencies, 6) Consider the impact of DI on performance.

PHP performance tuning tips and tricksPHP performance tuning tips and tricksMay 08, 2025 am 12:20 AM

PHPperformancetuningiscrucialbecauseitenhancesspeedandefficiency,whicharevitalforwebapplications.1)CachingwithAPCureducesdatabaseloadandimprovesresponsetimes.2)Optimizingdatabasequeriesbyselectingnecessarycolumnsandusingindexingspeedsupdataretrieval.

PHP Email Security: Best Practices for Sending EmailsPHP Email Security: Best Practices for Sending EmailsMay 08, 2025 am 12:16 AM

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment