Design Mønstre // Builder + Decorator
Design Mønstre // Builder + Decorator
Design Mønstre // Builder + Decorator

Design Mønstre // Builder + Decorator

Af Victor Østergaard Nielsen og Lukas Wai Lok Chan Mac

10/11/2024

Klassediagram

Loading...

Forklaring

Armor Interface og BasicArmor definerer basisstrukturen for rustnings systemet. ArmorDecorator: Abstractklasse der implementerer Decorator mønstret nu kan EnchantedArmor og UnbreakingArmor kasserne laves som konkrete klasser der tilføjer egenskaber til rustningen. ArmorBuilder Interface og ConcreteArmorBuilder implementerer Builder mønstret for at konstruere rustning trin for trin for at lette skabningen af rustningen på klienten. ArmorDirector styrer byggeprocesen ved hjælp af ArmorBuilder.

Hovedklassen viser hvordan man bruger systemet (er klienten)

Kode (ekstra opgave)

// Armor interface
public interface Armor {
    String getDescription();
    int getDefense();
}

// Concrete implementation af Armor
public class BasicArmor implements Armor {
    private String description;
    private int defense;

    public BasicArmor(String description, int defense) {
        this.description = description;
        this.defense = defense;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public int getDefense() {
        return defense;
    }
}

// Abstract Decorator
public abstract class ArmorDecorator implements Armor {
    protected Armor decoratedArmor;

    public ArmorDecorator(Armor armor) {
        this.decoratedArmor = armor;
    }

    @Override
    public String getDescription() {
        return decoratedArmor.getDescription();
    }

    @Override
    public int getDefense() {
        return decoratedArmor.getDefense();
    }
}

// Concrete Decorators
public class EnchantedArmor extends ArmorDecorator {
    public EnchantedArmor(Armor armor) {
        super(armor);
    }

    @Override
    public String getDescription() {
        return super.getDescription() + " (Enchanted)";
    }

    @Override
    public int getDefense() {
        return super.getDefense() + 5;
    }
}

public class UnbreakingArmor extends ArmorDecorator {
    public UnbreakingArmor(Armor armor) {
        super(armor);
    }

    @Override
    public String getDescription() {
        return super.getDescription() + " (Unbreaking)";
    }

    @Override
    public int getDefense() {
        return super.getDefense() + 2;
    }
}

// Builder Interface
public interface ArmorBuilder {
    void reset();
    void setArmorType(String type);
    void addEnchantment();
    void addUnbreaking();
    Armor getResult();
}

// Concrete Builder
public class ConcreteArmorBuilder implements ArmorBuilder {
    private Armor armor;

    @Override
    public void reset() {
        this.armor = null;
    }

    @Override
    public void setArmorType(String type) {
        switch (type.toLowerCase()) {
            case "leather":
                this.armor = new BasicArmor("Leather Armor", 5);
                break;
            case "iron":
                this.armor = new BasicArmor("Iron Armor", 15);
                break;
            case "diamond":
                this.armor = new BasicArmor("Diamond Armor", 20);
                break;
            default:
                throw new IllegalArgumentException("Unknown armor type");
        }
    }

    @Override
    public void addEnchantment() {
        if (this.armor != null) {
            this.armor = new EnchantedArmor(this.armor);
        }
    }

    @Override
    public void addUnbreaking() {
        if (this.armor != null) {
            this.armor = new UnbreakingArmor(this.armor);
        }
    }

    @Override
    public Armor getResult() {
        Armor result = this.armor;
        this.reset();
        return result;
    }
}

// Director
public class ArmorDirector {
    private ArmorBuilder builder;

    public ArmorDirector(ArmorBuilder builder) {
        this.builder = builder;
    }

    public Armor constructBasicArmor(String type) {
        builder.reset();
        builder.setArmorType(type);
        return builder.getResult();
    }

    public Armor constructEnchantedArmor(String type) {
        builder.reset();
        builder.setArmorType(type);
        builder.addEnchantment();
        return builder.getResult();
    }

    public Armor constructUnbreakableEnchantedArmor(String type) {
        builder.reset();
        builder.setArmorType(type);
        builder.addEnchantment();
        builder.addUnbreaking();
        return builder.getResult();
    }
}

// Main class to demonstrate usage
public class MinecraftArmorSystem {
    public static void main(String[] args) {
        ArmorBuilder builder = new ConcreteArmorBuilder();
        ArmorDirector director = new ArmorDirector(builder);

        Armor basicIronArmor = director.constructBasicArmor("iron");
        System.out.println(basicIronArmor.getDescription() + " - Defense: " + basicIronArmor.getDefense());

        Armor enchantedDiamondArmor = director.constructEnchantedArmor("diamond");
        System.out.println(enchantedDiamondArmor.getDescription() + " - Defense: " + enchantedDiamondArmor.getDefense());

        Armor ultimateArmor = director.constructUnbreakableEnchantedArmor("diamond");
        System.out.println(ultimateArmor.getDescription() + " - Defense: " + ultimateArmor.getDefense());
    }
}