Now I have made 3 classes
- AddHashCode
- ToLowerCase
- AddLength
I want to create a class, which has toString method overridden while selecting one or more of the above classes.
For example:
I want a class, which has toString method, which has the features of AddHashCode and ToLowerCase
or
I want a class, which has toString method, which has the features of all of the above classes.
So, lets do it with Decorator Pattern.
But the problem with Decorator Pattern is, that the classes you create must implement the String class.....But String class is final..
So I have tweaked the Decorator Design Pattern a bit, though it closely resembles the purpose of Decorator design pattern.
Create the following three classes
Class AddHashCode
Class ToLowerCase
Class AddLength
Use this as follows:
public class Main { public static void main(String[] args) { // Limitation and difference here will be, that on the LHS, the class used is ToLowerCase // and cannot be a String as was the case with usual Decorators. ToLowerCase x = new ToLowerCase (new AddHashCode (new String("YOGESH").toString()).toString()); System.out.println(x.toString()); AddHashCode y = new AddHashCode (new ToLowerCase (new AddLength(new String("YOGESH").toString()) .toString()) .toString()); System.out.println(y.toString()); } }
No comments:
Post a Comment