Decorative mode is the ability to dynamically extend an object without having to change the original class file and use inheritance. By creating a wrapper object, that is, decorating to wrap the real object. Decorative objects in decorative mode have the same interface as real objects. This allows the client object to interact with the adornment object in the same way as the real object, while the Adornment object contains a reference to the real object (reference), and the adornment object accepts all requests from the client. It forwards these requests to the real object. Both adornment mode and inheritance can extend the functionality of an object, but adornment mode provides more flexibility than inheritance. By using different decorative classes and arranging combinations of these decorations, you can create a combination of many different behaviors.
Basic Design
Simply look at the concept a bit monotonous, see a classic decorative mode of UML class diagram:
Component: Definethe method that Concretecompon ENT and decorator class to implement, the communication between adornment object and real object is realized through Component;
concretecomponent : Real objects that use concretecomponent derived classes to provide core functionality, at the same level as decorator;
Decorator : a class with a specific decorative function, used to decorate the Concretecomponent class, the specific decorative sub-class by inheriting decorator implementation;
About the overall contour has a general understanding, we can use the example of life to simulate the decorative mode, about the house, there are commercial residential and residential, bought a house we need to decorate, table, chair is necessary, we finally need to calculate the total cost, think a few seconds can see the following concrete realization;
function Implementation
Design the base class House and protocol:
@protocol housedelegate <NSObject> @optional-(double) Totalmoney; @end @interface house:nsobject< housedelegate>-(NSString *) Detialinfo; @end
House sub-class commercialhouse, decorated with objects:
@implementation commercialhouse-(double) totalmoney{ return 360000.89;} -(NSString *) detialinfo{ return [nsstring stringwithformat:@ "commercial House"];} @end
The base class of the Adornment object is Housedecorator, where a reference to the adorned object needs to be maintained:
@interface housedecorator:house-(instancetype) Initwithhouse: House, @property (strong,nonatomic) House *house; @end
@implementation housedecorator-(Instancetype) Initwithhouse: (House *) house{ self=[super init]; if (self) { self.house=house; } return self;} @end
Decorative Objects Tabledecorator:
@implementation tabledecorator-(double) totalmoney{ return self.house.totalmoney+10;} -(NSString *) detialinfo{ return [nsstring stringwithformat:@ "%@--table", Self.house.detialInfo];} @end
Decorative Objects Chairdecorator:
@implementation chairdecorator-(double) totalmoney{ return self.house.totalmoney+100;} -(NSString *) detialinfo{ return [NSString stringwithformat:@ "%@--chair", Self.house.detialInfo];} @end
Back to the initial total cost issue, let's look at the results:
House *house=[[commercialhouse Alloc]init]; House=[[tabledecorator Alloc]initwithhouse:house]; House=[[chairdecorator Alloc]initwithhouse:house]; NSLog (@ "house price:%f", [home Totalmoney]); NSLog (@ "House details:%@", [home Detialinfo]); NSLog (@ "blog Park-flyelephant"); NSLog (@ "http://www.cnblogs.com/xiaofeixiang/");
The price of the table chair can be changed dynamically, the number of table chairs is also uncertain, from these angles to look at the example slightly reluctantly, but probably the function design to achieve similar, original aim, I believe we will have their own unique view of the decorative mode ~
iOS Development-Decorating mode