{"id":4872,"date":"2024-02-12T16:00:46","date_gmt":"2024-02-12T16:00:46","guid":{"rendered":"https:\/\/codethataint.com\/blog\/?p=4872"},"modified":"2024-02-13T02:15:23","modified_gmt":"2024-02-13T02:15:23","slug":"telescoping-pattern","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/telescoping-pattern\/","title":{"rendered":"Telescoping Pattern"},"content":{"rendered":"<p><strong class=\"ctaHeader2\">What is Telescoping Pattern<\/strong><br \/>\nA telescoping constructor is a series of constructors where each one has a different number of parameters. Each constructor calls a more specific constructor in the hierarchy, providing a default value for the remaining parameters.<\/p>\n<p><strong class=\"ctaHeader2\">Disadvantage of Below Approach<\/strong><\/p>\n<ol>\n<li>Constructors are hard to read when they have many parameters <\/li>\n<li>When adding new parameters, you have to add new constructors to the chain. This pattern can therefore not scale very well <\/li>\n<li>It becomes difficult to remember the required order of the parameters, especially when many of them are of the same type. Additionally, the code becomes less readable, and it&#8217;s not always clear what is being passed to the constructor. This is addressed by using Builder Pattern\n<\/li>\n<\/ol>\n<p><strong>Report<\/strong><\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\r\nimport java.util.Date;\r\n\r\npublic class Report {\r\n    private String reportName;\r\n    private Integer reportSize;\r\n    private String  reportFormat;\r\n    private Date reportDate;\r\n\r\n    public Report() {\r\n    }\r\n\r\n    \/\/Constructor1\r\n    public Report(String reportName) {\r\n        this.reportName = reportName;\r\n    }\r\n\r\n    public Report(String reportName, Integer reportSize) {\r\n        this(reportName); \/\/Call to before Constructor1\r\n        this.reportSize = reportSize;\r\n    }\r\n\r\n    public Report(String reportName, Integer reportSize, String  reportFormat) {\r\n        this(reportName, reportSize); \/\/Call to before Constructor2\r\n        this.reportFormat = reportFormat;\r\n    }\r\n\r\n    public Report(String reportName, Integer reportSize, String  reportFormat, Date reportDate) {\r\n        this(reportName, reportSize, reportFormat); \/\/Call to before Constructor3\r\n        this.reportDate = reportDate;\r\n    }\r\n}\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is Telescoping Pattern A telescoping constructor is a series of constructors where each one has a different number of parameters. Each constructor calls a more specific constructor in the hierarchy, providing a default value for the remaining parameters. Disadvantage of Below Approach Constructors are hard to read when they have many parameters When adding&hellip; <a href=\"https:\/\/codethataint.com\/blog\/telescoping-pattern\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[351,64],"tags":[],"class_list":["post-4872","post","type-post","status-publish","format-standard","hentry","category-antipattern","category-design-patterns"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4872","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=4872"}],"version-history":[{"count":4,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4872\/revisions"}],"predecessor-version":[{"id":4876,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/4872\/revisions\/4876"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=4872"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=4872"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=4872"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}