Decorator mode in Design Mode

Source: Internet
Author: User

Decorator mode in Design Mode

The decorator mode is also called the packaging mode. It is an alternative to the inheritance relationship by extending the object functions in a transparent way to the client. The most typical example of using the decorator mode is the various Java IO streams.


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + signature + CjxwPrPpz/PX6bz + signature/O907/Signature + 38zl1 + m8/r3Hyaujus6qs + signature/Signature + m8/Signature/PX6bz + 0 rvWwrXEvdO/Signature + Signature /WwOCho7i61PC + 38zltcTXsMrOoaM8L3A + Cjxicj4KPHA + signature + CjxwPjxzdHJvbmc + s + signature + CjxwPjxwcmUgY2xhc3M9 "brush: java; "> public interface Car {public void show ();}
Specific Component roles:

public class RunCar implements Car {@Overridepublic void show() {this.run();}public void run() {System.out.println("Run...");}}

Abstract decoration role:

public abstract class CarDecorator implements Car {private Car car;public CarDecorator(Car car) {this.car = car;}public Car getCar() {return car;}@Overridepublic abstract void show(); }

Specific decorative role 1:

public class FlyCarDecorator extends CarDecorator {public FlyCarDecorator(Car car) {super(car);}@Overridepublic void show() {this.getCar().show();this.fly();}public void fly() {System.out.println("Fly...");}}

Specific decorative role 2:

public class SwimCarDecorator extends CarDecorator {public SwimCarDecorator(Car car) {super(car);}@Overridepublic void show() {this.getCar().show();this.swim();}public void swim() {System.out.println("Swim...");}}

Client call test class:

public class ClientApp {public static void main(String[] args) {Car car = new RunCar();car.show();System.out.println("----------------");CarDecorator swimCar = new SwimCarDecorator(car);swimCar.show();System.out.println("----------------");CarDecorator flySwimCar = new FlyCarDecorator(swimCar);flySwimCar.show();}}

Although the above Code has no practical significance, you can also learn how to use the decorator mode.


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.