![]() If the cache already has the data, I simply return it. If not, I load it and then add it to the cache. To cache the data, I use IMemor圜ache and check the cache if it contains my data. To do that, I created the DataServiceCachingDecorator class and also implemented the IDataService interface. In the GetData method, I add a stopwatch to measure how long collecting data takes and then log the time it took.Īdditionally, I want to add caching also using a decorator. To achieve that, I created the DataServiceLoggingDecorator class and implement the IDataService interface. The first feature I want to add with a decorator is logging. This method is called in the GetData action and then printed to the website. Inside the loop, I added a Thread.Sleep to slow down the data collection a bit to make it more real-world like. I created a DataService class with a GetData method which returns a list of ints. ![]() You can find the code of the demo on GitHub. Another use case is to modify data that is sent to or from a component. Mostly this pattern is used for cross-cutting concerns like logging or caching. The decorator pattern should be used to extend classes without changing them. Decorator Pattern UML (Source) When to use the Decorator Pattern
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |