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.