ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

设计模式(一)单例模式:懒汉式

2021-07-13 12:32:53  阅读:151  来源: 互联网

标签:int System public rooms 单例 println 设计模式 懒汉 out


单例模式

这种类型的设计模式属于创建型模式

这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个对象被创建。这个类提供了一种访问其唯一的对象的方式,可以直接访问,不需要实例化该类的对象。

目的:避免一个全局使用的对象不停的创建和注销

核心:私有构造方法

原理:因为构造方法是私有的所以在这个类之外不能通过new创建这个类的实例,这个类的实例只能在类的内部调用类私有的构造方法才能创建,只要保证类的内部只创建一个实例就可以实现。(个人理解)

懒汉式

当需要使用这个单例对象时才创建这个对象

案例:酒店管理

功能:查房(显示所有房间以及房间的租客)、入住、退房、退出

酒店类(单例类)

// 酒店类(单例模式:懒汉式)
// 全局只有一个酒店
public class Hotel {
    // 酒店单例
    private static Hotel hotel;
    // 房间
    private Room[][] rooms;

    // 公开的获取这个单例的get()方法
    // 懒汉式:当需要这个单例的时候才创建这个单例
    public static Hotel getHotel() {
        if (hotel == null) {
            hotel = new Hotel();
        }
        return hotel;
    }

    // (重要,核心)私有的构造方法
    private Hotel() {
        init();
    }

    // 初始化
    private void init() {
        rooms = new Room[5][5];
        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 5; j++) {
                rooms[i][j] = new Room((i + 1) * 1000 + j + 1);
            }
        }
    }

    // 查房
    public void queryRooms() {
        System.out.println("----------------查房-----------------");
        for (int i = 0; i < rooms.length; i++) {
            for (int j = 0; j < rooms[i].length; j++) {
                System.out.print(rooms[i][j].getRoomId() + "\t");
            }
            System.out.print("\n");
            for (int j = 0; j < rooms[i].length; j++) {
                if (rooms[i][j].getGuest() == null) {
                    System.out.print(rooms[i][j].getGuest() + "\t");
                } else {
                    System.out.print(rooms[i][j].getGuest().getName() + "\t");
                }
            }
            System.out.print("\n");
        }
        System.out.println("------------------------------------");
    }

    // 退房
    public void checkOut(int roomId) {
        int row = roomId / 1000 - 1;
        int col = roomId % 1000 - 1;
        if (row >= 0 && row <= 4 && col >= 0 && col <= 4) {
            if (rooms[row][col].checkOut()) {
                System.out.println("退房成功!");
            } else {
                System.out.println("退房失败,这个房间没有人入住!");
            }
        } else {
            System.out.println("输入房间号不存在!");
        }
    }

    // 入住
    public void checkIn(int roomId, Guest guest) {
        int row = roomId / 1000 - 1;
        int col = roomId % 1000 - 1;
        if (row >= 0 && row <= 4 && col >= 0 && col <= 4) {
            if (rooms[row][col].getGuest() != null) {
                System.out.println("入住失败!,该房间已经有人住了!");
            } else {
                rooms[row][col].setGuest(guest);
                System.out.println("入住成功!");
            }
        } else {
            System.out.println("入住失败!,输入房间号不存在!");
        }
    }
}

房间类

public class Room {
    private int roomId;
    private Guest guest;

    public Room() {
    }

    public Room(int roomId) {
        this.roomId = roomId;
    }

    public boolean checkOut() {
        boolean flag = false;
        if (guest != null) {
            guest = null;
            flag = true;
        }
        return flag;
    }
		//省略get和set方法  
}

客户类

public class Guest {
    private String name;
  	//省略构造方法、get以及set方法
}

入口

public class Main {
    public static void main(String[] args) {
        Hotel hotel = Hotel.getHotel();
        // 这两个局部变量指向的是同一个对象,全局也有且只有这一个Hotel类型的对象
        Hotel hotel2 = Hotel.getHotel();
        Scanner scanner = new Scanner(System.in);
        String service = "0";
        do {
            menu();
            service = scanner.next();
            switch (service) {
                case "1":
                    hotel.queryRooms();
                    break;
                case "2":
                    System.out.println("请输入客户姓名");
                    Guest guest = new Guest(scanner.next());
                    System.out.println("请输入想要入住的房间");
                    hotel.checkIn(scanner.nextInt(),guest);
                    break;
                case "3":
                    System.out.println("请输入想要退的房间");
                    hotel.checkOut(scanner.nextInt());
                    break;
                default:
                    service = "0";
                    break;
            }
        } while (!service.equals("0"));   
    }
}

简单的内存(忽略了很多,展现关键点)

标签:int,System,public,rooms,单例,println,设计模式,懒汉,out
来源: https://blog.csdn.net/ghhjgnn/article/details/118697323

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有