Java的常见模式

适配器模式

 package com.huawei;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import java.util.Observable;
import java.util.Observer; interface Window
{
public void open();
public void close();
public void active();
} abstract class WindowAdapter implements Window
{
public void open(){}
public void close(){}
public void active(){}
} class WindowImpl extends WindowAdapter
{
public void open()
{
System.out.println("Open.......");
}
public void close()
{
System.out.println("Close.......");
}
public void active()
{
System.out.println("Active.......");
}
} public class ForNumber
{
public static void main(String args[])
{
Window win = new WindowImpl();
win.open();
win.close();
}
}

工厂模式

 package com.huawei;

 import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader; import java.util.Observable;
import java.util.Observer; interface Fruit
{
public void eat();
} class Apple implements Fruit
{ public void eat()
{
System.out.println("Eat Apple");
} } class Orange implements Fruit
{
public void eat()
{
System.out.println("Eat Orange");
}
}
//定义工厂
class Factory
{
public static Fruit getInstance(String className)
{
Fruit f = null;
if ("apple".equals(className))
{
f = new Apple();
} if ("orange".equals(className))
{
f = new Orange();
} return f;
}
} public class ForNumber
{
public static void main(String args[])
{
Fruit f = null; //定义接口对象
f = new Factory().getInstance("apple");
f.eat();
}
}

 代理模式

 package com.ftl.xmlparse;

 interface Network
{
public void browse();
} class Real implements Network
{ public void browse()
{
System.out.println("Real Connection...");
} } class Proxy implements Network
{
private Network network; public Proxy(Network network)
{
this.network = network;
} public void check()
{
System.out.println("Checking...");
} public void browse()
{
this.check();
this.network.browse();
} } public class TestDemo
{
public static void main(String[] args)
{
Proxy proxy = new Proxy(new Real());
proxy.browse();
System.out.println("-------------");
Network net = new Proxy(new Real());
net.browse();
}
}

单例模式

 package com.ftl.xmlparse;
// 懒汉法: 来一个开一个内存地址空间
class Singleton{
public static Singleton single = null;
private Singleton(){}; public static Singleton getSingleton(){
single = new Singleton();
return single;
}
} // 饿汉法: 线程不安全
class Singleton1 {
public static Singleton1 single = null;
private Singleton1(){}; public static Singleton1 getSingleton1(){
if(single != null){
single = new Singleton1();
}
return single;
}
} // 轮番法: 线程安全,却效率低
class Singleton2 {
public static Singleton2 single = null;
private Singleton2(){}; public static Singleton2 getSingleton2(){
synchronized (Singleton1.single) {
if(single != null){
single = new Singleton2();
}
}
return single;
}
}
// 轮番法: 线程安全,却效率低
class Singleton4 {
public static Singleton4 single = null;
private Singleton4(){}; public static Singleton4 getSingleton4(){
if(single != null){
synchronized (Singleton1.single) {
single = new Singleton4();
}
}
return single;
}
} // 双重校验锁
// volatile:一般的变量是在寄存器内,一个变量一个寄存器,添加了volatile关键字后,变量保存在内存中
// 告诉系统直接从内从中获取变量的数值,且变量的更改会通知到每个线程,确保了数据的安全
// 且不允许编译系统优化代码,必须按照代码的顺序编译
class Singleton3 {
public static volatile Singleton3 single = null;
private Singleton3(){};
public static Singleton3 getSingleton3(){
if(single == null){
synchronized (Singleton1.single) {
if(single == null){
single = new Singleton3();
}
}
}
return single;
}
} public class TestDemo
{
public static void main(String[] args)
{
Singleton singleton = Singleton.getSingleton();
Singleton1 singleton1 = Singleton1.getSingleton1();
Singleton2 singleton2 = Singleton2.getSingleton2();
Singleton3 singleton3 = Singleton3.getSingleton3();
Singleton4 singleton4 = Singleton4.getSingleton4();
}
}

生产者消费者模式

 package com.ftl;

 import java.util.HashMap;
import java.util.Map;
/**
* 生产者和消费者模型
*
* @author 小a玖拾柒
* Date: 2018年8月18日
*
*/ class Info{
private String name;
private String content;
private boolean flag = false; public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public String getContent() {
return content;
} public void setContent(String content) {
this.content = content;
} public synchronized void set(String name, String content){
// System.out.println("进入到set函数()...");
if(flag){
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.setName(name);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
this.setContent(content);
this.flag = true;
super.notify();
} public synchronized void get(){
// System.out.println("进入到get函数()...");
if(!flag){
try {
super.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Name:" + this.getName() + ", Content:" + this.getContent());
this.flag = false;
super.notify();
}
} class Producer implements Runnable{
private Info info;
private boolean flag = true;
public Producer(Info info){
this.info = info;
}
@Override
public void run() {
// TODO Auto-generated method stub
for(int i = 0 ; i < 10; i++){
if(flag){
this.info.set("A", "AAAA");
this.flag = false;
}else{
this.info.set("B", "BBBB");
this.flag = true;
}
}
} } class Consumer implements Runnable{
private Info info; public Consumer(Info info){
this.info = info;
}
@Override
public void run() {
for(int i = 0 ; i < 10; i++){
this.info.get();
}
}
}
public class Test2018 {
public static void main(String[] args) {
Info info = new Info();
Consumer con = new Consumer(info);
Producer pro = new Producer(info);
new Thread(con).start();
new Thread(pro).start();
}
}

[生产者消费者更多参考] https://www.cnblogs.com/chentingk/p/6497107.html

最新文章

  1. spring框架详解: IOC装配Bean
  2. xcrun: error: active developer path (&quot;/XX&quot;) does not exist
  3. 【转】Ubuntu网卡配置
  4. MSSQL系统进程锁GHOST CLEANUP
  5. oracle 表空间使用情况
  6. 【poj3693】Maximum repetition substring(后缀数组+RMQ)
  7. Linux内核实现多路镜像流量聚合和复制
  8. 【js 编程艺术】小制作一
  9. 决策树(ID3 )原理及实现
  10. 深入理解Java 虚拟机阅读笔记(一)
  11. JS学习过程中碰到的小问题
  12. jQuery的下拉选select2插件用法
  13. IDEA cannot resolve symbol “xxxx”
  14. 一种HBase表数据迁移方法的优化
  15. jeecg-org.jeecgframework.web.system.listener.InitListener
  16. 〖Linux〗Kubuntu14.04 平滑字体的设置
  17. vue2.0的学习
  18. php PDO判断连接是否可用的方法
  19. 读书笔记_Effective_C++_条款二十七:尽量少做转型动作
  20. java文件系统中的的NIO与IO

热门文章

  1. 数据库~Mysql派生表注意的几点~关于百万数据的慢查询问题
  2. Linux下设置Tomcat开机启动
  3. sizeof数组名和字符指针是有区别的
  4. UVM_INFO
  5. ASP.NET MVC Core的TagHelper(基础篇)
  6. java调用ruby代码
  7. js中进行金额计算
  8. JavaScript内置对象与原生对象【转】
  9. ios开发 学习积累20161101
  10. 17、多线程 (Thread、线程创建、线程池)