Decorate

allelopath
allelopath used Ask the Experts™
on
What does it mean, to 'decorate' in Java terms (or perhaps broader, OO terms)
As in here:
http://apachecommonstipsandtricks.blogspot.com/2009/03/examples-of-lazy-lists-and-factories.html
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Decorate is a design pattern used in java class ...

Create a instantians for new class then  ur calss will check the pool memory if already avaliable then its give the same instances otherwise it will create a new one ...  best example for this patter is String ..
Sr. Software engineer
Commented:
I think the previous answer is wrong.
dravidnsr described the Singleton pattern instead of the Decorator pattern.

About the decorator pattern:
- http://www.allapplabs.com/java_design_patterns/decorator_pattern.htm
- https://en.wikipedia.org/wiki/Decorator_pattern

In short:
A decorator takes the original object and adds a certain behaviour to it.

Simple example:
Assume you have a class Person which contains among others the attribute yearOfBirth:

public class Person {
    ...
    private int yearOfBirth;
    ...

    public int getYearOfBirth() {
          return yearOfBirth;
    }
}

Open in new window


Now, when displaying a person's information, assume you want to display its age rather than its year of birth.

Option 1: add a getAge() method to the Person class
But, if you just have to add it for displaying reasons and don't really need it in your business logic, it might be better to apply the decorator pattern:

Option 2:
public class PersonDecorator {

    private Person person;

    public PersonDecorator(Person person) {
         this.person = person;
    }

    public String getName() {
         return person==null ? null : person.getName();
    }

    public String getAddress() {
        return person==null ? null : person.getAddress();
    }
    ...
    public int getAge() {
        if (person==null) return 0;
        // calculate currentYear
        return currentYear - person.getYearOfBirth();
    }
    ...
}

Open in new window


If further on, you need yet another extra "behaviour", you can extend your PersonDecorator class rather than your core Person class.
(This was just a stupid example to illustrate the idea.)
sorry my answer is wrong .. but its not (answer) not related to singleton

Author

Commented:
Thanks!
zzynxSr. Software engineer

Commented:
You're welcome.
Thanx 4 axxepting

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial