软件设计模式————(享元模式)
1.软件设计模式————(简单工厂模式)2.软件设计模式————(工厂方法模式)3.软件设计模式————(抽象工厂模式)4.软件设计模式————(建造者模式)5.软件设计模式————(原型模式)6.软件设计模式————(单例模式)7.软件设计模式————(适配器模式)8.软件设计模式————(桥接模式)9.软件设计模式————(组合模式)10.软件设计模式————(装饰模式)11.软件设计模式————(外观模式)
12.软件设计模式————(享元模式)
13.软件设计模式————(代理模式)14.软件设计模式————(命令模式)15.软件设计模式————(解释器模式)16.软件设计模式————(迭代器模式)17.软件设计模式————(中介者模式)18.软件设计模式————(备忘录模式)19.软件设计模式————(观察者模式)20.软件设计模式————(职责链模式)[实验任务一]:围棋
设计一个围棋软件,在系统中只存在一个白棋对象和一个黑棋对象,但是它们可以在棋盘的不同位置显示多次。
实验要求:
1.提交类图;
2.提交源代码;
import java.util.*; //坐标类:外部状态类 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; } } //围棋棋子类:抽象享元类 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{ public String getColor(){ return "黑色"; } } //白色棋子类:具体享元类 class WhiteIgoChessman extends IgoChessman{ public String getColor(){ return "白色"; } } //围棋棋子工厂类:享元工厂类 class IgoChessmanFactory{ private static IgoChessmanFactory instance = new IgoChessmanFactory(); private static Hashtable hashtable; private IgoChessmanFactory(){ hashtable = new Hashtable(); IgoChessman black,white; black = new BlackIgoChessman(); hashtable.put("b", black); white = new WhiteIgoChessman(); hashtable.put("w",white); } public static IgoChessmanFactory getInstance(){ return instance; } public static IgoChessman getIgoChessman(String color){ return (IgoChessman) hashtable.get(color); } } public class Client { public static void main(String[] args) { IgoChessman black1,black2,black3,white1,white2; black1 = IgoChessmanFactory.getIgoChessman("b"); black2 = IgoChessmanFactory.getIgoChessman("b"); black3 = IgoChessmanFactory.getIgoChessman("b"); System.out.println("判断两颗黑棋是否相同:"+(black1==black2)); white1 = IgoChessmanFactory.getIgoChessman("w"); white2 = IgoChessmanFactory.getIgoChessman("w"); System.out.println("判断两颗白棋是否相同:"+(white1==white2)); black1.locate(new Coordinates(1,2)); black2.locate(new Coordinates(3,4)); black3.locate(new Coordinates(1,3)); white1.locate(new Coordinates(2,4)); white2.locate(new Coordinates(2,5)); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix