枚举实现单例

单例模式的作用:

保证一个类只有一个实例,并提供一个访问它的全局访问点常用于重量级对象,这个类创建对象是耗时,耗内存和复杂的单例模式的好处 保证对象在内存中仅有一个,减少内存开销

单例模式枚举方式:

立即加载、线程安全、实现简单、序列化

例子

public class DBConnection {
    public static void main(String[] args) throws Exception {
        EnumSingleton.SingletonClass con1 = EnumSingleton.INSTANCE.getInstance();
        EnumSingleton.SingletonClass con2 = EnumSingleton.INSTANCE.getInstance();
        System.out.println(con1 == con2);
    }   

    public enum EnumSingleton {
        INSTANCE;// 枚举里的属性相当于Singleton的实例

        private SingletonClass instance;

        private EnumSingleton() {
            instance = new SingletonClass();
            System.out.println("call EnumSingleton()");
        }   

        public SingletonClass getInstance() {
            return instance;
        }   

        class SingletonClass {
        }   
    }   
}

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Java中实现Elasticsearch的简单应用,你需要先添加Elasticsearch的Java客户端库(例如Elasticsearch Java High Level REST Client)到你的项目中。然后,你可以使用该客户端库来连接到Elasticsearch集群并执行索引、搜索和聚合等操作。 以下是一个简单的示例,展示了如何使用Java High Level REST Client来实现Elasticsearch的基本操作: 1. 添加Maven依赖: ```xml <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>7.15.1</version> </dependency> ``` 2. 创建Elasticsearch客户端: ```java RestHighLevelClient client = new RestHighLevelClient( RestClient.builder(new HttpHost("localhost", 9200, "http"))); ``` 3. 创建索引: ```java CreateIndexRequest request = new CreateIndexRequest("my_index"); request.mapping("_doc", " {\n" + " \"properties\": {\n" + " \"title\": {\n" + " \"type\": \"text\"\n" + " },\n" + " \"content\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }", XContentType.JSON); CreateIndexResponse response = client.indices().create(request, RequestOptions.DEFAULT); ``` 4. 索引文档: ```java IndexRequest request = new IndexRequest("my_index"); request.id("1"); request.source( " {\n" + " \"title\": \"Elasticsearch Introduction\",\n" + " \"content\": \"Elasticsearch is a distributed search engine.\"\n" + " }", XContentType.JSON); IndexResponse response = client.index(request, RequestOptions.DEFAULT); ``` 5. 搜索文档: ```java SearchRequest request = new SearchRequest("my_index"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchQuery("content", "distributed")); request.source(sourceBuilder); SearchResponse response = client.search(request, RequestOptions.DEFAULT); ``` 以上是一个简单的Java示例,演示了如何使用Java High Level REST Client来实现Elasticsearch的索引和搜索操作。你可以根据自己的需求进一步学习和了解Elasticsearch的其他功能和用法。 关于枚举实现单例模式的问题,以下是一个Java中使用枚举实现单例模式的示例: ```java public enum Singleton { INSTANCE; // 添加其他成员变量和方法 public void doSomething() { // 单例对象的操作 } } ``` 在这个示例中,我们定义了一个枚举类Singleton,它只有一个枚举实例INSTANCE。你可以在枚举类中添加其他成员变量和方法来实现单例对象的操作。通过访问Singleton.INSTANCE,你可以获取到单例对象的实例,并调用其方法。 这种方式是Java中推荐的实现单例模式的方式之一,它保证了线程安全和唯一实例性质。同时,枚举类还提供了序列化和反序列化的支持,避免了反射和反序列化创建新的实例。因此,使用枚举实现单例模式是一种简洁、安全且可靠的方式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值