package shiyan12;

public class Client {
    public static void main(String[] args) {
        MainFrame mainframe = new MainFrame();
     mainframe.on();
    }
}
Client
package shiyan12;

public class CPU {
    public boolean run(){
        System.out.println("CPU自动成功");
        return true;
}
} 
CPU
package shiyan12;

public class HardDisk {
    public boolean read(){
        System.out.println("硬盘读入成功");
        return true;
    }
} 
HardDisk
package shiyan12;

public class MainFrame {
    public Memory memory  = new Memory();
    
      private CPU cpu = new CPU();
   
      private HardDisk hardDisk = new HardDisk();
   
      private OS os = new OS();
   
   
   
      public void on(){
   
          memory.check();
   
          cpu.run();
   
          hardDisk.read();
   
          os.load();
   
      }
  
}
MainFrame
package shiyan12;

public class Memory {
    public boolean check(){
        System.out.println("内存检查成功");
        return true;
    }
} 
Memory
package shiyan12;

public class OS {
    public boolean load(){
        System.out.println("操作系统载入成功");
        return true;
    }
}
OS

 实验13

package test13;

public class Client {
    public static void main(String[] args) {
        IgoChessman black1,black2,black3,white1,white2;
        IgoChessmanFactory factory;
        factory = IgoChessmanFactory.getInstance();
        black1 = factory.getIgoChessman("b");
        black2 = factory.getIgoChessman("b");
        black3 = factory.getIgoChessman("b");
        System.out.println("判断两颗黑棋是否相同:"+(black1==black2));

        white1 = factory.getIgoChessman("w");
        white2 = factory.getIgoChessman("w");
        System.out.println("判断两颗白棋是否相同:"+(white1==white2));

        black1.locate(new Coordinates(1, 1));
        black2.locate(new Coordinates(2, 4));
        black3.locate(new Coordinates(2, 3));
        white1.locate(new Coordinates(3, 5));
        white2.locate(new Coordinates(2, 6));
    }

}
Client
package test13;

public class Coordinates {
    private int x;
    private int y;

    public Coordinates(int x,int y) {
        // TODO Auto-generated constructor stub
        this.x = x;
        this.y = y;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

}
Coordinates
package test13;

public abstract class IgoChessman {
    public abstract String getColor();
    public void locate(Coordinates coord){
        System.out.println("棋子颜色:"+this.getColor()+",棋子位置:"+coord.getX()+","+coord.getY());
    }
}

class BlackIgoChessman extends IgoChessman{

    @Override
    public String getColor() {
        // TODO Auto-generated method stub
        return "黑色";
    }

}

class WhiteIgoChessman extends IgoChessman{

    @Override
    public String getColor() {
        // TODO Auto-generated method stub
        return "白色";
    }

}
IgoChessman
package test13;

import java.util.Hashtable;



public class IgoChessmanFactory {
    private static IgoChessmanFactory instance = new IgoChessmanFactory();
    private static Hashtable ht;

    public IgoChessmanFactory() {
        // TODO Auto-generated constructor stub
        ht = new Hashtable();
        IgoChessman black,white;
        black = new BlackIgoChessman();
        ht.put("b", black);
        white = new WhiteIgoChessman();
        ht.put("w", white);
    }

    public static IgoChessmanFactory getInstance(){
        return instance;
    }

    public static IgoChessman getIgoChessman(String color){
        return (IgoChessman)ht.get(color);
    }

}
IgoChessmanFactory

 

posted on 2023-11-13 08:43  夜的第七章i  阅读(3)  评论(0编辑  收藏  举报