首页 > 其他分享 >11.13(2)

11.13(2)

时间:2023-11-13 09:05:05浏览次数:27  
标签:享元 int 11.13 Coordinates public new IgoChessmanFactory

软件设计                  石家庄铁道大学信息学院

 

实验13:享元模式

本次实验属于模仿型实验,通过本次实验学生将掌握以下内容:

1、理解享元模式的动机,掌握该模式的结构;

2、能够利用享元模式解决实际问题。

 

[实验任务一]:围棋

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

实验要求:

1. 提交类图;

 

2.提交源代码;

package 享元模式;

 

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);

}

}

 

package 享元模式;

 

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));

 

}

}

3. 注意编程规范;

 

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

 

 

标签:享元,int,11.13,Coordinates,public,new,IgoChessmanFactory
From: https://www.cnblogs.com/ljh1688/p/17828399.html

相关文章

  • 11.13~11.19复习
    1.spring全家桶Spring启动流程?springbootSpringBoot启动流程?SpringBoot好处?SpringBoot常用注解2.mysql优化3.jvm详细说一下cms的回收过程详细说一下G1的回收过程GC如何调优 ......
  • 11.13(P)
    列表While循环rt:for循环rt:    元组rt: 只有一个元素时元组  ......
  • 11.13;周日;复盘
    复盘记录内容,回顾经验编程学习1.学习方法学习初期,用到啥学啥学习20%的功能即可,不必去深究啥火学啥对于作品,是先完成再完美(编程也是一样的)c语言理论知识指针......
  • 上周热点回顾(11.7-11.13)
    热点随笔:· 数据可视化分析之新技能——魔数图 (葡萄城技术团队)· 工作十年,入职新公司不到四个月就遇到裁员 (Angelasp)· 写点程序员离职的心得 (张飞洪[厦门])·......
  • 2022.11.13:CCPC广州
    补题传送门3题铁这把铁没有沈阳铜那么不甘心(沈阳打完之后,一星期都睡不好),看到了队伍内很多知识点的缺失,不知道在剩下一个正式赛来之前能不能弥补上(跟去年一样,做北大出的......
  • 11.13.4
    #include<stdio.h>intcz(intx,inty,inta[],intb[]);intmain(){inta[100],b[100];inti,m,n;scanf("%d",&m);for(i=0;i<m;i++){scanf("%d",&a[i]);}scanf("%d",&n);fo......
  • 22.11.13 CCPC 广州站 记录
    上来看A(树上DP),直观认为可做,前后拉着队友研究了两个小时,经过lcx,lgy两次hack正确性,最终基本得到答案思路,因为过于复杂和担心正确性问题不敢写。反思:1.正式比赛中不应该一开......
  • 11.13解题报告
    T1pineapple题目描述《暗黑破坏神》是一款经典的\(\text{RPG}\)游戏。其英文名为\(\text{Diablo}\),因发音相似而被称为“大菠萝”。游戏中每个玩家都有两个属性,被称......
  • 11.13.3
    #include<stdio.h>intcz(intx,inty,inta[],intb[]);intmain(){inta[100],b[100];inti,m,n;scanf("%d",&m);for(i=0;i<m;i++){scanf("%d",&a[i]);}scanf("%......
  • 第四周学习总结(11.6-11.13)
    两层全连接神经网络的内容要比想象中的多很多,代码量也很多,在cs231n只用了15分钟讲解的东西我用了一周半的时间才完全的消化理解,这周终于完成了全连接神经网络博客的书写htt......