三种设计模式的实现

1.消费者生产者模式

 1 import java.util.PriorityQueue;
 2 import java.util.Queue;
 3 
 4 public class ProducerAndConsumer {
 5     private final int capacity = 100;
 6     Queue<Integer> que = new PriorityQueue<>(capacity);
 7     public static void main(String[] args) {
 8         ProducerAndConsumer p = new ProducerAndConsumer();
 9         Thread t1 = new Thread(p.new Producer());
10         Thread t2 = new Thread(p.new Consumer());
11         t1.start();
12         t2.start();
13     }
14     class Producer implements Runnable{        
15         @Override
16         public void run() {
17             // TODO Auto-generated method stub
18             while (true) {
19                 synchronized (que) {
20                     while (que.size() == capacity) {
21                         try {
22                             wait();
23                         } catch (InterruptedException e) {
24                             // TODO Auto-generated catch block
25                             e.printStackTrace();
26                             notify();
27                         }
28                     }
29                     que.offer(1);
30                     notify();
31                 }                
32             }            
33         }
34     }
35     class Consumer implements Runnable{
36         public void run() {
37             while (true) {
38                 synchronized (que) {
39                     while (que.size() == 0) {
40                         try {
41                             wait();
42                         } catch (InterruptedException e) {
43                             // TODO Auto-generated catch block
44                             e.printStackTrace();
45                             notify();
46                         }
47                     }
48                     que.poll();
49                     notify();
50                 }                
51             }            
52         }        
53     }    
54 }
View Code

2.单例模式

需要注意构造方法需要设置为private,防止类在类外被实例化,获得类实例的唯一方法是通过getInstance();然后就是多线程加锁的处理。

 1 package 单例模式;
 2 
 3 /*饿汉式*/
 4 /*
 5 public class Singleton {
 6     private static final Singleton sg = new Singleton();
 7     private Singleton() {
 8         
 9     }
10     public static Singleton getInstance() {
11         return sg;
12     }
13 }
14 */
15 /*懒汉式*/
16 //线程不安全,多线程会出现多个实例
17 /*
18 public class Singleton {
19     private static Singleton sg;
20     private Singleton() {
21         
22     }
23     public static Singleton getInstance() {
24         if (sg == null) {
25             return new Singleton();
26         }        
27         return sg;
28     }
29 }
30 */
31 //多线程懒汉模式
32 public class Singleton {
33     private volatile static Singleton sg;
34     private Singleton() {
35         
36     }
37     public static Singleton getInstance() {
38         if (sg == null) {
39             synchronized (sg.getClass()) {
40                 if (sg == null) {//不加这一行还是会造成线程不安全
41                     return new Singleton();
42                 }
43             }            
44         }        
45         return sg;
46     }
47 }
View Code

 

3.工厂模式

http://blog.csdn.net/jason0539/article/details/23020989

http://blog.csdn.net/jason0539/article/details/44976775

转载于:https://www.cnblogs.com/fisherinbox/p/6689323.html

23种设计模式java实现。创建型 抽象工厂模式 http://www.cnblogs.com/java-my-life/archive/2012/03/28/2418836.html 工厂方法 http://www.cnblogs.com/java-my-life/archive/2012/03/25/2416227.html 建造者模式 http://www.cnblogs.com/java-my-life/archive/2012/04/07/2433939.html 原型模式 http://www.cnblogs.com/java-my-life/archive/2012/04/11/2439387.html 单态模式 http://www.cnblogs.com/java-my-life/archive/2012/03/31/2425631.html 结构型 适配器模式 http://www.cnblogs.com/java-my-life/archive/2012/04/13/2442795.html 桥接模式 http://blog.csdn.net/jason0539/article/details/22568865 组合模式 http://blog.csdn.net/jason0539/article/details/22642281 外观模式 http://blog.csdn.net/jason0539/article/details/22775311 装饰者模式 http://www.cnblogs.com/java-my-life/archive/2012/04/20/2455726.html 享元模式 http://www.cnblogs.com/java-my-life/archive/2012/04/26/2468499.html 代理模式 http://www.cnblogs.com/java-my-life/archive/2012/04/23/2466712.html 行为型 责任链模式 http://blog.csdn.net/zhouyong0/article/details/7909456 命令模式 http://www.cnblogs.com/java-my-life/archive/2012/06/01/2526972.html 解释器模式 http://www.cnblogs.com/java-my-life/archive/2012/06/19/2552617.html 迭代模式 http://www.cnblogs.com/java-my-life/archive/2012/05/22/2511506.html 中介者模式 http://blog.csdn.net/chenhuade85/article/details/8141831 备忘录模式 http://www.cnblogs.com/java-my-life/archive/2012/06/06/2534942.html 观察者模式 http://www.cnblogs.com/java-my-life/archive/2012/05/16/2502279.html 状态模式 http://www.cnblogs.com/java-my-life/archive/2012/06/08/2538146.html 策略模式 http://www.cnblogs.com/java-my-life/archive/2012/05/10/2491891.html 模板方法模式 http://www.cnblogs.com/java-my-life/archive/2012/05/14/2495235.html 访问者模式 http://www.cnblogs.com/java-my-life/archive/2012/06/14/2545381.html
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值