freepeople性欧美熟妇, 色戒完整版无删减158分钟hd, 无码精品国产vα在线观看DVD, 丰满少妇伦精品无码专区在线观看,艾栗栗与纹身男宾馆3p50分钟,国产AV片在线观看,黑人与美女高潮,18岁女RAPPERDISSSUBS,国产手机在机看影片

正文內(nèi)容

架構(gòu)師培訓講義6-類結(jié)構(gòu)設計(編輯修改稿)

2025-07-24 05:05 本頁面
 

【文章內(nèi)容簡介】 } }實現(xiàn):Payment o。privatevoid button1_Click(object sender, EventArgs e) { o = newCashPayment1()。 = ()。 = ()。}加入繼承:publicclassCashPayment1 : CashPayment {publicoverridestring Action() {//在執(zhí)行原來的代碼之前,彈出提示框 (現(xiàn)金支付)。()。 } }實現(xiàn):Payment o。privatevoid button1_Click(object sender, EventArgs e) { o = newCashPayment1()。 = ()。 = ()。}缺點:繼承層次多一層,提升了耦合性。當實現(xiàn)類比較多的時候,實現(xiàn)起來就比較復雜。在這樣的情況下,也可以使用裝飾器模式,這是用組合取代繼承的一個很好的方式。意圖事實上,上面所要解決的意圖可以歸結(jié)為“在不改變對象的前提下,動態(tài)增加它的功能”,也就是說,我們不希望改變原有的類,或者采用創(chuàng)建子類的方式來增加功能,在這種情況下,可以采用裝飾模式。結(jié)構(gòu)裝飾器結(jié)構(gòu)的一個重要的特點是,它繼承于一個抽象類,但它又使用這個抽象類的聚合(即即裝飾類對象可以包含抽象類對象),恰當?shù)脑O計,可以達到我們提出來的目的。模式中的參與者如下:Component (組成):定義一個對象接口,可以動態(tài)添加這些對象的功能,其中包括Operation(業(yè)務)方法。ConcreteComponent(具體組成):定義一個對象,可以為它添加一些功能。Decorator(裝飾):維持一個對Component對象的引用,并定義與Component接口一致的接口。ConcreteDecorator(具體裝飾):為組件添加功能。它可能包括AddedBehavior(更多的行為)和AddedState(更多的狀態(tài))。舉個例子:假定我們已經(jīng)構(gòu)造了一個基于支付的簡單工廠模式的系統(tǒng)。using System。using 。using 。namespace PaymentDemo{publicabstractclassPayment {privatedecimal amount。publicdecimal Amount {get {return amount。 }set { amount = value。 } }publicstring goSale() {return Action() + 完成,金額為 + amount + , 正在查詢庫存狀態(tài)。 }publicabstractstring Action()。 }publicclassCashPayment : Payment {publicoverridestring Action() {return現(xiàn)金支付。 } }publicclassCreditPayment : Payment {publicoverridestring Action() {return信用卡支付。 } }publicclassCheckPayment : Payment {publicoverridestring Action() {return支票支付。 } }}工廠類,注意這是獨立的模塊。using System。using 。using 。namespace PaymentDemo{//這是一個工廠類 publicclassFactory { publicstaticPayment PaymentFactory(string PaymentName) { Payment mdb=null。 switch (PaymentName) { case現(xiàn)金: mdb=newCashPayment()。 break。 case信用卡: mdb=newCreditPayment()。 break。 case支票: mdb=newCheckPayment()。 break。 } return mdb。 } }}調(diào)用: Payment obj。privatevoid button1_Click(object sender, EventArgs e) { obj = (現(xiàn)金)。 = ()。 = ()。 }privatevoid button2_Click(object sender, EventArgs e) { obj = (信用卡)。 = ()。 = ()。 }privatevoid button3_Click(object sender, EventArgs e) { obj = (支票)。 = ()。 = ()。 }現(xiàn)在需要每個類在調(diào)用方法goSale()的時候,除了完成原來的功能以外,先彈出一個對話框,顯示工廠的名稱,而且不需要改變來的系統(tǒng),為此,在工廠類的模塊種添加一個裝飾類Decorator,同時略微的改寫一下工廠類的代碼。//這是一個工廠類 publicclassFactory { publicstaticPayment PaymentFactory(string PaymentName) { Payment mdb=null。 switch (PaymentName) { case現(xiàn)金: mdb=newCashPayment()。 break。 case信用卡: mdb=newCreditPayment()。 break。 case支票: mdb=newCheckPayment()。 break。 } //return mdb。//下面是實現(xiàn)裝飾模式新加的代碼Decorator obj = newDecorator(PaymentName)。 = mdb。return obj。 } }//裝飾類publicclassDecorator : Payment {string strName。public Decorator(string strName) { = strName。 }Payment pm。publicPayment Pm {get {return pm。 }set { pm = value。 } }publicoverridestring Action() {//在執(zhí)行原來的代碼之前,彈出提示框 (strName)。return ()。 } }這就可以在用戶不知曉的情況下,也不更改原來的類的情況下,改變了性能。第五節(jié)利用策略與工廠模式實現(xiàn)通用的框架一、應用策略模式提升層的通用性意圖將算法封裝,使系統(tǒng)可以更換或擴展算法,策略模式的關(guān)鍵是所有子類的目標一致。結(jié)構(gòu)策略模式的結(jié)構(gòu)如下。 其中:Strategy(策略):抽象類,定義需要支持的算法接口,策略由上下文接口調(diào)用。示例:目標:在C下構(gòu)造通用的框架,就需要使用XML配置文件技術(shù)。構(gòu)造一個一個類容器框架,可以動態(tài)裝入和構(gòu)造對象,裝入類可以使用配置文件,這里利用了反射技術(shù)。問題:如何動態(tài)構(gòu)造對象,集中管理對象。解決方案:策略模式,XML文檔讀入,反射的應用。我們現(xiàn)在要處理的架構(gòu)如下:。configurationdescription說明/descriptionclass id=標記 type=類名,dll文件名property name=屬性名value屬性值/value/property………/class/configuration應用程序上下文接口:ApplicationContext只有一個方法,也就是由用戶提供的id提供類的實例。代碼:using System。using 。using 。using 。using 。using 。using 。namespace處理類{publicinterfaceApplicationContext {Object getClass(string id)。 }publicclassFileSystemXmlApplicationContext : ApplicationContext {//用一個哈西表保留從XML讀來的數(shù)據(jù)privateHashtable hs = newHashtable()。public FileSystemXmlApplicationContext(string fileName) {try { readXml(fileName)。 }catch (Exception e) {(())。 } }//私有的讀XML方法。privatevoid readXml(String fileName) {//讀XML把數(shù)據(jù)放入哈西表 hs = (fileName, class, property)。 }publicObject getClass(string id) {//由id取出內(nèi)部的哈西表對象Hashtable hsb = (Hashtable)hs[id]。Object obj = null。string m = hsb[type].ToString()。int i = (39。,39。)。string classname = (0, i)。string filename = (i + 1) + .dll。//利用反射動態(tài)構(gòu)造對象//定義一個公共語言運行庫應用程序構(gòu)造塊 MyDll。Type[] types。 MyDll = (filename)。 types = ()。 obj = (classname)。Type t = ()。IEnumerator em = ()。//指針放在第一個之前 ()。while (()) {DictionaryEntry s1 = (DictionaryEntry)。if (() != type) {string pname = set_ + ()。 (pname, , null, obj, newobject[] { })。 } }return obj。 } }//這是一個專門用于讀配置文件的類classConfiguration {publicstaticHashtable Attribute(String configname,String mostlyelem,String childmostlyelem) {Hashtable hs = newHashtable(
點擊復制文檔內(nèi)容
教學課件相關(guān)推薦
文庫吧 www.dybbs8.com
備案圖片鄂ICP備17016276號-1