package com.eric.结构型模式.装饰者模式.引例;
/**
* @author Eric
* @ProjectName my_design_23
* @description 抽象构件
* @CreateTime 2020-11-29 19:52:25
*/
public interface Component {
public void operation();
}
package com.eric.结构型模式.装饰者模式.引例;
/**
* @author Eric
* @ProjectName my_design_23
* @description 具体构件类
* @CreateTime 2020-11-29 20:04:05
*/
public class ConcreteComponent implements Component {
@Override
public void operation() {
//业务代码
}
}
package com.eric.结构型模式.装饰者模式.引例;
/**
* @author Eric
* @ProjectName my_design_23
* @description 装饰者
* @CreateTime 2020-11-29 20:04:47
*/
public class Decorator implements Component {
private Component component = null;
public Decorator(Component component){
this.component = component;
}
@Override
public void operation() {
this.component.operation();
}
}
package com.eric.结构型模式.装饰者模式.引例;
/**
* @author Eric
* @ProjectName my_design_23
* @description 具体装饰者
* @CreateTime 2020-11-29 20:06:35
*/
public class ConcreteDecorator extends Decorator {
public ConcreteDecorator(Component component) {
super(component);
}
//定义自己的方法
private void method(){
System.out.println("装饰");
}
//重写operation()方法
@Override
public void operation() {
this.method();
super.operation();
}
}
package com.eric.结构型模式.装饰者模式.例1;
/**
* @author Eric
* @ProjectName my_design_23
* @description 汽车接口
* @CreateTime 2020-11-29 23:46:29
*/
public interface Car {
//车的装配
public void show();
}
package com.eric.结构型模式.装饰者模式.例1;
/**
* @author Eric
* @ProjectName my_design_23
* @description 奔驰车(裸车-需装饰)
* @CreateTime 2020-11-29 23:47:10
*/
public class Benz implements Car{
@Override
public void show() {
System.out.println("奔驰车的默认颜色是黑色!");
}
}
package com.eric.结构型模式.装饰者模式.例1;
/**
* @author Eric
* @ProjectName my_design_23
* @description 汽车装饰抽象类
* @CreateTime 2020-11-29 23:48:36
*/
public abstract class CarDecorator implements Car {
private Car car = null;
public CarDecorator(Car car){
this.car = car;
}
@Override
public void show() {
this.car.show();
}
}
package com.eric.结构型模式.装饰者模式.例1;
/**
* @author Eric
* @ProjectName my_design_23
* @description 具体的汽车装饰类
* @CreateTime 2020-11-29 23:50:14
*/
public class ConcreteCarDecorator extends CarDecorator {
public ConcreteCarDecorator(Car car) {
super(car);
}
//给汽车进行彩绘
private void print(){
System.out.println("在车尾绘制“新手“字样,颜色是紫色霞光");
}
//给车安装GPS设备
private void setGps(){
System.out.println("安装GPS定位导航系统!");
}
//重写show()方法
public void show(){
super.show();
this.print();
this.setGps();
}
}
package com.eric.结构型模式.装饰者模式.例1;
/**
* @author Eric
* @ProjectName my_design_23
* @description 测试
* @CreateTime 2020-11-29 23:54:31
*/
public class ClientDemo {
public static void main(String[] args) {
Car car = new Benz();
//对奔驰车进行装饰
ConcreteCarDecorator decorator = new ConcreteCarDecorator(car);
decorator.show();
}
}
package com.eric.结构型模式.装饰者模式.例2;
/**
* @author Eric
* @ProjectName my_design_23
* @description 形状接口
* @CreateTime 2020-11-30 00:09:30
*/
public interface Shape {
//画画
public void draw();
}
package com.eric.结构型模式.装饰者模式.例2;
/**
* @author Eric
* @ProjectName my_design_23
* @description 圈圈
* @CreateTime 2020-11-30 00:10:09
*/
public class Circle implements Shape{
@Override
public void draw() {
System.out.println("画了个圈圈...");
}
}
package com.eric.结构型模式.装饰者模式.例2;
/**
* @author Eric
* @ProjectName my_design_23
* @description 矩形
* @CreateTime 2020-11-30 00:11:29
*/
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("画了一个框框...");
}
}
package com.eric.结构型模式.装饰者模式.例2;
/**
* @author Eric
* @ProjectName my_design_23
* @description 矩形
* @CreateTime 2020-11-30 00:11:29
*/
public class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("画了一个框框...");
}
}
package com.eric.结构型模式.装饰者模式.例2;
/**
* @author Eric
* @ProjectName my_design_23
* @description 红色的装饰者
* @CreateTime 2020-11-30 00:14:41
*/
public class RedShapeDecorator extends ShapeDecorator {
public RedShapeDecorator(Shape shape) {
super(shape);
}
private void setRedBorder(){
System.out.println("边边是红色的...");
}
@Override
public void draw() {
shape.draw();
this.setRedBorder();
}
}
package com.eric.结构型模式.装饰者模式.例2;
/**
* @author Eric
* @ProjectName my_design_23
* @description 测试
* @CreateTime 2020-11-30 00:20:50
*/
public class DecoratorPatternDemo {
public static void main(String[] args) {
Shape circle = new Circle();
ShapeDecorator redShapeDecorator1 = new RedShapeDecorator(circle);
redShapeDecorator1.draw();
Shape rectangle = new Rectangle();
ShapeDecorator redShapeDecorator2 = new RedShapeDecorator(rectangle);
redShapeDecorator2.draw();
}
}
原文:https://www.cnblogs.com/zyl-0110/p/14204672.html