Three ways to implement decorate in Java

Source: Internet
Author: User

Everyone who has read Gof's famous design pattern will know the decorator model. Now, let's forget about the decorator concept, and try to understand the decorator model from our development experience.

Decorator is another thing (or person) that is used to decorate a thing (or person). A decorator directly alters the duties or characteristics of the object being decorated, but cannot change the properties of the object being decorated. For example: A frame can decorate the picture, cosmetics can decorate the girl's face and so on.

Discuss some of the existing examples from our professional perspective:

1 JScrollPane can decorate the JComponent view section. The jcomponent itself is not changed, but adds a new attribute (scrollable).

The 2 Bufferedinputstream is a inputstream, itself Bufferedinputstream is a inputstream, but it is faster because it provides a cache of data.

3 Consider Debugbutton, which is the same as JButton, but it can add messages to the log file when clicked. Debugbutton is a JButton, because it directly changes the JButton but does not change its own properties.

4 again, like Scrolloverbutton, it adds a mouse-sliding behavior. It is flat when the mouse is moved, and it has a raised border when the mouse passes. Obviously, Scrolloverbutton is also a jbutton ornament.

Now, we know that decorator may have three different implementations:

1 Inheritance (inheritance)

2 Package (wrapper)

3 Plug-in (External)

This article discusses each implementation model and their pros and cons.

Inherited

For developers, the most intuitive decorator implementation is to write a derived class that inherits from the decorated class and assigns new responsibilities. New responsibilities can be achieved by adding methods or modifying existing methods.

public class DebugButton extends JButton
{
public DebugButton()
{
addActionListener(new ActionListener()
{
System.out.println("debug message");
});
}
}

In addition, we can use the same way to achieve Scrolloverbutton: not to increase actionlistener, but to increase mouselistener. Changes the JButton border in the MouseListener callback method and changes the border from Empetyborder to Raisedbevelborder when mouseentered () is invoked. When the mouseexited () method is called, the border is then restored from Raisedbevelborder to Empetyborder.

For Bufferedinputstream, the same implementation is also very simple. Modify each read data method so that it reads data from the memory buffer. If the buffer is empty, it can use the Super.read () method to fetch the data and populate the buffer. JScrollPane, it's a bit complicated to implement, and I'll discuss why it's more difficult to implement it in an inherited way.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.