首页 > 其他分享 >实验13:享元模式

实验13:享元模式

时间:2024-11-11 08:48:46浏览次数:1  
标签:享元 13 int Coordinates 实验 Chess ChessFactory new public

[实验任务一]:围棋

设计一个围棋软件,在系统中只存在一个白棋对象和一个黑棋对象,但是它们可以在棋盘的不同位置显示多次。

实验要求:

1.提交类图;

2.提交源代码;

3.注意编程规范;

4.要求用简单工厂模式和单例模式实现享元工厂类的设计

1.类图

 

2.源代码

Coordinates.java
package test13;
//外部状态类——坐标类
class Coordinates {

    private int x;
    private int y;

    public Coordinates(int x,int y) {
        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;
    }
}

Chess.java
package test13;

abstract class Chess {
    public abstract String getColor();
    public void locate(Coordinates co) {
        System.out.println(this.getColor()+"棋的位置:"+co.getX()+","+co.getY());
    }
}

ChessFactory.java
package test13;

import java.util.Hashtable;

public class ChessFactory {
    private static ChessFactory instance=new ChessFactory();
    private static Hashtable ht;
    public ChessFactory() {
        ht=new Hashtable();
        Chess black,white;
        black=new BlackChess();
        ht.put("b", black);
        white=new WhiteChess();
        ht.put("w", white);
    }
    public static ChessFactory getInstance() {
        return instance;
    }
    public static Chess getChess(String color) {
        return (Chess)ht.get(color)
;    }
}

BlackChess.java
package test13;
public class BlackChess extends Chess{

    @Override
    public String getColor() {
        return "黑";
    }

}

WhiteChess.java
package test13;

public class WhiteChess extends Chess{

    @Override
    public String getColor() {
        // TODO 自动生成的方法存根
        return "白";
    }

}
Client.java
package test13;

public class Client {

    public static void main(String[] args) {
        Chess black1,black2,black3,white1,white2;
        ChessFactory factory;
        factory = ChessFactory.getInstance();
        black1 = ChessFactory.getChess("b");
        black2 = ChessFactory.getChess("b");
        black3 = ChessFactory.getChess("b");
        white1 = ChessFactory.getChess("w");
        white2 = ChessFactory.getChess("w");
        black1.locate(new Coordinates(1, 1));
        black2.locate(new Coordinates(-4, 6));
        black3.locate(new Coordinates(2, 4));
        white1.locate(new Coordinates(-2, 3));
        white2.locate(new Coordinates(3, 3));
    }
}

 

标签:享元,13,int,Coordinates,实验,Chess,ChessFactory,new,public
From: https://www.cnblogs.com/po3a/p/18538997

相关文章

  • 实验三
    任务一:button.hpp#pragmaonce#include<iostream>#include<string>usingstd::string;usingstd::cout;//按钮类classButton{public:Button(conststring&text);stringget_label()const;voidclick();private:stringla......
  • 实验3 类和对象
    实验任务1:代码:1#pragmaonce23#include<iostream>4#include<string>56usingstd::string;7usingstd::cout;89//按钮类10classButton{11public:12Button(conststring&text);13stringget_label()const;14vo......
  • 「杂题乱刷2」CF1354E
    题目链接CF1354EGraphColoring(*2100)解题思路发现这个东西就是类似于二分图染色的东西。因为\(2\)只能和\(1,3\)链接。其余种类的点都不能连接。不妨把\(1,3\)都看成同一个点放到最后处理。那么我们就相当于是要找到一种方案使得选择每个联通快的黑点或白点,使得最......
  • 实验3 类和对象_基础编程2
    任务1:#pragmaonce#include<iostream>#include<string>usingstd::string;usingstd::cout;//按钮类classButton{public:Button(conststring&text);stringget_label()const;voidclick();private:stringlabel;};......
  • 实验四
    task1源代码:#include<stdio.h>#defineN4#defineM2voidtest1(){intx[N]={1,9,8,4};inti;//输出数组x占用的内存字节数printf("sizeof(x)=%d\n",sizeof(x));//输出每个元素的地址、值for(i=0;i<N;++i)......
  • 20222318 2024-2025-1 《网络与系统攻防技术》实验四实验报告
    1.实验内容1.1实验任务(1)恶意代码文件RaDa.exe类型标识、脱壳与字符串提取。(2)使用IDAPro静态或动态分析crackme1.exe与crakeme2.exe,寻找特定输入,使其能够输出成功信息。(3)分析一个自制恶意代码样本rada,并撰写报告,回答问题。(4)取证分析实践:对于Snort收集的蜜罐主机5天的网络数......
  • 实验4
    任务1:#include<stdio.h>#defineN4#defineM2voidtest1(){intx[N]={1,9,8,4};inti;printf("sizeof(x)=%d\n",sizeof(x));for(i=0;i<N;++i){printf("%p:%d\n",&x[i],x[i]);}printf("x=......
  • c++实验三
    task1:代码:button.hpp:1#pragmaonce23#include<iostream>4#include<string>56usingstd::string;7usingstd::cout;89//按钮类10classButton{11public:12Button(conststring&text);13stringget_label()......
  • 实验4
    1:代码:#include<stdio.h>#defineN4#defineM2voidtest1(){intx[N]={1,9,8,4};inti;//输出数组x占用的内存字节数printf("sizeof(x)=%d\n",sizeof(x));//输出每个元素的地址、值for(i=0;i<N;++i)printf(......
  • 程序设计实验3
    任务1task1.cpp1#include"window.hpp"2#include<iostream>34usingstd::cout;5usingstd::cin;67voidtest(){8Windoww1("newwindow");9w1.add_button("maximize");10w1.display();11......