건설 자 모드 - 노트

19522 단어 디자인 모드Java
건설 자 모드 1
@Data
@NoArgsConstructor
@AllArgsConstructor
public class ComplexClass {
    private String variableA;
    private String variableB;
    private String variableC;

    public static class Builder {
        private ComplexClass complexClass = new ComplexClass();
        
        public Builder(String variableA, String variableB, String variableC) {
            complexClass.variableA = variableA;
            complexClass.variableB = variableB;
            complexClass.variableC = variableC;
        }       
        
        public Builder variableA(String variableA) {
            complexClass.variableA = variableA;
            return this;
        }   
        
        public Builder variableB(String variableAB) {
            complexClass.variableB = variableB;
            return this;
        }   
        
        public Builder variableC(String variableC) {
            complexClass.variableC = variableC;
            return this;
        }   

        public ComplexClass build() {
            return complexClass;
        }
    }   
}

건설 자 모드 2
public class ShouQiang {
    private int length;

    public ShouQiang(int length) {
        this.length = length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public void fire() {
        System.out.println("  " + this.length + "       !");
    }
}

public class ShouLiuDan {
    private int length;

    public ShouLiuDan(int length) {
        this.length = length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public void fire() {
        System.out.println("    " + this.length + "        !");
    }
}

public class DaPao {
    private int length;

    public DaPao(int length) {
        this.length = length;
    }

    public void setLength(int length) {
        this.length = length;
    }

    public void fire() {
        System.out.println("  " + this.length + "       !");
    }
}

public class Weapon {

    ShouQiang shouQiang;

    ShouLiuDan shouLiuDan;

    DaPao daPao;

    public void fire() {
        System.out.println("    !!!");
        shouLiuDan.fire();
        shouLiuDan.fire();
        daPao.fire();
        System.out.println("     ,     !!!");
    }

}

public interface WarFactory {

    void setShouQiang(int length);

    void setShouliuDan(int length);

    void setDaPao(int length);

    Weapon getWeapon();
}

public class SmallWar implements WarFactory {

    private Weapon weapon = new Weapon();
    @Override
    public void setShouQiang(int length) {
        weapon.shouQiang = new ShouQiang(length);
    }

    @Override
    public void setShouliuDan(int length) {
        weapon.shouLiuDan = new ShouLiuDan(length);
    }

    @Override
    public void setDaPao(int length) {
        weapon.daPao = new DaPao(length);
    }

    @Override
    public Weapon getWeapon() {
        return weapon;
    }
}

public interface App {

    public static void main(String[] args) {
        WarFactory war = new SmallWar();
        war.setShouQiang(100);
        war.setShouliuDan(1000);
        war.setDaPao(10000);
        final Weapon weapon = war.getWeapon();
        weapon.fire();
    }

}

좋은 웹페이지 즐겨찾기