Java 如何在单例类中实现Map?

Java 如何在单例类中实现Map?,java,singleton,Java,Singleton,我的任务是制作一个关于花店的节目。我要做一个班级价目表,这是一个单件的。我还有一个给定的测试函数main: public static void main(String[] args) { PriceList pl = PriceList.getInstance(); pl.put("rose", 10.0); pl.put("lilac", 12.0); pl.put("peony", 8.0); 查看这些pl.put(),我决定在类PriceList中实现Map接口,但我不知道具

我的任务是制作一个关于花店的节目。我要做一个班级价目表,这是一个单件的。我还有一个给定的测试函数main:

 public static void main(String[] args) {
 PriceList pl = PriceList.getInstance();
 pl.put("rose", 10.0);
 pl.put("lilac", 12.0);
 pl.put("peony", 8.0);
查看这些pl.put(),我决定在类PriceList中实现Map接口,但我不知道具体如何实现,因为这个类只有一个对象,它必须是Map。我已经写了那么多,不知道下一步该怎么办:

public class PriceList <String, Double>  implements Map <String, Double> {

private static PriceList instance = null;

protected PriceList() {}

public static PriceList getInstance() {
    if (instance == null)
        instance = new PriceList();
    return instance;
}

public void put(String string, double d) {
    // TODO Auto-generated method stub

}}
public类PriceList实现Map{
私有静态PriceList实例=null;
受保护的价格表(){}
公共静态价格表getInstance(){
if(实例==null)
实例=新价格表();
返回实例;
}
公共void put(字符串,双d){
//TODO自动生成的方法存根
}}

提前感谢您的帮助

你的单身汉是对的!您可以在类中创建map属性并将put方法委托给maps的put方法,而不是实现map接口。举个例子:

public class PriceList{

    private Map<String, Double> map = new HashMap<String, Double>();

    private static PriceList instance = null;

    private PriceList() {}

    public static PriceList getInstance() {
        if (instance == null)
            instance = new PriceList();
        return instance;
    }

    public void put(String string, double d) {
        map.put(string,double);       
    }
}
公共类价格表{
私有映射映射=新的HashMap();
私有静态PriceList实例=null;
私有价格表(){}
公共静态价格表getInstance(){
if(实例==null)
实例=新价格表();
返回实例;
}
公共void put(字符串,双d){
map.put(字符串,双精度);
}
}

有更简单的方法:

  • 添加具有Flower和price属性的PricePerFlower类,并将列表作为属性放入PriceList类中

  • 或者只是在PriceList类中添加一个Map属性


    • Map实现通常非常复杂(至少是高效的)

      如果您必须使用此大纲(
      PriceList
      作为单例并实现
      Map
      接口),我建议在引擎盖下使用现有的Map实现:

      public class PriceList <String, Double>  implements Map <String, Double> {
      
          private Map<String, Double> map = new HashMap<>();
          private static PriceList instance = null;
      
          protected PriceList() {}
      
          public static PriceList getInstance() {
              if (instance == null)
                  instance = new PriceList();
              return instance;
          }
      
          public void put(String string, double d) {
              map.put(string, d);
      
          }}
      
      public类PriceList实现Map{
      私有映射映射=新的HashMap();
      私有静态PriceList实例=null;
      受保护的价格表(){}
      公共静态价格表getInstance(){
      if(实例==null)
      实例=新价格表();
      返回实例;
      }
      公共void put(字符串,双d){
      map.put(字符串,d);
      }}
      
      结果
      听起来像是家庭作业。。。问题到底是什么?你测试过了吗?是的,这是个家庭作业,但比这个大。我有一个测试类FloristsTest,我不能更改它,我必须实现必要的类才能使它工作。我还没有测试过它Yet这里的人并不热衷于帮助做家庭作业-这是供你研究的,但除此之外,BrunoDM给出了一个很好的答案。好的,谢谢你提供的信息。我想我必须制作一个单例类,这个类的唯一目标是地图。这个解决方案我比较方便,谢谢!
      public class MyContext {
          private static MyContext ourInstance = null;
          private HashMap<String, String> translatedValue;
      
          public static MyContext getInstance() {
              if (ourInstance == null)
                  ourInstance = new MyContext();
              return ourInstance;
          }
      
          private MyContext() {
              translatedValue = new HashMap<>();
          }
      
          public void addTranslatedValue(String title, String value) {
              translatedValue.put(title, value);
          }
      
          public String getTranslatedValue(String value) {
              return translatedValue.get(value);
          }
      }
      
      MyContext myContext = MyContext.getInstance();
      myContext.addTranslatedValue("Next", valueTranslated);
      System.out.println(myContext.getTranslatedValue("Next"));
      
      valueTranslated