Java學習---常見的模式

來源:互聯網
上載者:User

標籤:isp   pen   rgs   ret   win   tst   exce   open   bsp   

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 Window11 {12     public  void open();13     public  void close();14     public  void active();15 }16 17 abstract class WindowAdapter implements Window18 {19     public  void open(){}20     public  void close(){}21     public  void active(){}22 }23 24 class WindowImpl extends WindowAdapter25 {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 ForNumber42 {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 Fruit11 {12     public void eat();13 }14 15 class Apple implements Fruit16 {17 18     public void eat()19     {20         System.out.println("Eat Apple");21     }22     23 }24 25 26 class Orange implements Fruit27 {28     public void eat()29     {30         System.out.println("Eat Orange");31     }32 }33 //定義工廠34 class Factory35 {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 ForNumber54 {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

 

Java學習---常見的模式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.