設計模式-<工廠模式> – JAVA編程語言程序開發技術文章

package 工廠模式;

public class FactoryPattern {
   
  public static void main(String[] args) {
    ComputerFactory Myfactory=new IBMFactory();
    Myfactory.createCpu();
  }
  /**
    * 註意該工廠接口'隻幹'這些
    * @author Administrator
    */
  interface ComputerFactory{
    void createMac();//這個好像是網卡吧,忘瞭
    void createCpu();
  }
  public static class IBMFactory implements ComputerFactory{

    public void createCpu() {
      System.out.println("\ncreate IBMFactory    Cpu");
    }

    public void createMac() {
      System.out.println("\ncreate IBMFactory    Mac");
    }
  }
  public static class LenovoFactory implements ComputerFactory{

    public void createCpu() {
      System.out.println("\ncreate LenovoFactory    Cpu");
    }

    public void createMac() {
      System.out.println("\ncreate LenovoFactory    Mac");
    }
  }
}

 

本文出自 “飛沙” 博客

發佈留言