常用设计模式
来源:互联网 发布:域名是知识产权吗 编辑:程序博客网 时间:2024/06/10 16:50
一、单例模式
单例模式,最常见的就是饥饿模式和懒汉模式,一个直接实例化对象,一个在调用方法时进行实例化对象。在多线程模式中,考虑到性能和线程安全问题,我们一般选下面两种比较经典的单例模式,在性能提高的同时,又保证了线程安全。
(1).static inner class方式单例
(2).double check instance 方式单例
1.静态内部类 方式单例:
public class Singletion { private static class InnerSingletion { private static Singletion single = new Singletion(); } public static Singletion getInstance(){ return InnerSingletion.single; }}
2.双重检索单例:
public class DubbleSingleton { private static DubbleSingleton ds; public static DubbleSingleton getDs(){ if(ds == null){ synchronized (DubbleSingleton.class) { if(ds == null){ ds = new DubbleSingleton(); } } } return ds; }}
饿汉模式:
public class Singleton{ private static Singleton instance = new Singleton(); private Singleton(){} public static Singleton newInstance(){ return instance; } } 缺点,在类加载之后就被创建,即使没有用到
懒汉模式:
public class Singleton{ private static Singleton instance = null; private Singleton(){} public static synchronized Singleton newInstance(){ if(null == instance){ instance = new Singleton(); } return instance; } }在特定时间加载,延迟加载
二、工厂设计模式
1、工厂方法模式(Factory Method)
1.1、普通工厂模式,就是建立一个工厂类,对实现了同一接口的一些类进行实例的创建。首先看下关系图:
举例如下:(我们举一个发送邮件和短信的例子)
首先,创建二者的共同接口:
public interface Sender { public void Send(); }
其次,创建实现类:
public class MailSender implements Sender { @Override public void Send() { System.out.println("this is mailsender!"); } } public class SmsSender implements Sender { @Override public void Send() { System.out.println("this is sms sender!"); } }
最后,建工厂类:
public class SendFactory { public Sender produce(String type) { if ("mail".equals(type)) { return new MailSender(); } else if ("sms".equals(type)) { return new SmsSender(); } else { System.out.println("请输入正确的类型!"); return null; } } }
我们来测试下:
public class FactoryTest { public static void main(String[] args) { SendFactory factory = new SendFactory(); Sender sender = factory.produce("sms"); sender.Send(); } }
输出:this is sms sender!
三、适配器模式
使用一个现成的类,但是它的接口不完全符合你的需求,我只想要它其中的一个方法,不想
覆写其他的方法。
比如,窗体有变大,变小,关闭的行为,但是我现在只需要关闭行为;
package reviewDemo;//适配器模式:只想用其中的某一个方法,用适配器作为中间的过渡interface Windows{ void max(); void min(); void close();}//适配器模式,实现接口所有的方法,但是不写方法体!class AdapterWindows implements Windows{ @Override public void max() { } @Override public void min() { } @Override public void close() { }}class MyWindows extends AdapterWindows{ //覆写父类的方法 public void close(){ System.out.println("这个实现的是关闭功能!"); }}public class Demo17 { public static void main(String[] args) { new MyWindows().close(); }}
阅读全文
0 0
- 常用的设计模式
- 一句话常用设计模式
- 常用设计模式
- 常用设计模式
- 常用设计模式收集
- 常用设计模式收集
- java常用设计模式
- java常用设计模式
- 常用设计模式
- 常用设计模式收集
- Java常用设计模式
- java常用设计模式
- 常用设计模式
- 常用设计模式二
- 常用设计模式3
- 常用设计模式
- java常用设计模式
- java常用设计模式 .
- HTTP Components简介
- [idea中配置maven]
- 09.02 问题解决(初步)
- C++面向对象
- Win7+VB6.0+access数据库出现实时错误“3078”的解决办法
- 常用设计模式
- WCF分布式开发步步为赢(1):WCF分布式框架基础概念
- C++语言中函数参数传递的三种方式
- Linux环境下加载动态链接库
- Linux安装ftp组件
- React 样式: CSS模块化
- windows server2008R2如何安装.net framework4.0
- Linux drwxr-xr-x 的解读
- 使用poi生成excel