Decorator Design Pattern

package com.sheting.design.pattern.demo7;

/**
 * @Author SheTing
 * @Time 2018/3/22 9:11
 */
public interface Car {
    void assemble();
}

package com.sheting.design.pattern.demo7;

/**
 * @Author SheTing
 * @Time 2018/3/22 9:12
 */
public class BasicCar implements Car {

    @Override
    public void assemble() {
        System.out.print("Basic Car.");
    }
}

package com.sheting.design.pattern.demo7;

/**
 * @Author SheTing
 * @Time 2018/3/22 9:12
 */
public class CarDecorator implements Car {

    protected Car car;

    public CarDecorator(Car c){
        this.car=c;
    }

    @Override
    public void assemble() {
        this.car.assemble();
    }

}

package com.sheting.design.pattern.demo7;

/**
 * @Author SheTing
 * @Time 2018/3/22 9:13
 */
public class SportsCar extends CarDecorator {

    public SportsCar(Car c) {
        super(c);
    }

    @Override
    public void assemble() {
        super.assemble();
        System.out.print(" Adding features of Sports Car.");
    }

}

package com.sheting.design.pattern.demo7;

/**
 * @Author SheTing
 * @Time 2018/3/22 9:15
 */
public class LuxuryCar extends CarDecorator{

    public LuxuryCar(Car c) {
        super(c);
    }

    @Override
    public void assemble(){
        super.assemble();
        System.out.print(" Adding features of Luxury Car.");
    }

}

package com.sheting.design.pattern.demo7;

/**
 * @Author SheTing
 * @Time 2018/3/22 9:16
 */
public class DecoratorPatternTest {

    public static void main(String[] args) {
        Car sportsCar = new SportsCar(new BasicCar());
        sportsCar.assemble();
        System.out.println("\n*****");

        Car sportsLuxuryCar = new SportsCar(new LuxuryCar(new BasicCar()));
        sportsLuxuryCar.assemble();
    }
    
}

在JDK中使用
String fileName = "/Users/pankaj/source.txt";
File file = new File(fileName);
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);

你可能感兴趣的:(Decorator Design Pattern)