User requirements:
Design ideas:
1.UML Figure
2. Use the decorative mode to dynamically add some additional functionality to an object, providing a more resilient alternative than inheritance for extended functionality.
Specific code implementation:
1. Abstract Components
Moilephone class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceylx{ Public Abstract classMobilephone { Public Abstract voidSendMessage (); Public Abstract voidCall (); Public Abstract stringShow (); }}
2. Specific components
Apple class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceylx{ Public classApple:mobilephone { Public Override voidSendMessage () {Console.WriteLine ("Apple features: Texting"); } Public Override voidCall () {Console.WriteLine ("Apple features: Call"); } Public Override stringShow () {stringstr ="Apple features: send SMS \ n"+"Apple features: Call \ n"; returnstr; } }}
mi class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceylx{ Public classMi:mobilephone { Public Override voidSendMessage () {Console.WriteLine ("mi function: Send SMS"); } Public Override voidCall () {Console.WriteLine ("mi function: Call"); } Public Override stringShow () {stringstr ="mi function: send SMS \ n"+"mi function: Call \ n"; returnstr; } }}
3. Abstract Decoration class
function Class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceylx{ Public Abstract classFunction:mobilephone {PrivateMobilephone Mobilephone; Public voiddecorate (mobilephone phone) {mobilephone=phone; } Public Override voidSendMessage () {mobilephone. SendMessage (); } Public Override voidCall () {mobilephone. Call (); } Public Override stringShow () {stringStr=Mobilephone. Show (); returnstr; } }}
4. Specific decoration category
Bluetooth class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceylx{ Public classbluetooth:function { Public voidconnection () {Console.WriteLine ("Bluetooth module: Connection function"); } Public Override stringShow () {stringStr=Base. Show () +"Bluetooth module: connection function \ n"; returnstr; } }}
GPS class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceylx{ Public classgps:function { Public stringLocation ; Public Override stringShow () { location="This is the school of Science and Technology of Ningbo University \ n"; stringstr =Base. Show () +"GPS module Positioning function:"+Location ; returnstr; } }}
Cemera class
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceylx{ Public classcemera:function { Public Override voidCall () {Console.WriteLine ("Call feature Upgrade: Video Call"); } Public Override stringShow () {stringstr =Base. Show () +"Call feature upgrade: Video Call \ n"; returnstr; } }}
Client
usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;namespaceylx{classProgram {Static voidMain (string[] args) {Mobilephone Apple=NewApple (); Mobilephone mi=NewMi (); Bluetooth bluetooth=NewBluetooth (); Bluetooth. Decorate (apple); GPS GPS=NewGPS (); Gps. Decorate (Bluetooth); Cemera Cemera=NewCemera (); Cemera. Decorate (GPS); Console.WriteLine (Cemera. Show ()); Bluetooth bluetooth1=NewBluetooth (); Bluetooth1. Decorate (MI); Cemera Cemera1=NewCemera (); Cemera1. Decorate (bluetooth1); Console.WriteLine (Cemera1. Show ()); Console.read (); } }}
Operation Result:
Experience and sentiment:
Advantages:
Decoration patterns can provide more flexibility than inheritance, extending the functionality of an object in a dynamic manner, and creating a combination of many different behaviors by using different decorative classes and the arrangement combinations of these decorations.
the concrete structure class and the concrete adornment class can change independently.
Disadvantages:
When you use adornment mode, you produce many small objects that are more error-prone than inheritance.
Applicable scenarios:
It is useful for adding responsibilities to a single object in a dynamic, transparent manner without affecting other objects. These functions can also be dynamically revoked when it is necessary to dynamically add functionality to an object.
Decoration Mode Instance