Home Backend Development PHP Tutorial PHP Talk 'Refactoring - Improving the Design of Existing Code' Part 2 Moving Features Between Objects_PHP Tutorial

PHP Talk 'Refactoring - Improving the Design of Existing Code' Part 2 Moving Features Between Objects_PHP Tutorial

Jul 21, 2016 pm 03:19 PM
method move php object thinking characteristic index

思维导图

索引:

Ø 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 of this Website
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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP Working with Database CakePHP Working with Database Sep 10, 2024 pm 05:25 PM

Working with database in CakePHP is very easy. We will understand the CRUD (Create, Read, Update, Delete) operations in this chapter.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

CakePHP Logging CakePHP Logging Sep 10, 2024 pm 05:26 PM

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

See all articles