PHP聚合耦合?解決方案
Jun 13, 2016 pm 01:33 PM
PHP聚合耦合???
這是一個(gè)通用模型接口:
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->interface API{ public function main(); public function params($value); }
然后,一般模型類繼承它:
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->class test implements API { public function main(){ echo 'hello world!'; } public function params($value){ var_dump($value); } }
前端控制器,解析了URL后實(shí)例化模型類,調(diào)用接口,傳遞模型數(shù)據(jù):
- PHP code
<!-- Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ -->class Control{ private function __construct(){ self::Router(); $obj = new self::$Model(); self::_Interface($obj); } public static _Interface(API $obj){ $obj -> main(); $obj -> params(); } }
這是我做的簡(jiǎn)單的耦合結(jié)構(gòu),但是,這只是相對(duì)于控制器和模型類之間的耦合,而且是通用類,如果是特殊類還得使用特殊的接口和控制器,但問(wèn)題不在這,問(wèn)題在于我想讓模型之間也通過(guò)接口交互(數(shù)據(jù)耦合?),但是想了很多也沒(méi)有實(shí)際的解決辦法,該怎樣做這個(gè)接口呢???
比如,我需要調(diào)用一個(gè)數(shù)據(jù)庫(kù)類的函數(shù),一般情況下是直接調(diào)用:
? $db = db::main();
? #db -> Query(***********************);
但是,這不符合低耦合概念,我只要一改Query();方法就完蛋了,有什么辦法能拉取數(shù)據(jù)??
或者說(shuō),給個(gè)數(shù)據(jù)耦合的實(shí)例也行,自己慢慢研究算了....
謝謝啦!
(注:我知道,可能會(huì)有人說(shuō)上面的代碼不過(guò)完善什么的?的確,我是把前端控制器的一些東西都集成起來(lái)了,但這不是我今天討論的焦點(diǎn),如果給大家?guī)?lái)了視覺(jué)上的不便,請(qǐng)見(jiàn)諒)
------解決方案--------------------
說(shuō)是實(shí)話非常不明白你到底要做什么。。。
但是數(shù)據(jù)層要交互的話,明顯應(yīng)該通過(guò)控制層來(lái)做,否則要控制層做什么?因?yàn)橹挥锌刂茖又罃?shù)據(jù)之間有什么具體業(yè)務(wù)邏輯上的關(guān)系,而model僅僅應(yīng)該關(guān)心的是自己的一畝三分地,超出這個(gè)范圍之外的都應(yīng)該交給上層處理,這樣才是降低了耦合!
另外提到的$db->query的問(wèn)題,明顯可以通過(guò)抽離字段、條件、排序等,將query轉(zhuǎn)換成
$db->select()->where()->order()->limit()
下面有2種方式:
1 然后接著抽象
$db::fetch($where, $order, $limit) {
?return $db->select('*')->where($where)->order()->limit();
}
調(diào)用的時(shí)候
$db = db::main();
$db->fetch($where, $order, $limit);
2 或者可以封裝一個(gè)底層的model基類
basemodel::fetch($where, $order, $limit) {
$db = db::main();
?return $db->select('*')->where($where)->order()->limit();
}
模型基礎(chǔ)
class test extends basemodel implements API {?
function dothing(){
parent::fetch($where, $order, $limit);
}
}
沒(méi)有明白這個(gè)和數(shù)據(jù)耦合有什么關(guān)系,這僅僅是調(diào)用db的封裝!
------解決方案--------------------
2樓得說(shuō)的有理,我個(gè)人認(rèn)為降低耦合的辦法就是顯示層只負(fù)責(zé)顯示,邏輯處理就交個(gè)業(yè)務(wù)邏輯層,數(shù)據(jù)訪問(wèn)層只管處理數(shù)據(jù),個(gè)做個(gè)的事!
關(guān)于數(shù)據(jù)庫(kù)類得問(wèn)題,我建議你去看看uchome的數(shù)據(jù)庫(kù)類,我覺(jué)得他的類寫(xiě)得還不錯(cuò),跟2樓說(shuō)的比較相似!
------解決方案--------------------
既然是控制器的基類。控制器不都可以遵循基類提供的標(biāo)準(zhǔn)執(zhí)行數(shù)據(jù)?
------解決方案--------------------
我以為你應(yīng)經(jīng)被各種名詞帶入到一個(gè)誤區(qū)了
你實(shí)際上是想在各個(gè)功能模塊間傳遞某中信息
其實(shí)這是很容易實(shí)現(xiàn)的一種機(jī)制,就是操作系統(tǒng)中廣泛使用的事件驅(qū)動(dòng)或消息驅(qū)動(dòng)
一個(gè)模塊將自己的需求廣播出去,能夠?qū)崿F(xiàn)此需求的模塊就此需求返回需要的數(shù)據(jù)

