Java design mode (11): Structural-Decorative mode (Decorator)

Source: Internet
Author: User

Responsibilities:

Dynamically adds new functionality to an object.

Decoration mode is a technique used in place of inheritance, and it is not possible to extend the new functionality of an object without the addition of inheritance.

It is more flexible to use the object's association relation instead of the inheritance relation, and avoids the rapid expansion of the type system.


Implementation Details:

Componment Abstract Component Roles

    • Real objects and decorative objects have the same interface. This allows the client object to interact with the adornment object in the same way as the real object.
Concretecomponment Specific component roles (real objects)
    • FileInputStream, fileoutstream in IO stream
Decorator Decorative characters:
    • Holds a reference to an abstract artifact. The adornment object accepts requests from all clients and forwards them to the real object so that new functionality can be added before and after the real object is called.
Concretedecorator Specific Decorative characters:
    • Responsible for adding new responsibilities to the component objects.

/** * Abstract Component */public interface ICar {void move ();} /** * Real Build */class Car implements icar{@Overridepublic void Move () {System.out.println ("Run on Land");}} Class Supercar implements icar{protected ICar Car;public supercar (ICar car) {super (); this.car = car;} @Overridepublic void Move () {Car.move ();}} Class Flycar extends Supercar{public flycar (ICar car) {super (car);} public void Fly () {System.out.println ("Flying in the Sky");} public void Move () {super.move (); Fly ();}} Class Watercar extends Supercar{public watercar (ICar car) {super (car);} public void Swim () {System.out.println ("midstream Water");} public void Move () {super.move (); Swim ();}} Class Aicar extends Supercar{public aicar (ICar car) {super (car);} public void Automove () {System.out.println ("auto-run"); public void Move () {super.move (); Automove ();}}

Test class:

public class Client{public static void Main (string[] args) {Car car = new Car (); Car.move (); System.out.println ("Add new features, fly ....") "); Flycar Flycar = new Flycar (car); Flycar.move (); System.out.println ("Add new features, swim in the water ....") "); Watercar wcar = new Watercar (flycar); Wcar.move ();}}


Scenarios used in development:

    1. Design of input stream and output stream in IO
    2. The component functions of the graphical interface in swing package
    3. The Servlet API provides a default implementation class for the decorator design pattern of a Request object
    4. Httpservletrequestwrapper, which enhances the functionality of the Request object.
    5. Struts2, handling of Request,response,seesion objects



The difference between decorative mode and bridging mode:

Two modes are designed to solve too many sub-object problems, but they have different incentives.

    • Bridge mode is the existing mechanism of the object itself along with several maintenance changes, is the existing partial instability,
    • The decoration mode is designed to add new features.

Java design mode (11): Structural-Decorative mode (Decorator)

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.