介紹
意圖:動態地為一個物件增加一些額外的職責。就增加功能來說,裝飾模式相比生成子類別更靈活。
主要解決:我們擴展一個類別常使用繼承方式實現,由於繼承為類別引入靜態特徵,並且隨著擴展功能的增多,子類別會越來越膨脹。
如何使用:在不想增加很多子類別的情況下擴充。
如何解決:將具體功能職責劃分,同時繼承裝飾者模式。
關鍵程式碼:
1. Component 類別扮演抽象角色,不應該具體實作。
2. 修飾類別引用合繼承 Component 類,具體擴展類別重寫父類別方法。
使用場景:
1. 擴充一個類別的功能。
2. 動態增加功能,動態撤銷。
實作
建立一個 Shape 介面合實作了 Shape 介面的實體類別。然後再建立一個實作了 Shape 介面的抽象裝飾類別 ShapeDecorator,並把 Shape 物件當作它的實例變數。 RedShapeDecorator 是實作了 ShapeDecorator 的實體類別。 DecoratorPatternDemo 類別使用 RedShapeDecorator 來裝飾 Shape 物件。
步驟1:
public interface Shape{ void draw(); }
步驟2:
public class Rectangle implements Shape{ @Override public ovid draw(){ System.out.println("draw Rectangle"); } }
public class Cricle implements Shape{ @Override public ovid draw(){ System.out.println("draw Circle"); } }
步驟3:
public abstract class ShapeDecorator implements Shape{ private Shape shape; // 持有一个 Shape 对象 public ShapeDecorator(Shape shape){ this.shape = shape; } public void draw(){ shape.draw();// TODO 根据传进来的具体 Shape 对象,调用对应的 draw 方法 } }
步驟4:
public class RedShapeDecorator extends ShapeDecorator{ public RedShapeDecorator(Shape shape){ super(shape); } @Override public void draw(){ shape.draw(); setRedBorder(shape); } public void setRedBorder(Shape shape){ System.out.println("Border Color: Red"); } }
步驟5
public class DecoratorPatterndDemo{ public static void main(String args[]){ //TODO 面向抽象层编程 // 普通的Circle Shape circle = new Circle(); System.out.println("Circle with normal Border"); circle.darw(); // 红色边界的 Circle ShapeDecorator redCircle = new RedShapeDecorator(new Circle); System.out.println("Circle of red Border"); redCircle.draw(); // 红色边界的 Rectangle ShapeDecorator redRectangle = new RedShapeDecorator(new Rectangel): System.out.println("Rectangle of red Border"); redRectangle.draw(); } }
優缺點
優點:裝飾類別和被裝飾類別可以獨立發展,不會相互耦合,裝飾模式是繼承的一個替代模式,裝飾模式可以動態擴展一個實現類別的功能。
缺點:多層裝飾比較複雜。
以上是Java結構型設計模式之裝飾模式怎麼實現的詳細內容。更多資訊請關注PHP中文網其他相關文章!

Javaremainsagoodlanguageduetoitscontinuousevolutionandrobustecosystem.1)Lambdaexpressionsenhancecodereadabilityandenablefunctionalprogramming.2)Streamsallowforefficientdataprocessing,particularlywithlargedatasets.3)ThemodularsystemintroducedinJava9im

Javaisgreatduetoitsplatformindependence,robustOOPsupport,extensivelibraries,andstrongcommunity.1)PlatformindependenceviaJVMallowscodetorunonvariousplatforms.2)OOPfeatureslikeencapsulation,inheritance,andpolymorphismenablemodularandscalablecode.3)Rich

Java的五大特色是多態性、Lambda表達式、StreamsAPI、泛型和異常處理。 1.多態性讓不同類的對象可以作為共同基類的對象使用。 2.Lambda表達式使代碼更簡潔,特別適合處理集合和流。 3.StreamsAPI高效處理大數據集,支持聲明式操作。 4.泛型提供類型安全和重用性,編譯時捕獲類型錯誤。 5.異常處理幫助優雅處理錯誤,編寫可靠軟件。

java'stopfeatureSnificallyenhanceItsperformanCandScalability.1)對象 - 方向clincipleslike-polymormormormormormormormormormormormorableableflexibleandscalablecode.2)garbageCollectionAutectionAutoctionAutoctionAutoctionAutoctionAutoctionAutoMenateMememorymanateMmanateMmanateMmanagementButCancausElatemention.3)

JVM的核心組件包括ClassLoader、RuntimeDataArea和ExecutionEngine。 1)ClassLoader負責加載、鏈接和初始化類和接口。 2)RuntimeDataArea包含MethodArea、Heap、Stack、PCRegister和NativeMethodStacks。 3)ExecutionEngine由Interpreter、JITCompiler和GarbageCollector組成,負責bytecode的執行和優化。

Java'ssafetyandsecurityarebolsteredby:1)strongtyping,whichpreventstype-relatederrors;2)automaticmemorymanagementviagarbagecollection,reducingmemory-relatedvulnerabilities;3)sandboxing,isolatingcodefromthesystem;and4)robustexceptionhandling,ensuringgr

Javaoffersseveralkeyfeaturesthatenhancecodingskills:1)對象 - 方向 - 方向上的allowslowsmodelowsmodelingreal-worldentities

thejvmisacrucialcomponentthatrunsjavacodebytranslatingitolachine特定結構,影響性能,安全性和便攜性。 1)theclassloaderloader,links andinitializesClasses.2)theexecutionEngineExecutionEngineExecutionEngineExecuteNexeCuteByteCuteByteCuteByTecuteByteCuteByteCuteBytecuteBytecuteByteCoDeinintolachineinstructionsions.3)Memo.3)Memo


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

Atom編輯器mac版下載
最受歡迎的的開源編輯器

SublimeText3 英文版
推薦:為Win版本,支援程式碼提示!

Dreamweaver CS6
視覺化網頁開發工具

EditPlus 中文破解版
體積小,語法高亮,不支援程式碼提示功能

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中