写点什么

单例模式和组合模式

用户头像
王鹏飞
关注
发布于: 2020 年 06 月 21 日
单例模式和组合模式

单例模式

Singleton 模式保证产生单一实例,就是说一个类只生产一个实例。

使用 singleton 有两种原因:

  • 是因为只有一个实例,可以减少实例频繁创建和销毁代理的系统销毁

  • 是当多个用户使用这个实例的时候,便于进行统一控制

手写单列类如图:

顺便了解一下Spring Singleton Scope 的概念

Only one shared instance of a singleton bean is managed, and all requests for beans with an ID or IDs that match that bean definition result in that one specific bean instance being returned by the Spring container.

To put it another way, when you define a bean definition and it is scoped as a singleton, the Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object. The following image shows how the singleton scope works:

  • 声明为单例的bean的对象只会在Spring 容器中存在一个实例,这个实例被存储在一个单例类的缓存集合中。所以的引用将返回缓存中的对象实例。

Spring’s concept of a singleton bean differs from the singleton pattern as defined in the Gang of Four (GoF) patterns book. The GoF singleton hard-codes the scope of an object such that one and only one instance of a particular class is created per ClassLoader. The scope of the Spring singleton is best described as being per-container and per-bean. This means that, if you define one bean for a particular class in a single Spring container, the Spring container creates one and only one instance of the class defined by that bean definition.

  • Spring 中的单例的与设计模式中差别指出在于设计模式中的硬编码方式的单例是jvm的ClassLoader层面的。而Spring 中的单例由Spring 容器根据 bean 和class 的声明创建的唯一的一个实例。

组合模式

  • 是一种“对象的结构模型”

请用组合设计模式编写程序,打印输出图 1 的窗口,窗口组件的树结构如图 2 所示,打印输出示例参考图 3。



Java 代码例子

/**
* 组件接口声明
*/
public interface Component {
void print();
}



public class Button implements Component {
private String msg;
private List<Component> components;
public Button(String msg,List<Component> components){
this.msg = msg;
this.components = components;
}
@Override
public void print() {
System.out.println(msg);
if(components != null){
components.forEach(component -> component.print());
}
}
}



public class CheckBox implements Component {
private String msg;
private List<Component> components;
public CheckBox(String msg,List<Component> components){
this.msg = msg;
this.components = components;
}
@Override
public void print() {
System.out.println(msg);
if(components != null){
components.forEach(component -> component.print());
}
}
}



public class Frame implements Component {
private String msg;
private List<Component> components;
public Frame(String msg,List<Component> components){
this.msg = msg;
this.components = components;
}
@Override
public void print() {
System.out.println(msg);
if(components != null){
components.forEach(component -> component.print());
}
}
}



public class Label implements Component {
private String msg;
private List<Component> components;
public Label(String msg,List<Component> components){
this.msg = msg;
this.components = components;
}
@Override
public void print() {
System.out.println(msg);
if(components != null){
components.forEach(component -> component.print());
}
}
}



public class Linkable implements Component {
private String msg;
private List<Component> components;
public Linkable(String msg,List<Component> components){
this.msg = msg;
this.components = components;
}
@Override
public void print() {
System.out.println(msg);
if(components != null){
components.forEach(component -> component.print());
}
}
}



public class PasswordBox implements Component {
private String msg;
private List<Component> components;
public PasswordBox(String msg,List<Component> components){
this.msg = msg;
this.components = components;
}
@Override
public void print() {
System.out.println(msg);
if(components != null){
components.forEach(component -> component.print());
}
}
}



public class Picture implements Component {
private String msg;
private List<Component> components;
public Picture(String msg,List<Component> components){
this.msg = msg;
this.components = components;
}
@Override
public void print() {
System.out.println(msg);
if(components != null){
components.forEach(component -> component.print());
}
}
}



public class TextBox implements Component {
private String msg;
private List<Component> components;
public TextBox(String msg,List<Component> components){
this.msg = msg;
this.components = components;
}
@Override
public void print() {
System.out.println(msg);
if(components != null){
components.forEach(component -> component.print());
}
}
}



public class WindowForm implements Component {
private String msg;
private List<Component> components;
public WindowForm(String msg,List<Component> components){
this.msg = msg;
this.components = components;
}
@Override
public void print() {
System.out.println(msg);
if(components != null){
components.forEach(component -> component.print());
}
}
}



public class WindowPrint {
public static void main(String[] args) {
/**
* 第一部分:组件声明
*/
Component logo = new Picture("print Picture(LOGO图片)",null);
Component logIn = new Button("print Button(登录)",null);
Component signIn = new WindowForm("print Button(注册)",null);
Component userName = new Label("print Label(用户名)",null);
Component textbox = new TextBox("print TextBox(文本框)",null);
Component passWord = new Label("print Label(密码)",null);
Component passWordBox = new PasswordBox("print PasswordBox(密码框)",null);
Component checkBox = new CheckBox("print CheckBox(复选框)",null);
Component rememberName = new TextBox("print TextBox(记住姓名)",null);
Component forgetName = new Linkable("print Linkable(忘记密码)",null);
/**
* 第二部分:组件组装
*/
List<Component> windowComponent = new ArrayList<>();
windowComponent.add(logo);
windowComponent.add(logIn);
windowComponent.add(signIn);
List<Component> frameComponent = new ArrayList<>();
frameComponent.add(userName);
frameComponent.add(textbox);
frameComponent.add(passWord);
frameComponent.add(passWordBox);
frameComponent.add(checkBox);
frameComponent.add(rememberName);
frameComponent.add(forgetName);
Component frame = new Frame("print Frame(FREAM1)",frameComponent);
windowComponent.add(frame);
Component windowForm = new WindowForm("print WinForm(WINDOW窗口)",windowComponent);
/**
* 第三部分:组件输出
*/
windowForm.print();
}
}

总结

组合模式用处理单个对象的方式处理一组对象,这一组对象以树形结构进行组合。每一个对象中包含一个与自己类型相同的集合。



发布于: 2020 年 06 月 21 日阅读数: 67
用户头像

王鹏飞

关注

还未添加个人签名 2019.06.11 加入

还未添加个人简介

评论

发布
暂无评论
单例模式和组合模式