Decorator with a different contract

In the decorator template, I am confused about how to use the decorator method. I found out that the decorator pattern is used to add functions to the base class. But I could only name the external decorator method, so how to use the internal decorator method if it is not mentioned in the interface. I am not good at English, so I write code to demonstrate my question.

public class OrderSystem {
    public static void main(String[] args) {
        Pancakes pancakes = new MixedPancakes();
        pancakes = new Ham(pancakes);
        ((Ham) pancakes).hamState(); // call hamState
        pancakes = new Egg(pancakes);
        ((Egg) pancakes).eggState();
        // i can't call hamState() there because it not belong to Egg

        Pancakes pancakes1 = new Ham(new Egg(new FlourPancakes()));
        // similarly, i can't use eggState() there.
        System.out.println("订单:" + pancakes1.getDescription());
        System.out.println("价格:" + pancakes1.cost());
    }
}

interface Pancakes {
    public abstract String getDescription();

    public abstract int cost();
}

abstract class Seasoning implements Pancakes {
    @Override
    public abstract String getDescription();
}

class Ham extends Seasoning {

    Pancakes pancakes;

    public Ham(Pancakes pancakes) {
        this.pancakes = pancakes;
    }

    @Override
    public int cost() {
        return pancakes.cost() + 2;
    }

    @Override
    public String getDescription() {
        return pancakes.getDescription() + "+火腿";
    }

    public void hamState() {
        System.out.println("火腿切碎");
    }

}

class Egg extends Seasoning {

    Pancakes pancakes;

    public Egg(Pancakes pancakes) {
        this.pancakes = pancakes;
    }

    @Override
    public int cost() {
        return pancakes.cost() + 1;
    }

    @Override
    public String getDescription() {
        return pancakes.getDescription() + "+鸡蛋";
    }

    public void eggState() {
        System.out.println("鸡蛋打碎");
    }
}

class MixedPancakes implements Pancakes {

    @Override
    public String getDescription() {
        return "五谷杂粮煎饼";
    }

    @Override
    public int cost() {
        return 6;
    }
}

class FlourPancakes implements Pancakes {

    @Override
    public String getDescription() {
        return "白面煎饼";
    }

    @Override
    public int cost() {
        return 5;
    }
}

, , , (, cost() getDescription()), . , , , shoot() - . , shoot(), cut() - . ? .

+4
1

, .

, :

public abstract class Soldier {
    public abstract void attack();
}

public abstract class SoldierDecorator extends Soldier {

    protected Soldier soldier;

    public SoldierDecorator(Soldier soldier) {
        this.soldier = soldier;
    }

    @Override
    public abstract void attack();
}

public class SoldierWithGun extends SoldierDecorator {

    public SoldierWithGun(Soldier soldier) {
        super(soldier);
    }

    @Override
    public void attack() {
        soldier.attack();
        shootWithTheGun();
    }

    private void shootWithTheGun() {
        System.out.println("Shooting with the gun...");
    }
}

public class SoldierWithSword extends SoldierDecorator {

    public SoldierWithSword(Soldier soldier) {
        super(soldier);
    }

    @Override
    public void attack() {
        soldier.attack();
        cutWithSword();
    }

    private void cutWithSword() {
        System.out.println("Cutting with the sword...");
    }
}

;

, /, .

, , BaseSoldier , SoldierWithGun BaseSoldier, , .

, , , .

"", , , , .

+1

Source: https://habr.com/ru/post/1670166/


All Articles