設計模式(一):接口型模式介紹

1 接口java

類的接口 (interface) 就是該類容許其餘類對象訪問的方法和字段的集合。接口做爲對象必須實現的承諾。接口永遠不可能被實例化爲對象,所以只能定義虛方法常量字段數組

做用:ui

    限制了對象之間的交互(交互能夠只用interface來完成,interface進行限制)this

與抽象類的區別:spa

    一個類能夠實現(implements)任意多個接口,但只能繼承(extend)一個抽象類。rest

    一個抽象類可有非抽象方法,能夠定義構造器,接口的全部方法都是抽象的。component

    接口只能聲明static final 常量,由於通常成員變量沒法實例化。對象

總之,接口只是一種限制形式blog

 

2 Adapter模式繼承

目的:利用現有的類,知足須要的接口

2.1 接口適配

情形:應用程序須要調用RequiredInterface接口,包含requiredMethod方法,而一個現存的類ExistingClass是一個已有的實現,但接口與應用程序的接口不一致,所以採用接口適配模式

方法:

    定義一個新類NewClass,繼承ExistingClass,並實現RequiredInterface接口。

2.2 對象適配器

情形:應用程序須要調用RequiredClass類,包含requiredMethod()方法,而一個現存的類ExistingClass是一個相同功能的實現

問題:java中只容許單繼承,不能同時繼承RequiredClass和ExisitingClass兩個類。

解決:定義NewClass,繼承RequiredClass(知足應用程序調用要求),幷包含ExistingClass的實例對象做爲成員變量,NewClass在實現requiredMethod()方法時調用ExistingClass實例對象的usefulMethod()方法就能夠了。

2.3 實例:JTable的使用

本例是一個基於對象適配器的模式實例

Javax.swing.JTable類定義了一個常規的二維單元表。構造一個JTable的實例須要實例化成員變量TableModel(接口),爲了方便,java類庫部分實現了接口構造了抽象類AbstractTableModel,只需實現getColumnCount() getRowCount()和getValueAt()三個抽象方法而已,所以選擇對象適配模式

具體表格里填的是什麼,固然是須要用戶類本身定義,而後適配TableModel接口。

能夠看到,適配類RocketTableModel內包含了Rocket實例對象的數組,抽象類的方法均可以經過調用Rocket對象來實現。

 

程序,來自www.oozinoz.com

主程序:

  1. public class ShowRocketTable {
  2.     public static void main(String[] args) {
  3.         setFonts();
  4.         JTable table = new JTable(getRocketTable());
  5.         table.setRowHeight(36);
  6.         JScrollPane pane = new JScrollPane(table);
  7.         pane.setPreferredSize(new java.awt.Dimension(300, 100));
  8.         display(pane, " Rockets");
  9.     }
  10.  
  11.     /**
  12.      * Display a Swing component. We'll refactor this later into a nice facade.
  13.      *
  14.      * @param c the component to display
  15.      * @param title the window title
  16.      */
  17.     public static void display(Component c, String title) {
  18.         JFrame frame = new JFrame(title);
  19.         frame.getContentPane().add(c);
  20.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  21.         frame.pack();
  22.         frame.setVisible(true);
  23.     }
  24.  
  25.     private static RocketTableModel getRocketTable() {
  26.         Rocket r1 = new Rocket("Shooter", 1.0, new Dollars(3.95), 50.0, 4.5);
  27.         Rocket r2 = new Rocket("Orbit", 2.0, new Dollars(29.03), 5000, 3.2);
  28.         return new RocketTableModel(new Rocket[] { r1, r2 });
  29.     }
  30.  
  31.     private static void setFonts() {
  32.         Font font = new Font("Dialog", Font.PLAIN, 18);
  33.         UIManager.put("Table.font", font);
  34.         UIManager.put("TableHeader.font", font);
  35.     }
  36. }

適配類RocketTableModel:

  1. public class RocketTableModel extends AbstractTableModel {
  2.     protected Rocket[] rockets;
  3.     protected String[] columnNames = new String[] { "Name", "Price", "Apogee" };
  4.  
  5.     /**
  6.      * Construct a rocket table from an array of rockets.
  7.      * @param rockets an array of rockets
  8.      */
  9.     public RocketTableModel(Rocket[] rockets) {
  10.         this.rockets = rockets;
  11.     }
  12.  
  13.     /**
  14.       * @return the number of columns in this table.
  15.      */
  16.     public int getColumnCount() {
  17.         return columnNames.length;
  18.     }
  19.  
  20.     /**
  21.      * @param index which column is interesting
  22.      * @return the name of the indicated column
  23.      */
  24.     public String getColumnName(int i) {
  25.         return columnNames[i];
  26.     }
  27.  
  28.     /**
  29.      * @return the number of rows in this table.
  30.      */
  31.     public int getRowCount() {
  32.         return rockets.length;
  33.     }
  34.  
  35.     /**
  36.      * @param row which row is interesting
  37.      * @param col which column is interesting
  38.      * @return the value at the indicated row and column.
  39.      */
  40.     public Object getValueAt(int row, int col) {
  41.         switch (col) {
  42.         case 0:
  43.             return rockets[row].getName();
  44.         case 1:
  45.             return rockets[row].getPrice();
  46.         case 2:
  47.             return new Double(rockets[row].getApogee());
  48.         default:
  49.             return null;
  50.         }
  51.     }
  52. }

原類Rocket:

  1. public class Rocket extends Firework {
  2.     private double apogee;
  3.  
  4.     private double thrust;
  5.  
  6.     /**
  7.      * Allow creation of empty objects to support reconstruction from persistent
  8.      * store.
  9.      */
  10.     public Rocket() {
  11.     }
  12.  
  13.     /**
  14.      * Create a rocket with all its expected properties. See the superclass for
  15.      * descriptions of other parameters
  16.      *
  17.      * @param apogee
  18.      * The height (in meters) that the rocket is expected to reach
  19.      * @param thrust
  20.      * The rated thrust (or force, in newtons) of this rocket
  21.      */
  22.     public Rocket(String name, double mass, Dollars price, double apogee,
  23.             double thrust) {
  24.         super(name, mass, price);
  25.         setApogee(apogee);
  26.         setThrust(thrust);
  27.     }
  28.  
  29.     /**
  30.      * The height (in meters) that the rocket is expected to reach.
  31.      */
  32.     public double getApogee() {
  33.         return apogee;
  34.     }
  35.  
  36.     public void setApogee(double value) {
  37.         apogee = value;
  38.     }
  39.  
  40.     /**
  41.      * The rated thrust (or force, in newtons) of this rocket.
  42.      */
  43.     public double getThrust() {
  44.         return thrust;
  45.     }
  46.  
  47.     public void setThrust(double value) {
  48.         thrust = value;
  49.     }
  50. }

固然,getName() getPrice()這兩個方法在超類Firework中定義。

相關文章
相關標籤/搜索