架构设计-第三周作业

请在草稿纸上手写一个单例模式的实现代码,拍照提交作业。

双重检测
/**
 * @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();
    }
}

根据当周学习情况,完成一篇学习总结

总体课程中规中矩,对于已经有一定设计模式基础的来说,课程基本没有压力。当前课程定位为架构入门课,略失望。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值