zoukankan      html  css  js  c++  java
  • Java学习---常见的模式

    Java的常见模式

    适配器模式

     1 package com.huawei;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 
     7 import java.util.Observable;
     8 import java.util.Observer;
     9 
    10 interface Window
    11 {
    12     public  void open();
    13     public  void close();
    14     public  void active();
    15 }
    16 
    17 abstract class WindowAdapter implements Window
    18 {
    19     public  void open(){}
    20     public  void close(){}
    21     public  void active(){}
    22 }
    23 
    24 class WindowImpl extends WindowAdapter
    25 {
    26     public  void open()
    27     {
    28         System.out.println("Open.......");
    29     }
    30     public  void close()
    31     {
    32         System.out.println("Close.......");
    33     }
    34     public  void active()
    35     {
    36         System.out.println("Active.......");
    37     }
    38 }
    39 
    40 
    41 public class ForNumber
    42 {
    43     public static void main(String args[])
    44     {
    45        Window win = new WindowImpl();
    46        win.open();
    47        win.close();
    48     }
    49 }
    View Code

    工厂模式

     1 package com.huawei;
     2 
     3 import java.io.BufferedReader;
     4 import java.io.IOException;
     5 import java.io.InputStreamReader;
     6 
     7 import java.util.Observable;
     8 import java.util.Observer;
     9 
    10 interface Fruit
    11 {
    12     public void eat();
    13 }
    14 
    15 class Apple implements Fruit
    16 {
    17 
    18     public void eat()
    19     {
    20         System.out.println("Eat Apple");
    21     }
    22     
    23 }
    24 
    25 
    26 class Orange implements Fruit
    27 {
    28     public void eat()
    29     {
    30         System.out.println("Eat Orange");
    31     }
    32 }
    33 //定义工厂
    34 class Factory
    35 {
    36     public static Fruit getInstance(String className)
    37     {   
    38         Fruit f = null;
    39         if ("apple".equals(className))
    40         {
    41             f = new Apple();  
    42         }
    43         
    44         if ("orange".equals(className))
    45         {
    46             f = new Orange();  
    47         }
    48         
    49         return f;
    50     }
    51 }
    52 
    53 public class ForNumber
    54 {
    55     public static void main(String args[])
    56     {
    57        Fruit f = null;                      //定义接口对象
    58        f = new Factory().getInstance("apple");
    59        f.eat();
    60     }
    61 }
    View Code

     代理模式

     1 package com.ftl.xmlparse;
     2 
     3 interface Network
     4 {
     5     public void browse();
     6 }
     7 
     8 class Real implements Network
     9 {
    10 
    11     public void browse()
    12     {
    13         System.out.println("Real Connection...");
    14     }
    15 
    16 }
    17 
    18 class Proxy implements Network
    19 {
    20     private Network network;
    21 
    22     public Proxy(Network network)
    23     {
    24         this.network = network;
    25     }
    26 
    27     public void check()
    28     {
    29         System.out.println("Checking...");
    30     }
    31 
    32     public void browse()
    33     {
    34         this.check();
    35         this.network.browse();
    36     }
    37 
    38 }
    39 
    40 public class TestDemo
    41 {
    42     public static void main(String[] args)
    43     {
    44         Proxy proxy = new Proxy(new Real());
    45         proxy.browse();
    46         System.out.println("-------------");
    47         Network net = new Proxy(new Real());
    48         net.browse();
    49     }
    50 }
    View Code

    单例模式

     1 package com.ftl.xmlparse;
     2 //  懒汉法: 来一个开一个内存地址空间
     3 class Singleton{
     4     public static Singleton single = null; 
     5     private Singleton(){};
     6     
     7     public static Singleton getSingleton(){
     8         single = new Singleton();
     9         return single;
    10     }
    11 }
    12 
    13 //  饿汉法: 线程不安全
    14 class Singleton1 {
    15     public static Singleton1 single = null; 
    16     private Singleton1(){};
    17     
    18     public static Singleton1 getSingleton1(){
    19         if(single != null){
    20             single = new Singleton1();
    21         }
    22         return single;
    23     }
    24 }
    25 
    26 //  轮番法: 线程安全,却效率低
    27 class Singleton2 {
    28     public static Singleton2 single = null; 
    29     private Singleton2(){};
    30     
    31     public static Singleton2 getSingleton2(){
    32         synchronized (Singleton1.single) {
    33             if(single != null){
    34                 single = new Singleton2();
    35             }
    36         }
    37         return single;
    38     }
    39 }
    40 //  轮番法: 线程安全,却效率低
    41 class Singleton4 {
    42     public static Singleton4 single = null; 
    43     private Singleton4(){};
    44     
    45     public static Singleton4 getSingleton4(){
    46         if(single != null){
    47             synchronized (Singleton1.single) {
    48                 single = new Singleton4();
    49             }
    50         }
    51         return single;
    52     }
    53 }
    54 
    55 // 双重校验锁
    56 // volatile:一般的变量是在寄存器内,一个变量一个寄存器,添加了volatile关键字后,变量保存在内存中
    57 // 告诉系统直接从内从中获取变量的数值,且变量的更改会通知到每个线程,确保了数据的安全
    58 // 且不允许编译系统优化代码,必须按照代码的顺序编译
    59 class Singleton3 {
    60     public static volatile Singleton3 single = null; 
    61     private Singleton3(){};
    62     public static Singleton3 getSingleton3(){
    63         if(single == null){
    64             synchronized (Singleton1.single) {
    65                 if(single == null){
    66                     single = new Singleton3();
    67                 }
    68             }
    69         }
    70             return single;
    71     }
    72 }
    73     
    74 
    75 public class TestDemo
    76 {
    77     public static void main(String[] args)
    78     {
    79         Singleton  singleton = Singleton.getSingleton();
    80         Singleton1 singleton1 = Singleton1.getSingleton1();
    81         Singleton2 singleton2 = Singleton2.getSingleton2();
    82         Singleton3 singleton3 = Singleton3.getSingleton3();
    83         Singleton4 singleton4 = Singleton4.getSingleton4();
    84     }
    85 }
    View Code

    生产者消费者模式

      1 package com.ftl;
      2 
      3 import java.util.HashMap;
      4 import java.util.Map;
      5 /**
      6  * 生产者和消费者模型
      7  *
      8  * @author 小a玖拾柒
      9  * Date:   2018年8月18日  
     10  *
     11  */
     12 
     13 class Info{
     14     private String name;
     15     private String content;
     16     private boolean flag = false;
     17     
     18     public String getName() {
     19         return name;
     20     }
     21 
     22     public void setName(String name) {
     23         this.name = name;
     24     }
     25 
     26     public String getContent() {
     27         return content;
     28     }
     29 
     30     public void setContent(String content) {
     31         this.content = content;
     32     }
     33 
     34     public synchronized void set(String name, String content){
     35 //        System.out.println("进入到set函数()...");
     36         if(flag){
     37             try {
     38                 super.wait();
     39             } catch (InterruptedException e) {
     40                 // TODO Auto-generated catch block
     41                 e.printStackTrace();
     42             }
     43         }
     44         this.setName(name);
     45         try {
     46             Thread.sleep(1000);
     47         } catch (InterruptedException e) {
     48             // TODO Auto-generated catch block
     49             e.printStackTrace();
     50         }
     51         this.setContent(content);
     52         this.flag = true;
     53         super.notify();
     54     }
     55     
     56     public synchronized void get(){
     57 //        System.out.println("进入到get函数()...");
     58         if(!flag){
     59             try {
     60                 super.wait();
     61             } catch (InterruptedException e) {
     62                 // TODO Auto-generated catch block
     63                 e.printStackTrace();
     64             }
     65         }
     66         try {
     67             Thread.sleep(1000);
     68         } catch (InterruptedException e) {
     69             // TODO Auto-generated catch block
     70             e.printStackTrace();
     71         }
     72         System.out.println("Name:" + this.getName() + ", Content:" + this.getContent());
     73         this.flag = false;
     74         super.notify();
     75     }
     76 }
     77 
     78 class Producer implements Runnable{
     79     private Info info;
     80     private boolean flag = true;
     81     public Producer(Info info){
     82         this.info = info;
     83     }
     84     @Override
     85     public void run() {
     86         // TODO Auto-generated method stub
     87         for(int i = 0 ; i < 10; i++){
     88             if(flag){
     89                 this.info.set("A", "AAAA");
     90                 this.flag = false;
     91             }else{
     92                 this.info.set("B", "BBBB");
     93                 this.flag = true;
     94             }
     95         }
     96     }
     97     
     98     
     99 }
    100 
    101 class Consumer implements Runnable{
    102     private Info info;
    103     
    104     public Consumer(Info info){
    105         this.info = info;
    106     }
    107     @Override
    108     public void run() {
    109         for(int i = 0 ; i < 10; i++){
    110             this.info.get();
    111         }    
    112     }
    113 }
    114 public class Test2018 {
    115     public static void main(String[] args) {
    116         Info info = new Info();
    117         Consumer con = new Consumer(info);
    118         Producer pro = new Producer(info);
    119         new Thread(con).start();
    120         new Thread(pro).start();
    121     }
    122 }
    View Code

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

  • 相关阅读:
    快速创建一个 Servlet 项目(1)
    快速创建一个 Servlet 项目(2)
    多级派生情况下派生类的构造函数
    最近看了点C++,分享一下我的进度吧!
    进程同步&进程间通信
    multiprocess模块
    进程
    网络编程之socket
    网络通信原理
    网络通信的流程 | 初始socket
  • 原文地址:https://www.cnblogs.com/ftl1012/p/javamodel.html
Copyright © 2011-2022 走看看