{"id":491,"date":"2010-07-16T09:31:00","date_gmt":"2010-07-16T09:31:00","guid":{"rendered":"https:\/\/opstree.com\/blog\/\/2010\/07\/16\/template-design-pattern-in-action-part1\/"},"modified":"2019-07-11T07:45:29","modified_gmt":"2019-07-11T07:45:29","slug":"template-design-pattern-in-action-part1","status":"publish","type":"post","link":"https:\/\/opstree.com\/blog\/2010\/07\/16\/template-design-pattern-in-action-part1\/","title":{"rendered":"Template design pattern in action part1"},"content":{"rendered":"<p>So finally I got time to complete the blog \ud83d\ude42<\/p>\n<p>I&#8217;ll discuss about how we can solve the problem using templating design pattern. So what we have done, we have written a Template class &#8220;CacheTemplate&#8221; having a method getCachedData.<br \/>\ngetCachedData takes 2 parameters<br \/>\nkey: Key for which we have data cached<br \/>\ncacheCallback: If we don&#8217;t have data cached then we will call this cacheCallback to get the data and store it into cache.<\/p>\n<p><a name=\"more\"><\/a><\/p>\n<pre style=\"background-color:#eeeeee;border:1px dashed rgb(153,153,153);color:black;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;line-height:14px;overflow:auto;padding:5px;width:100%;\"><code>\npublic class CacheTemplate {\nprivate CacheProvider cacheProvider;\npublic  T getCachedData(String key,\nCacheCallback cacheCallback) {\nT data = (T) cacheProvider.get(key);\nif (data == null) {\ndata = cacheCallback.cacheOperation();\ncacheProvider.put(key, data);\n}\nreturn data;\n}\n}\n<\/code>\n<\/pre>\n<p>Now taking forward the example taken in previos blog, let us apply this template to the &#8220;getPersons&#8221; method<\/p>\n<pre style=\"background-color:#eeeeee;border:1px dashed rgb(153,153,153);color:black;font-family:Andale Mono, Lucida Console, Monaco, fixed, monospace;font-size:12px;line-height:14px;overflow:auto;padding:5px;width:100%;\"><code>\npublic List getPersons() {\n\nreturn cacheTemplate.getCachedData(\"persons\", new CacheCallback&gt;() {\n@Override\npublic List cacheOperation() {\n_getPersons();\n}\n}\n}\n\nprivate List _getPersons() {\npersons = \/\/Business logic to get persons;\nreturn persons;\n}\n<\/code>\n<\/pre>\n<p>Now if you compare the current implementation with the previous implementation and check the concerns we had they all are resolved.<br \/>\n1.) Now our business logic of retrieving the persons is at one place.<br \/>\n2.) Now we have a generic implementation of managing cache.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So finally I got time to complete the blog \ud83d\ude42 I&#8217;ll discuss about how we can solve the problem using templating design pattern. So what we have done, we have written a Template class &#8220;CacheTemplate&#8221; having a method getCachedData. getCachedData takes 2 parameters key: Key for which we have data cached cacheCallback: If we don&#8217;t &hellip; <a href=\"https:\/\/opstree.com\/blog\/2010\/07\/16\/template-design-pattern-in-action-part1\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Template design pattern in action part1&#8221;<\/span><\/a><\/p>\n","protected":false},"author":150552946,"featured_media":29900,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_coblocks_attr":"","_coblocks_dimensions":"","_coblocks_responsive_height":"","_coblocks_accordion_ie_support":"","jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[1],"tags":[16749,36769,11867],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"https:\/\/opstree.com\/blog\/wp-content\/uploads\/2025\/11\/DevSecOps-1.jpg","jetpack_likes_enabled":true,"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pfDBOm-7V","jetpack-related-posts":[],"_links":{"self":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/491"}],"collection":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/users\/150552946"}],"replies":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/comments?post=491"}],"version-history":[{"count":3,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/491\/revisions"}],"predecessor-version":[{"id":956,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/posts\/491\/revisions\/956"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media\/29900"}],"wp:attachment":[{"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/media?parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/categories?post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/opstree.com\/blog\/wp-json\/wp\/v2\/tags?post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}