剑指offer-多例设计模式

package com.msc.design;

//多例设计模式
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 Mult {
  public static void main(String[] args) {
      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());
      }
      System.out.println("---------------------------");
      ColorB c = ColorB.GREEN ;
      System.out.println(ColorB.RED.getMessage());
      System.out.println(c.getMessage());
      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));
  }
}

在这里插入图片描述

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

隔壁de小刘

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

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

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

打赏作者

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

抵扣说明:

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

余额充值