java 单例模式和工厂模式

16 篇文章 1 订阅
单例模式根据实例化对象时机的不同分为两种:一种是饿汉式单例,一种是懒汉式单例。
私有的构造方法
指向自己实例的私有静态引用
以自己实例为返回值的静态的公有的方法
饿汉式单例

    public class Singleton {
        private static Singleton singleton = new Singleton();
        private Singleton(){}
        public static Singleton getInstance(){
            return singleton;
        }
    }

懒汉式单例

    public class Singleton {
        private static Singleton singleton;
        private Singleton(){}

        public static synchronized Singleton getInstance(){
            if(singleton==null){
                singleton = new Singleton();
            }
            return singleton;
        }
    }

工厂方法模式代码

    interface IProduct {
        public void productMethod();
    }

    class Product implements IProduct {
        public void productMethod() {
            System.out.println("产品");
        }
    }

    interface IFactory {
        public IProduct createProduct();
    }

    class Factory implements IFactory {
        public IProduct createProduct() {
            return new Product();
        }
    }

    public class Client {
        public static void main(String[] args) {
            IFactory factory = new Factory();
            IProduct prodect = factory.createProduct();
            prodect.productMethod();
        }
    }

抽象工厂模式代码

    interface IProduct1 {
        public void show();
    }
    interface IProduct2 {
        public void show();
    }

    class Product1 implements IProduct1 {
        public void show() {
            System.out.println("这是1型产品");
        }
    }
    class Product2 implements IProduct2 {
        public void show() {
            System.out.println("这是2型产品");
        }
    }

    interface IFactory {
        public IProduct1 createProduct1();
        public IProduct2 createProduct2();
    }
    class Factory implements IFactory{
        public IProduct1 createProduct1() {
            return new Product1();
        }
        public IProduct2 createProduct2() {
            return new Product2();
        }
    }

    public class Client {
        public static void main(String[] args){
            IFactory factory = new Factory();
            factory.createProduct1().show();
            factory.createProduct2().show();
        }

    }

【原创】原创文章,更多关注敬请关注微信公众号。


  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Java中的单例模式是指一个类只有一个实例,且该类能自行创建这个实例的一种模式。在Java中,单例模式可以通过不同的实现方式来实现,其中一种常见的实现方式是使用静态工厂方法模式(Simple Factory Pattern)。静态工厂方法模式是一种创建型模式,它通过一个静态方法来创建对象,而不是通过直接调用构造方法来创建对象。这种模式可以将对象的创建和使用解耦,提供了一种更加灵活和可控的方式来实现对象的创建。工厂模式Java中常用的设计模式之一,它属于创建型模式,提供了一种创建对象的最佳方式。工厂模式通过定义一个共同的接口或抽象类来创建对象,然后由具体的工厂类来实现这个接口或抽象类,并负责创建具体的对象。这样一来,客户端就可以通过工厂类来创建对象,而无需直接调用对象的构造方法。 [1][2][3<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* *3* [基于java单例模型和工厂模式](https://blog.csdn.net/weixin_56102526/article/details/120318486)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值