首页 > 其他分享 >设计模式实验12实验13

设计模式实验12实验13

时间:2023-11-14 20:22:52浏览次数:44  
标签:12 ChessPiece package void System class 实验 设计模式 public

外观模式

 

package test12;

public class Memory {
    public void check(){
        System.out.println("内存自检");
    }
}


package test12;

public class HardDisk {
    public void read(){
        System.out.println("硬盘读取");
    }
}


package test12;

public class CPU {
    public void run(){
        System.out.println("CPU运行");
    }
}


package test12;

public class OS {
    public void load(){
        System.out.println("操作系统的载入");
    }
}

package test12;

public class Mainframe {
    private Memory memory;
    private CPU cpu;
    private HardDisk hardDisk;
    private OS os;

    public Mainframe() {
        memory=new Memory();
        cpu=new CPU();
        hardDisk=new HardDisk();
        os=new OS();
    }

    public void on(){
        System.out.println("计算机开机。。。");
        memory.check();
        cpu.run();
        hardDisk.read();
        os.load();
        System.out.println("计算机开机成功");
    }
}
package test12;

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

享元模式

package test13;

// 享元接口
interface ChessPiece {
    void display(int x, int y);
}

 

 

package test13;

class WhiteChessPiece implements ChessPiece {
    @Override
    public void display(int x, int y) {
        System.out.println("在坐标(" + x + ", " + y + ")显示白棋");
    }
}


package test13;

class BlackChessPiece implements ChessPiece {
    @Override
    public void display(int x, int y) {
        System.out.println("在坐标(" + x + ", " + y + ")显示黑棋");
    }
}
package test13;

class ChessFactory {
    private static ChessPiece whiteChessPiece;
    private static ChessPiece blackChessPiece;

    // 获取白棋对象
    static ChessPiece getWhiteChessPiece() {
        if (whiteChessPiece == null) {
            whiteChessPiece = new WhiteChessPiece();
        }
        return whiteChessPiece;
    }

    // 获取黑棋对象
    static ChessPiece getBlackChessPiece() {
        if (blackChessPiece == null) {
            blackChessPiece = new BlackChessPiece();
        }
        return blackChessPiece;
    }
}


package test13;

public class Client {
    public static void main(String[] args) {
        // 获取享元工厂对象
        ChessFactory chessFactory = new ChessFactory();

        // 在不同位置显示白棋
        ChessPiece whiteChess1 = chessFactory.getWhiteChessPiece();
        whiteChess1.display(1, 1);

        ChessPiece whiteChess2 = chessFactory.getWhiteChessPiece();
        whiteChess2.display(2, 2);

        // 在不同位置显示黑棋
        ChessPiece blackChess1 = chessFactory.getBlackChessPiece();
        blackChess1.display(3, 3);

        ChessPiece blackChess2 = chessFactory.getBlackChessPiece();
        blackChess2.display(4, 4);
    }
}

 

标签:12,ChessPiece,package,void,System,class,实验,设计模式,public
From: https://www.cnblogs.com/syhxx/p/17832442.html

相关文章

  • 软件设计模式学习每日总结-第二天
    回顾第一天:昨天学习的设计模式中的uml类图,还记得类与类的关系有关联(聚合、组合),依赖,继承,实现,其中依赖的耦合度最小,而继承的耦合度最大,而软件设计追求的就是高内聚,低耦合。还学习各种软件设计原则。第二天创建型模式:将创建和使用分离,使用者不需要关注创建的细节。简单工厂模式:......
  • 实验4
    任务1.1#include<stdio.h>#defineN4voidtest1(){ inta[N]={1,9,8,4}; inti; printf("sizeof(a)=%d\n",sizeof(a)); for(i=0;i<N;++i) printf("%p:%d\n",&a[i],a[i]); printf("a=%p\n",a);}......
  • P2612
    一道插入DP题。分析这一类型的DP大多是对许多段的维护,各个段之间的要求较弱或没有,一般都很难搞。先把概率转成计数。观察题面,我们可以考虑维护一条从上到下类似扫描线的东西,每次计算下移一格的贡献。很明显,在坐标轴上画出图像,应当由数个峰组成。从上往下扫描就会形成一些......
  • 学习随笔(设计模式:装饰模式)
    内容今天学习了新的设计模式,装饰模式。装饰模式:动态地给一个对象添加一些额外的职责,就增加功能来说,装饰模式比生成子类更加灵活。收获1.当我们在设计软件时,考虑使用基类和派生类的时候,就可以考虑是否可以使用装饰模式,因为装饰模式比生成子类更加灵活2.当我们对一个已有的类添......
  • 计算机网络实验4
    步骤一:创建虚拟网络拓扑步骤二:为虚拟网络中的各路由器配置静态路由步骤三:关闭网卡步骤四:步骤五:创建一个3500字节长度的文件命名为3500.0步骤六:在主机ns57c上启动wireshark,并在ns57C后台启动Wireshark步骤七:在两个主机上启动tcp服务程序,将主机ns57C上的3500.0文件读......
  • 大型数据库实验五
    ......
  • chatgpt升级啦,训练数据时间更新到2023年4月,支持tools(升级functionCall),128k上下文
     (2023年11月7日)gpt-4-1106-previewhttps://platform.openai.com/docs/models/gpt-4-and-gpt-4-turbo训练数据日期升级到2023年四月上线文增加到128k调用一次chatgpt接口,可以得到多次函数调用 importOpenAIfrom"openai";constopenai=newOpenAI();//Exampledummyfunc......
  • 即时通讯技术文集(第23期):IM安全相关文章(Part12) [共15篇]
    为了更好地分类阅读52im.net总计1000多篇精编文章,我将在每周三推送新的一期技术文集,本次是第23 期。[- 1 -] 理论联系实际:一套典型的IM通信协议设计详解(含安全层设计)[链接] http://www.52im.net/thread-283-1-1.html[摘要] 本文将以理论联系实际的方式,详细讲解一套典......
  • 软件设计实验12:外观模式
    [实验任务一]:计算机开启在计算机主机(Mainframe)中,只需要按下主机的开机按钮(on()),即可调用其他硬件设备和软件的启动方法,如内存(Memory)的自检(check())、CPU的运行(run())、硬盘(HardDisk)的读取(read())、操作系统(OS)的载入(load()),如果某一过程发生错误则计算机启动失败。......
  • 01_实验一_操作系统的启动start
    实验一操作系统的启动从源代码到可运行的操作系统(前置知识)API与SDK以C语言编写的操作系统为背景进行介绍,EOS是由C语言编写的操作系统和应用程序之间一个重要的纽带就是应用程序接口(简称API)。操作系统通过开放API为应>用程序提供服务,应用程序通过使用这些API......