设计模式并不是一种算法,而是一种思想,一种软件开发思想;这种思想便于开发的维护、扩展。
/**
* @description: 1.1多线程单列-懒汉式-用时再创建
* @author: zoutai
* @create: 2019/4/11
**/
public class SingletonCon {
// 全局访问、volatile禁止重排序
private volatile static SingletonCon instance;
// 构造函数私有
private SingletonCon(){}
// 实例化函数类函数
public static SingletonCon getInstance(){
// 判断是否为null,只有为null是才加锁,否则不加锁,直接返回实例;提高效率
if(instance==null){
synchronized (SingletonCon.class){
// 在上一步判断为null和加锁期间,其他线程可能已经实例化了对象,所以需要再次判断
if(instance==null){
instance = new SingletonCon();
}
}
}
return instance;
}
}
/**
*
* 补充说明volatile禁止重排序:对于构造语句 instance = new SingletonCon();
* 在构造时,CPU会对进行语句优化,进行重排序,先对instance赋值,然后才实例化对象;这样就instance不为null,但是却没有实例化对象。
内部重排序指令如下:
inst = allocat(); // 分配内存
sSingleton = inst; // 赋值
constructor(inst); // 真正执行构造函数
*
**/
/**
* @description: 1.2 单例模式-饿汉式
* @author: zoutai
* @create: 2019/4/11
**/
public class SingletonStatic {
// 类加载时就初始化,只初始化一次
private static SingletonStatic instance = new SingletonStatic();
private SingletonStatic(){}
public static SingletonStatic getInstance(){
return instance;
}
}
// 2 工厂模式:
// 线程池接口
public interface MyThreadPool {
void createThead();
}
// 第一种线程池
public class SingleThreadPool implements MyThreadPool{
@Override
public void createThead() {
System.out.println("线程个数只有一个的线程池:");
}
}
// 第二种线程池
public class FixedThreadPool implements MyThreadPool{
@Override
public void createThead() {
System.out.println("线程个数固定的线程池:");
}
}
// 工厂创建
public class ThreadFactory {
// 工厂方法创建-参考自Executors类
public MyThreadPool newThread(int num){
if(num==1){
return new SingleThreadPool();
} else {
return new FixedThreadPool();
}
}
}
public class TestFactory {
public static void main(String[] args) {
ThreadFactory threadFactory = new ThreadFactory();
MyThreadPool singleThread = threadFactory.newThread(1);
singleThread.createThead();
MyThreadPool fixedThread = threadFactory.newThread(10);
fixedThread.createThead();
}
}
// 3 抽象工厂模式
// 创建用户工厂
public interface User {
void createUser();
}
public class StudentUser implements User {
@Override
public void createUser() {
System.out.println("学生用户");
}
}
public class TeacherUser implements User{
@Override
public void createUser() {
System.out.println("老师用户");
}
}
// 创建线程池工厂同上(省略)
// 创建抽象工厂,集合所有的工厂
public interface AbstractFactory {
MyThreadPool newThread();
User newUser();
}
public class SingleFactory implements AbstractFactory {
@Override
public MyThreadPool newThread() {
return new FixedThreadPool();
}
@Override
public User newUser() {
return new TeacherUser();
}
}
public class FixedFactory implements AbstractFactory{
@Override
public MyThreadPool newThread() {
return new SingleThreadPool();
}
@Override
public User newUser() {
return new StudentUser();
}
}
public class TestAbstractFactory {
public static void main(String[] args) {
AbstractFactory factory = new SingleFactory();
MyThreadPool singleThread = factory.newThread();
singleThread.createThead();
User user = factory.newUser();
user.createUser();
}
}
// 4.装饰器
// 定义输入流接口
public interface InputStream {
void read();
}
// 文件类型输入流
public class FileInputStream implements InputStream{
@Override
public void read() {
System.out.println("fileInputStream:");
}
}
// 装饰器:为所有的输入流 增添新的功能
public abstract class FilterInputStream implements InputStream{
protected InputStream inputStream;
public FilterInputStream(InputStream inputStream) {
this.inputStream = inputStream;
}
@Override
public void read() {
inputStream.read();
}
}
// 缓冲装饰器:添加具有缓冲功能的装饰器
public class BufferedInputStream extends FilterInputStream {
public BufferedInputStream(InputStream inputStream) {
super(inputStream);
}
@Override
public void read() {
super.read();
readBuffered();
}
private void readBuffered() {
System.out.println("buffered read");
}
}
public class TestDecorator {
public static void main(String[] args) {
FileInputStream fi = new FileInputStream();
System.out.println("1、只有文件读功能:");
fi.read();
System.out.println("-----------");
System.out.println("2、加入缓冲功能");
BufferedInputStream bis = new BufferedInputStream(new FileInputStream());
bis.read();
}
}
// 5 适配器
// 副接口:后置通知模板-参考AOP
// 适配者
public interface AfterAdvice {
void after();
}
// 接口after的拦截器
public class MethodAfterAdvice implements AfterAdvice {
@Override
public void after() {
System.out.println("MethodAfterAdvice:");
}
}
// 主接口
public interface BeforeAdvice {
void before();
}
// 具体的拦截器
public class MathodBeforeAdvice implements BeforeAdvice {
@Override
public void before() {
System.out.println("MathodBeforeAdvice:");
}
}
// 适配器:适配接口通过实例对象调用
public class MethodBeforeAdviceAdapter implements BeforeAdvice{
AfterAdvice afterAdvice;
public MethodBeforeAdviceAdapter(int type) {
if(type>0){
afterAdvice=new MethodAfterAdvice();
}
}
@Override
public void before() {
if(afterAdvice!=null){
afterAdvice.after();
}else {
System.out.println("MathodBeforeAdvice:");
}
}
}
// 6 代理模式
public interface Task {
void dealTask(String taskName);
}
public class PostTask implements Task {
@Override
public void dealTask(String taskName) {
System.out.println("post task :" + taskName);
}
}
// 代理类,实现被代理者的内部逻辑
public class ProxyTask implements Task {
private Task task;
@Override
public void dealTask(String taskName) {
if(task==null){
task = new PostTask();
}
System.out.println("代理间接调用");
task.dealTask(taskName);
}
}
public class TestProxy {
public static void main(String[] args) {
Task task = new ProxyTask();
task.dealTask("打印开始日志");
}
}
// 7 模板类
public abstract class JdbcTemplate {
// 具体的方式自己实现
abstract void getConnection();
abstract void doTask();
abstract void releaseConnection();
// 固定业务逻辑顺序-不可变
public void execute(){
getConnection();
doTask();
releaseConnection();
}
}
public class MyJdbcTemplate extends JdbcTemplate{
@Override
void getConnection() {
System.out.println("getConnection:");
}
@Override
void doTask() {
System.out.println("doTask");
}
@Override
void releaseConnection() {
System.out.println("releaseConnection");
}
}
// 8 策略模式
public interface Strategy {
int cal(int num1,int num2);
}
// 各种策略实现
public class AddStrategy implements Strategy {
@Override
public int cal(int num1, int num2) {
return num1 + num2;
}
}
public class SubStrategy implements Strategy {
@Override
public int cal(int num1, int num2) {
return num1 - num2;
}
}
// 策略类
public class Context {
private Strategy strategy;
public Context(Strategy strategy) {
this.strategy = strategy;
}
public int execute(int num1, int num2){
return strategy.cal(num1,num2);
}
}
public class testStrategy {
public static void main(String[] args) {
Context context = new Context(new AddStrategy());
System.out.println(context.execute(1,2));
context = new Context(new SubStrategy());
System.out.println(context.execute(1,2));
}
}