Java单例设计、多例设计、枚举

本文通过实例解析了单例模式的饿汉式和懒汉式实现,以及多例模式、枚举多例和接口实现。介绍了如何在不同场景下使用这些设计模式,并展示了Color类的多例和枚举实例。

在这里插入图片描述

package com.msc.example;
//饿汉式
class Sinleton{
    private static final Sinleton INSTANCE = new Sinleton() ;
    private Sinleton () {} ;
    public static Sinleton getInstance(){
        return INSTANCE ;
    }
    public void print(){
        System.out.println("单例设计模式饿汉式");
    }
}
//懒汉式
class SinletonB{
    private static  SinletonB instance ;
    private SinletonB () {} ;
    public static SinletonB getInstance(){
        if(instance == null){
            instance = new SinletonB() ;
        }
        return instance ;
    }
    public void print(){
        System.out.println("单例设计模式懒汉式");
    }
}
//多例设计模式
class Color{
    private static final Color RED = new Color("红色") ;
    private static final Color GREEN = new Color("绿色") ;
    private static final Color BLUE = new Color("蓝色") ;
    public String color ;
    private Color(String color){
        this.color = color ;
    }
    public static Color getColor(String color){
        switch (color){
            case "red" : return RED ;
            case "green" : return GREEN ;
            case "blue" : return BLUE ;
            default: return null ;
        }
    }
    public String toString(){
        return this.color ;
    }
}
//枚举 多例设计模式
interface IMessage{
    public void print() ;
}
enum ColorB implements IMessage{
    RED("红色"){
        @Override
        public String getMessage() {
            return this.toString();
        }
    } , BLUE("蓝色"){
        @Override
        public String getMessage() {
            return this.toString();
        }
    } , GREEN("绿色"){
        @Override
        public String getMessage() {
            return this.toString();
        }
    };
    private String color ;
    private ColorB() {} ;
    private  ColorB(String color){
        this.color = color ;
    }

    @Override
    public String toString() {
        return "ColorB{" +
                "color='" + color + '\'' +
                '}';
    }

    public void print(){
        System.out.println("枚举实现接口");
    }
    public abstract String getMessage() ;

}
enum Sex{
    MALE("男"),FEMALE("女") ;
    private String sex ;
    private Sex () {} ;
    private Sex (String sex){
        this.sex = sex ;
    }

    @Override
    public String toString() {
        return this.sex ;
    }
}
class PersonD{
    private String name ;
    private Sex sex ;
    public PersonD () {} ;
    public PersonD (String name,Sex sex) {
        this.name = name ;
        this.sex = sex ;
    } ;

    @Override
    public String toString() {
        return "PersonD{" +
                "name='" + name + '\'' +
                ", sex=" + sex +
                '}';
    }
}
public class SingletonDemo {
    public static void main(String[] args) {
        System.out.println("----------单例-------------");
        Sinleton instance = null ;
        instance = Sinleton.getInstance() ;
        instance.print();
        SinletonB instancec = null ;
        instancec = SinletonB.getInstance() ;
        instancec.print();
        System.out.println("----------多例-------------");
        Color red = Color.getColor("red");
        System.out.println(red.toString());
        Color blue = Color.getColor("blue");
        System.out.println(blue.toString());
        System.out.println("----------枚举-------------");
        IMessage msg = ColorB.RED ;
        msg.print();
        for(ColorB c : ColorB.values()){
            System.out.println(c.toString());
        }
        ColorB c = ColorB.GREEN ;
        System.out.println("----------枚举switch-------------");
        switch (c){
            case RED -> System.out.println(ColorB.RED.getMessage()) ;
            case GREEN -> System.out.println(ColorB.GREEN.getMessage()) ;
            case BLUE -> System.out.println(ColorB.BLUE.getMessage()) ;
            default -> System.out.println("默认") ;
        }
        System.out.println("----------枚举option-------------");
        System.out.println(new PersonD("张三",Sex.MALE));
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

隔壁de小刘

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值