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

Java学习---常见的模式的更多相关文章

  1. java运行时内存模式学习

    学习java运行时内存模式: 各区介绍: 方法区(线程共享):用于存放被虚拟机加载的类的元数据:静态变量,常量,以及编译和的代码(字节码),也称为永久代(所有该类的实例被回收,或者此类classLoa ...

  2. Java设计模式学习记录-状态模式

    前言 状态模式是一种行为模式,用于解决系统中复杂的对象状态转换以及各个状态下的封装等问题.状态模式是将一个对象的状态从该对象中分离出来,封装到专门的状态类中,使得对象的状态可以灵活多变.这样在客户端使 ...

  3. Java设计模式学习之工厂模式

    在Java(或者叫做面向对象语言)的世界中,工厂模式被广泛应用于项目中,也许你并没有听说过,不过也许你已经在使用了.Java 设计模式之工厂模式 简单来说,工厂模式的出现源于增加程序序的可扩展性,降低 ...

  4. Java学习笔记——Java工厂模式之简单工厂

    package com.app; import java.util.Date; /* * 工厂模式:简单工厂.工厂方法.抽象工厂 * * */ public class Test0718_Factor ...

  5. Java 学习

    effective+java第三版 2016-09-23 15:25 effective+java第三版 相关问答 EffectiveJava第28条中关于泛型的一个疑问? @又名耶稣谢邀 一.先说说 ...

  6. 转:Java学习路线图,专为新手定制的Java学习计划建议

    转自:http://blog.csdn.net/jinxfei/article/details/5545874 怎么学习Java,这是很多新手经常会问我的问题,现在我简单描述下一个Java初学者到就业 ...

  7. java学习路线(好资源大家分享)

    对于入门java将近两年的时间,曾经迷惘过,一直想知道java的具体学习路线,看过了许许多多的java经验分享的帖子,评论,以及其他各种培训机构所谓的学习路线,发现没有一个符合我个人需求的学习路线,根 ...

  8. 一份Java学习路线图

    Java学习路线图 本文会持续更新,敬请关注.下划线的文字会在这个博客继续更新. 回顾自己的Java学习经历,从学校的课堂到现在每天的项目开发,一份路线路线图浮出来. 其中有未做好的地方,也有自我感觉 ...

  9. Java学习笔记4

    Java学习笔记4 1. JDK.JRE和JVM分别是什么,区别是什么? 答: ①.JDK 是整个Java的核心,包括了Java运行环境.Java工具和Java基础类库. ②.JRE(Java Run ...

随机推荐

  1. Javac之Environment

    关于Env的源代码如下: /** A class for environments, instances of which are passed as * arguments to tree visi ...

  2. Javac之关于方法的选择

    15.12. Method Invocation Expressions 15.12.1. Compile-Time Step 1: Determine Class or Interface to S ...

  3. 虹软人脸识别在 linux中so文件加载不到的问题

    其实是可以加载到的,不过是so文件放的位置不一对,最简单的方式是放在 /usr/lib64 目录下,也可自己设置. so文件加载不到会报这个错误:    .lang.UnsatisfiedLinkEr ...

  4. rails 中 create, new, build, save 的用法以及误区汇总 (转)

    自己很初级,初级的不能再初级,所以初次接触rails的时候,对于里面的create,new,build等方法不是很了解,用的很混乱,导致经常出现不必要的bug,很苦恼,决定,总结一下,结合网上已有资源 ...

  5. 问题集录--TensorFlow深度学习

    TensorFlow深度学习框架 Google不仅是大数据和云计算的领导者,在机器学习和深度学习上也有很好的实践和积累,在2015年年底开源了内部使用的深度学习框架TensorFlow. 与Caffe ...

  6. OpenStack概述

    OpenStack OpenStack is a cloud operating system that controls large pools of compute, storage, and n ...

  7. Html.DropDownList传值

    Html.DropDownList传值: 可以传入明确的IEnumerable<SelectListItem>,也可以通过ViewBag或者ViewData隐式地传入,前提是需要相同的名称 ...

  8. Win7系统下网站发布IIS配置

    *本帖为个人收集贴,所有版权归:西门的后花园 http://ons.me* Technorati 标记: IIS,网站,发布,配置 一.首先是安装IIS.打开控制面板,找到“程序与功能”,点进去 二. ...

  9. 怎样求逆序对数(Inverse Number)?

    #返回上一级 @Author: 张海拔 @Update: 2014-01-14 @Link: http://www.cnblogs.com/zhanghaiba/p/3520089.html /* * ...

  10. 六、阻塞队列LinkedBlockQueue

    一.简介 Java提供了FIFO先进先出的阻塞队列实现,这其实是一种生产者消费者理念,可以通过阻塞队列将生产者和消费者进行解耦合. LinkedBlockQueue是一种无界队列,但事实上它只是队列可 ...