如下伪代码,
AService do2对P进行了操作,然后碰到的问题是PService里对P进行操作,如果调用了AService do2那么
由于不是对P最新的引用,save时会把AService do2的修改覆盖掉。
//Update 2015年05月22日17:24:28
@Ke_Wu 这不应该是逻辑问题,事实上,我作为后来的调用者没必要也不可能知道AService::do2里的具体实现,但现在碰到问题了,那么就是设计的问题了
<code>class AService { function do2(pid) { ... p = P.getById(pid); p.s = 'zz'; p.save(); ... } } class PService { function do1(pid) { ... p = P.getById(pid); p.s = 'yy'; AService.do2(pid); ... p.a = 'a';p.b = 'b'; ... p.save();//p.s 仍旧是yy, zz被yy覆盖 ... } } class CService { function do4(cid) { ... c = C.getById(cid); pid = c.pid; AService.do2(pid); ... } } </code>
回复内容:
如下伪代码,
AService do2对P进行了操作,然后碰到的问题是PService里对P进行操作,如果调用了AService do2那么
由于不是对P最新的引用,save时会把AService do2的修改覆盖掉。
//Update 2015年05月22日17:24:28
@Ke_Wu 这不应该是逻辑问题,事实上,我作为后来的调用者没必要也不可能知道AService::do2里的具体实现,但现在碰到问题了,那么就是设计的问题了
<code>class AService { function do2(pid) { ... p = P.getById(pid); p.s = 'zz'; p.save(); ... } } class PService { function do1(pid) { ... p = P.getById(pid); p.s = 'yy'; AService.do2(pid); ... p.a = 'a';p.b = 'b'; ... p.save();//p.s 仍旧是yy, zz被yy覆盖 ... } } class CService { function do4(cid) { ... c = C.getById(cid); pid = c.pid; AService.do2(pid); ... } } </code>
简化下来其实问题就是:
<code>php</code><code>p1 = P.getById(pid); p1.s = 'yy'; ... p2 = P.getById(pid); p2.s = 'zz'; p2.save(); ... p1.save(); </code>
保存了p2的修改(可能是存到数据库),并不意味着内存里的p1随之更新,除非你重新get一遍p1。
重构的目的是用来改善正确工作代码的风格和设计。
这段代码的问题是逻辑错误,对它而言谈重构还为时过早。
你的问题的本质,是两个“主语”(只是在你的案例中恰好都是service而已)的各自一个“行为”(do1 和 do2)含有了完全相同的一个“行动效果”(修改p.s的值)。
冲突不在于service,而在于行动效果冗余。
试想一下,换一个案例,其中只有一个主语,两个行为(do1 和 do2)都是它的,那么问题也是等价的。
两个行为有重叠的行动效果,实在太常见的了。
关键在于,你怎样界定,哪种重叠是满足需求的?哪种是错误、不合理的?
举一个满足需求的例子:
需求是:p是一个鼠标悬停的tips(界面组件)。先根据鼠标坐标,赋值p.top为一个值。随后,计算tips是否超出了窗口边缘。如果是,则计算tips的top的最大值(因为窗口大小可能会被改变,所以需要计算),然后赋值p.top为该最大值。p.left同理。
这是我做网页前端开发时遇到过的需求。
你的解决办法,大概可以解决你的那一个具体案例,但换成别的情况可能就又不对症了。
在我看来,关键在于,一个行为的源头(往往是事件)所导致一连串行动效果,其中要避免出现重叠;除非需求要求必要的重叠。
这“一连串”的“串法”,是设计上要想清楚的。你已经在朝这个方向努力了,只是关注点稍有偏离。
至于串的过程中的对象(主语/宾语)是不是service、是何种service,倒是没有关系。
我的解决办法如下,有什么缺点请指教:
Service应该分为2种:1,名词Service; 2, 行为Service
如:UserService 与 RegisterService
对于【名词Service】其里面每个method都必须返回相应的对象,如UserService下的upgrade(uid)就必须返回被升级后的user对象。
对于【行为Service】只对外暴露出一个execute(data),excute(data)必须返回行为成功与否的状态以及被施加这个行为的对象,如RegisterService下的excute(data)就必须返回注册成功与否,以及如果成功了它影响的对象。
通常我们约定对外只调用【行为Service】,再在【行为Service】里调用多个【名词Service】和其他【行为Service】,如在RegisterService::execute(data)里调用UserService::create(), UserService::markNewbee(uid),SendEmailService::execut()等;
【名词Service】中不允许调用【行为Service】。
所有Service的每个method的入参都可以是id或者对象实例,如upgrade()可以接受uid也可以接受user作为入参。
回到我的提问,可以这么写
<code>class AService { function get(aid_or_object) { if (aid_or_object instanceOf A) { return aid_or_object; } return A.getById(aid); } } class PService { function get(pid_or_object) { if (pid_or_object instanceOf P) { return pid_or_object; } return P.getById(pid); } } class Do2Service { function execute(aid_or_object, pid_or_object = null) { a = AService.get(aid_or_object); if (pid_or_object instanceOf P) { p = pid_or_object } else { p = PService.get(a.pid); } p.s = 'zz'; p.save(); a.save(); return [:success, a, p]; } } class Do3Service { function execute(pid_or_object) { p = PService.get(pid_or_object); p.s = 'cc'; p.save(); return [:success, p]; } } class Do1Service { function execute(pid_or_object) { p = PService.get(pid_or_object); p.s = 'yy' if condition1 result, a, p = Do2Servce.execute(p.aid, p) if condition2 result, p = Do3Servce.execute(p) if condition3 p.a = 'a'; p.b = 'b'; p.save() return [:success, p, a]; } } </code>
想要解决什么问题?

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


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

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download
The most popular open source editor

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

SublimeText3 English version
Recommended: Win version, supports code prompts!
