写点什么

架构师训练营第三周 -homework

用户头像
张荣召
关注
发布于: 2020 年 10 月 01 日

1. 请手写一个单例模式的实现代码,拍照提交作业。

解:

1.1单例方法1:饿汉模式

public  class Singleton1{

     private Singleton1(){   } 

     private static Singleton1 intance1=new Singleton1(); 

     public static Singleton1 getInstance(){ return intance1;}

}



1.2单例方法2:懒汉模式

public class Singleton2{

     private Singleton2(){   }

     private static Singleton2  instance=null;

     public static  synchronized  Singleton2 getInstance(){ 

           if(instance==null){  instance=new Singleton2();  } 

           return instance;

     }

}

1.3 单例方法3:同步锁模式

public class Singleton3{

     private Object internalLockObj=new Object();

     private Singleton3(){   }

     private static Singleton3  instance=null;

     public static Singleton3 getInstance(){ 

           synchronized(internalLockObj){ 

                if(instance==null){  instance=new Singleton3();  } 

           }

           return instance;

     }

}

1.4 单例方法3:显式加锁模式

public class Singleton4{

     private Lock lock=new ReentrantLock();

     private Singleton4(){   }

     private static Singleton4  instance=null;

     public static Singleton4 getInstance(){ 

lock.lock();

try{

   if(instance==null){  instance=new Singleton4();  }

return instance;

}catch(Exception e){

throw new RuntimeException(e);

}finally{

lock.unlock();

}

     }

}

2. 请用组合设计模式编写程序

2.1类图



2.2代码示例:

/**
* 控件顶层接口
* **/
public interface Control {
String getName();
void addControl(Control control);
void draw();
}

/**
* 简单控件基类
* */
public abstract class AbstractSimpleControl implements Control {
private String name;
@Override
public String getName(){
return this.name;
}
protected AbstractSimpleControl(String name){
this.name=name;
}
@Override
public void addControl(Control control){
throw new UnsupportedOperationException();
}
@Override
public void draw(){
System.out.println("print "+this.getClass().getSimpleName()+"("+this.getName()+")");
}
}

/**
* 容器控件基类
* **/
public abstract class AbstractContainerControl implements Control {
private String name;
private List<Control> controls;

protected AbstractContainerControl(String name){
this.name=name;
this.controls=new ArrayList<Control>();
}
public void addControl(Control control){
this.controls.add(control);
}

public String getName(){
return this.name;
}
@Override
public void draw() {
System.out.println("print "+this.getClass().getSimpleName()+"("+this.getName()+")");

for(Control control:this.controls){
control.draw();
}
}
}

/**
* Button控件
* */
public class Button extends AbstractSimpleControl {
public Button(String name){super(name);}
}

/***
* 复选框
* */
public class CheckBox extends AbstractSimpleControl {
public CheckBox(String name){super(name);}
}

/**
* 标签控件
* **/
public class Label extends AbstractSimpleControl {
public Label(String name){super(name);}
}

public class LinkLabel extends AbstractSimpleControl {
public LinkLable(String name){super(name);}
}

/**
* passwordBox
* **/
public class PasswordBox extends AbstractSimpleControl {
public PasswordBox(String name){super(name);}
}

/**
* 图片控件
* */
public class Picture extends AbstractSimpleControl {
public Picture(String name){super(name);}
}

/**
* 文本输入框控件
* */
public class TextBox extends AbstractSimpleControl {
public TextBox(String name){super(name);}
}

/**
* WinForm容器控件
* ***/
public class WinForm extends AbstractContainerControl {
public WinForm(String name){
super(name);
}
}

/**
* Frame 容器控件
* **/
public class Frame extends AbstractContainerControl {
public Frame(String name){
super(name);
}
}


public class CompositeControlDemo {
public static void main(String[] args) {
Control winForm = new WinForm("WINDOW窗口");

Control picture = new Picture("LOGO图片");
winForm.addControl(picture);

Control loginBtn=new Button("登录");
winForm.addControl(loginBtn);

Control registerBtn=new Button("注册");
winForm.addControl(registerBtn);

Control frame=new Frame("FRAME1");
winForm.addControl(frame);

Control userNameLabel=new Label("用户名");
frame.addControl(userNameLabel);

Control textBox=new TextBox("文本框");
frame.addControl(textBox);

Control passwordLabel=new Label("密码");
frame.addControl(passwordLabel);

Control passwordBox=new PasswordBox("密码框");
frame.addControl(passwordBox);

Control checkbox=new CheckBox("复选框");
frame.addControl(checkbox);

Control rememberMe=new TextBox("记住用户名");
frame.addControl(rememberMe);

Control noPassword=new LinkLable("忘记密码");
frame.addControl(noPassword);

winForm.draw();
}
}



用户头像

张荣召

关注

还未添加个人签名 2018.05.02 加入

还未添加个人简介

评论

发布
暂无评论
架构师训练营第三周-homework