Hot AI Tools

Undress AI Tool
Undress images for free

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

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

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

Many users will choose the Huawei brand when choosing smart watches. Among them, Huawei GT3pro and GT4 are very popular choices. Many users are curious about the difference between Huawei GT3pro and GT4. Let’s introduce the two to you. . What are the differences between Huawei GT3pro and GT4? 1. Appearance GT4: 46mm and 41mm, the material is glass mirror + stainless steel body + high-resolution fiber back shell. GT3pro: 46.6mm and 42.9mm, the material is sapphire glass + titanium body/ceramic body + ceramic back shell 2. Healthy GT4: Using the latest Huawei Truseen5.5+ algorithm, the results will be more accurate. GT3pro: Added ECG electrocardiogram and blood vessel and safety

"Detailed explanation of how to use take and limit in Laravel" In Laravel, take and limit are two commonly used methods, used to limit the number of records returned in database queries. Although their functions are similar, there are some subtle differences in specific usage scenarios. This article will analyze the usage of these two methods in detail and provide specific code examples. 1. Take method In Laravel, the take method is used to limit the number of records returned, usually combined with the orderBy method.

Function means function. It is a reusable code block with specific functions. It is one of the basic components of a program. It can accept input parameters, perform specific operations, and return results. Its purpose is to encapsulate a reusable block of code. code to improve code reusability and maintainability.

Why Snipping Tool Not Working on Windows 11 Understanding the root cause of the problem can help find the right solution. Here are the top reasons why the Snipping Tool might not be working properly: Focus Assistant is On: This prevents the Snipping Tool from opening. Corrupted application: If the snipping tool crashes on launch, it might be corrupted. Outdated graphics drivers: Incompatible drivers may interfere with the snipping tool. Interference from other applications: Other running applications may conflict with the Snipping Tool. Certificate has expired: An error during the upgrade process may cause this issu simple solution. These are suitable for most users and do not require any special technical knowledge. 1. Update Windows and Microsoft Store apps

Practical Guide to Where Method in Laravel Collections During the development of the Laravel framework, collections are a very useful data structure that provide rich methods to manipulate data. Among them, the Where method is a commonly used filtering method that can filter elements in a collection based on specified conditions. This article will introduce the use of the Where method in Laravel collections and demonstrate its usage through specific code examples. 1. Basic usage of Where method

In this article, we will learn about enumerate() function and the purpose of “enumerate()” function in Python. What is the enumerate() function? Python's enumerate() function accepts a data collection as a parameter and returns an enumeration object. Enumeration objects are returned as key-value pairs. The key is the index corresponding to each item, and the value is the items. Syntax enumerate(iterable,start) Parameters iterable - The passed in data collection can be returned as an enumeration object, called iterablestart - As the name suggests, the starting index of the enumeration object is defined by start. if we ignore

Detailed explanation of the role and function of the MySQL.proc table. MySQL is a popular relational database management system. When developers use MySQL, they often involve the creation and management of stored procedures (StoredProcedure). The MySQL.proc table is a very important system table. It stores information related to all stored procedures in the database, including the name, definition, parameters, etc. of the stored procedures. In this article, we will explain in detail the role and functionality of the MySQL.proc table

Part 1: Initial Troubleshooting Steps Checking Apple’s System Status: Before delving into complex solutions, let’s start with the basics. The problem may not lie with your device; Apple's servers may be down. Visit Apple's System Status page to see if the AppStore is working properly. If there's a problem, all you can do is wait for Apple to fix it. Check your internet connection: Make sure you have a stable internet connection as the "Unable to connect to AppStore" issue can sometimes be attributed to a poor connection. Try switching between Wi-Fi and mobile data or resetting network settings (General > Reset > Reset Network Settings > Settings). Update your iOS version:
