架構設計-第三週作業

請在草稿紙上手寫一個單例模式的實現代碼,拍照提交作業。

雙重檢測

/**
 * @author liuwenxue
 * @date 2020-06-23
 */
public class DoubleLockSingleton {

    private static volatile DoubleLockSingleton instance;

    private DoubleLockSingleton() {
    }

    public static DoubleLockSingleton getInstance() {
        DoubleLockSingleton tmp = null;
        if (tmp == null) {
            synchronized (DoubleLockSingleton.class) {
                tmp = instance;
                if (tmp == null) {
                    tmp = new DoubleLockSingleton();
                    instance = tmp;
                }
            }
        }
        return instance;
    }
}

枚舉

/**
 * @author liuwenxue
 * @date 2020-06-23
 */
public enum EnumSingleton {
    INSTANCE;

    static EnumSingleton getInstance() {
        return INSTANCE;
    }
}

內部靜態類

/**
* @author liuwenxue
* @date 2020-06-23
*/
public class InternalSingleton {

    private InternalSingleton() {
    }

    private static class InstanceHolder {
        private static InternalSingleton instance = new InternalSingleton();
    }

    public InternalSingleton getInstance() {
        return InstanceHolder.instance;
    }
}

請用組合設計模式編寫程序,打印輸出圖 1 的窗口,窗口組件的樹結構如圖 2 所示,打印輸出示例參考圖 3。


/**
 * @author liuwenxue
 * @date 2020-06-23
 */
public interface Printable {
    void print();
}

/**
 * @author liuwenxue
 * @date 2020-06-24
 */
public interface Component extends Printable {
    void addComponent(Printable component);
}

/**
* @author liuwenxue
* @date 2020-06-24
*/
public class AbstractComponent implements Component {
    private String name;

    private List<Printable> subComponent = new ArrayList<>();

    AbstractComponent(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    @Override
    public void addComponent(Printable component) {
        this.subComponent.add(component);
    }

    @Override
    public void print() {
        System.out.println(this.name);
        if (!subComponent.isEmpty()) {
            System.out.println("child component:");
        }
        for (Printable com : subComponent) {
            com.print();
        }
    }
}

/**
* @author liuwenxue
* @date 2020-06-23
*/
public class WinForm extends AbstractComponent {
    WinForm(String name) {
        super(name);
    }
}

/**
 * @author liuwenxue
 * @date 2020-06-23
 */
public class Button extends AbstractComponent {
    Button(String name) {
        super(name);
    }
}

/**
 * @author liuwenxue
 * @date 2020-06-24
 */
public class CheckBox extends AbstractComponent {
    CheckBox(String name) {
        super(name);
    }
}

/**
 * @author liuwenxue
 * @date 2020-06-23
 */
public class Frame extends AbstractComponent {
    Frame(String name) {
        super(name);
    }
}

/**
 * @author liuwenxue
 * @date 2020-06-24
 */
public class Label extends AbstractComponent {
    Label(String name) {
        super(name);
    }
}

/**
 * @author liuwenxue
 * @date 2020-06-24
 */
public class LinkLable extends AbstractComponent {
    LinkLable(String name) {
        super(name);
    }
}

/**
 * @author liuwenxue
 * @date 2020-06-24
 */
public class PasswordBox extends AbstractComponent {
    PasswordBox(String name) {
        super(name);
    }
}

/**
 * @author liuwenxue
 * @date 2020-06-23
 */
public class Picture extends AbstractComponent {
    Picture(String name) {
        super(name);
    }
}

/**
 * @author liuwenxue
 * @date 2020-06-24
 */
public class TextBox extends AbstractComponent {
    TextBox(String name) {
        super(name);
    }
}




/**
* @author liuwenxue
* @date 2020-06-24
*/
public class Client {

    public static void main(String[] args) {
        Component winForm = new WinForm("WINDOW 窗口");
        Component picture = new Picture("LOGO 圖片");
        Component signIn = new Button("登錄");
        Component signUp = new Button("註冊");
        Component frame = new Frame("FRAME1");
        Component userName = new Label("用戶名");
        Component textBox = new TextBox("文本框");
        Component password = new Label("密碼");
        Component passwordBox = new PasswordBox("密碼框");
        Component checkBox = new CheckBox("複選框");
        Component LinkLabel = new LinkLable("忘記密碼");
        Component rememberMe = new TextBox("記住用戶名");
        winForm.addComponent(picture);
        winForm.addComponent(signIn);
        winForm.addComponent(signUp);
        winForm.addComponent(frame);
        frame.addComponent(userName);
        frame.addComponent(password);
        frame.addComponent(textBox);
        frame.addComponent(passwordBox);
        frame.addComponent(checkBox);
        frame.addComponent(LinkLabel);
        frame.addComponent(rememberMe);
        winForm.print();
    }
}

根據當週學習情況,完成一篇學習總結

總體課程中規中矩,對於已經有一定設計模式基礎的來說,課程基本沒有壓力。當前課程定位爲架構入門課,略失望。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章