简体   繁体   中英

Decorator Pattern : How to withdraw additional responsibilities from a decorated object ?

I have a sheet class in my application:

 class  Sheet
{
  Public int[] RatioIDs{get;set;}
  Public int[] PartnerIDs{get;set;}
}

This sheet class have thousands of instances created and stored at the run time, However when I process them I would be working at max 5 sheet objects.Only before processing a sheet object I decorate it with additional properties and methods to avoid the overhead:

//Assume I have decorated the sheet object with following additional responsiblities.

class DecoratedSheet
{
//There would be like 100 partner object in this array.
//and these partners are fetched based on partnerIDs array in sheet object
 Public Partner[] Partners;

public double GetRatioAmountForPartner(Partner partner)
{
  //Get ratio amount, its calculation would be based on
 //RatioID array in sheet object for a given partner.
}    

}

Once I'm done with a decorated sheet object, I don't need its additional properties (esp. the array of 100 partners). So I what to withdraw the additional responsibilities attached to this decorated sheet object. How should I do this ??

class Sheet : ISheetWithPartners {
    public IEnumerable<int> RatioIDs { get; set; }
    public IEnumerable<int> PartnerIDs { get; set; }
    public IEnumerable<Partner> Partners { get; set; }
    public double GetRatioAmountForPartner(Partner partner) {
        // Your code here
    }
}

interface ISheet {
    IEnumerable<int> RatioIDs { get; }
    IEnumerable<int> PartnerIDs { get; }
}

interface ISheetWithPartners : ISheet {
    IEnumerable<Partner> Partners { get; }
    double GetRatioAmountForPartner(Partner partner);
}

Then you can deal with interfaces. Easier than creating functions like .GetBase() or whatever

Do you want to retain the decorated state of the object or do you just want to fetch the original object in it's original state? The idea of a decorator is that you have a base object with minimal properties and you add to it at run time. In this case I don't know what the decorator is - you haven't specified it in your code. Eg. is Partner a decorator for Sheet? Where does the actual 'Sheet' object come into play? DecoratedSheet takes a Partner object and stores it in an array of Partners, but where does Sheet come in to play?

Easiest way would be to implement a getComponent() or getBase() method that returns a Sheet object from whatever you're decorating it with. Each decorator you create and add to the original object can and should have access to the properties of the object, whilst containing their own properties, so that you can swap and change as you like.

I hope that helps.

The wiki page has a good example: http://en.wikipedia.org/wiki/Decorator_pattern

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM