- 軟件再工程:優(yōu)化現(xiàn)有軟件系統(tǒng)的方法與最佳實(shí)踐
- (美)Bradley Irby
- 809字
- 2020-11-04 16:44:58
1.4 理解服務(wù)抽象
服務(wù)抽象指的是,對(duì)外部操作來(lái)說(shuō),一個(gè)類(lèi)向外部操作隱藏內(nèi)部工作的程度。應(yīng)該盡可能多地設(shè)置服務(wù)的屬性和方法為私有的,以阻止外部調(diào)用者對(duì)于服務(wù)中任何部分的無(wú)意操作。通過(guò)隱藏這些信息,我們促使應(yīng)用程序更加松耦合。
回顧代碼清單1.6中的例子,思考ShowDatabaseInfo類(lèi)和DisplayForm類(lèi)的交互方式。ShowDatabaseInfo直接給窗體中的標(biāo)簽賦值。
代碼清單1.8 直接給標(biāo)簽賦值
/// <summary> /// Show the information to the user in the given form. /// </summary> public void ShowDataInForm(DisplayForm frm, ADODB.Recordset data) { frm.UserFirstNameLbl.Text = data.Fields[0].ToString(); frm.UserLastNameLbl.Text = data.Fields[0].ToString(); frm.Show(); }
假設(shè)想讓窗體使用另一種不同類(lèi)型的控件(比如列表框)來(lái)顯示信息。窗體的這個(gè)改變并不影響ShowDatabaseInfo類(lèi)的邏輯,但是由于缺乏窗體的抽象,所以我們還是需要在該類(lèi)中更新代碼。
在不引入緊耦合的情況下同時(shí)允許這兩個(gè)類(lèi)進(jìn)行交互,這意味我們必須為這個(gè)窗體創(chuàng)建一個(gè)抽象,這樣的話,窗體就可以根據(jù)需求來(lái)改變,而依賴于該窗體的其他類(lèi)則無(wú)須做相應(yīng)改動(dòng)。服務(wù)抽象的理念使得類(lèi)能夠按需完善。請(qǐng)閱讀以下實(shí)現(xiàn)相同目的的抽象代碼。
代碼清單1.9 緊耦合類(lèi)的抽象版本
/// <summary> /// This class will retrieve a string from the database and /// display it on the screen. /// </summary> public class ShowDatabaseInfoAbstracted { /// <summary> /// Main driver method /// </summary> public void GetAndShowData(int recordId) { ADODB.Recordset data = GetData(); DisplayFormAbstracted frm = GetForm(); ShowDataInForm(frm, data); } /// <summary> /// Open an ADO connection to Microsoft SQL Server, /// get the data, and return it. /// </summary> public ADODB.Recordset GetData() { var connection = new ADODB.Connection(); connection.ConnectionString = "my connection string"; connection.Open(); var rs = new ADODB.Recordset(); //The following query is specific to Microsoft SQL Server. //It will not execute properly on Oracle. rs.Open("Microsoft SQL Specific Query", connection); rs.ActiveConnection = null; return rs; } /// <summary> /// Get the proper form to use for our display. /// </summary> private DisplayFormAbstracted GetForm() { return new DisplayFormAbstracted(); } /// <summary> /// Show the information to the user in the given form. /// </summary> public void ShowDataInForm(DisplayFormAbstracted frm, ADODB.Recordset data) { frm.UserFirstName = data.Fields[0].ToString(); frm.UserLastName = data.Fields[0].ToString(); frm.Show(); } } /// <summary> /// Form that displays the information to the user. /// </summary> public class DisplayFormAbstracted : Form { /// <summary> /// This is private so it cannot be used by an outside /// consumer, thus improving the abstraction. /// </summary> private Label _userFirstNameLbl; private Label _userLastNameLbl; /// <summary> /// These getters and setters can change as the /// display requirements change, but the calling /// code will be unaffected. /// </summary> public string UserFirstName { get { return _userFirstNameLbl.Text; } set { _userFirstNameLbl.Text = value; } } /// <summary> /// These getters and setters can change as the /// display requirements change, but the calling /// code will be unaffected. /// </summary> public string UserLastName { get { return _userLastNameLbl.Text; } set { _userLastNameLbl.Text = value; } } /// <summary> /// Constructor that creates the data display form. /// </summary> public DisplayFormAbstracted() { _userFirstNameLbl = new Label(); Controls.Add(_userFirstNameLbl); _userLastNameLbl = new Label(); Controls.Add(_userLastNameLbl); } }
這段代碼清單1.9和代碼清單1.6的不同之處在于DisplayFormAbstracted的定義。現(xiàn)在,它暴露的僅僅是用戶的名和姓的字符串屬性,而不是暴露整個(gè)標(biāo)簽。這有助于抽象化,因?yàn)榇绑w使用者不需要了解數(shù)據(jù)是如何顯示的。DisplayFormAbstracted可以變?yōu)槲谋究蚧蛘咂渌问剑襍howDatabaseInfoAbstracted類(lèi)無(wú)須改變。
- Getting Started with Citrix XenApp? 7.6
- 從零開(kāi)始構(gòu)建企業(yè)級(jí)RAG系統(tǒng)
- Learning Spring 5.0
- C++ Builder 6.0下OpenGL編程技術(shù)
- Access 2010數(shù)據(jù)庫(kù)基礎(chǔ)與應(yīng)用項(xiàng)目式教程(第3版)
- 游戲程序設(shè)計(jì)教程
- 正則表達(dá)式經(jīng)典實(shí)例(第2版)
- 零基礎(chǔ)學(xué)單片機(jī)C語(yǔ)言程序設(shè)計(jì)
- 運(yùn)用后端技術(shù)處理業(yè)務(wù)邏輯(藍(lán)橋杯軟件大賽培訓(xùn)教材-Java方向)
- Orleans:構(gòu)建高性能分布式Actor服務(wù)
- Python編程快速上手2
- UI動(dòng)效設(shè)計(jì)從入門(mén)到精通
- 零基礎(chǔ)學(xué)Java(第5版)
- HTML5程序開(kāi)發(fā)范例寶典
- jQuery Essentials