English 中文(简体)
是否应当储存资产?
原标题:Is Hashtable appropriate for storing assets?

我来自行动说明3的背景,这是我首次撰写过任何 Java。 照片似乎与闪电中的幻觉相类似,但我希望确保我正确使用。 我认为,《哈希姆》被归类为接受“强硬”作为目标。 这是否正确? 是否有一个不同的收款分级,更适合类似情况? 无论如何,请将我的N00b Java up起来。 我需要了解这一点。

package com.typeoneerror.apps.app_name.utils;

import android.content.Context;
import android.graphics.Typeface;

import java.util.Hashtable;

public class FontRegistry
{
    private static FontRegistry _instance;

    private Context                         _context;
    private Hashtable<String, Typeface>     _fonts;

    private FontRegistry()
    {
        _fonts = new Hashtable<String, Typeface>();
    }

    public static FontRegistry getInstance()
    {
        if (_instance == null)
        {
            _instance = new FontRegistry();
        }
        return _instance;
    }

    public void init(Context context)
    {
        _context = context;

    }

    public Typeface getTypeface(int resourceId)
    {
        String fontName = _context.getResources().getString(resourceId);
        if (!_fonts.containsKey(fontName))
        {
            String fontPath = "fonts/" + fontName;
            Typeface typeface = Typeface.createFromAsset(_context.getAssets(), fontPath);
            _fonts.put(fontName, typeface);
        }
        return (Typeface)_fonts.get(fontName);
    }
}
最佳回答

给您的两条建议。

首先,变量类型应当指地图的接口。 这给你带来了更大的未来灵活性,并将更好地与大多数其他的贾瓦dev发展中国家沟通。

第二,执行工作应当是哈希姆帕拉,而不是哈希姆。 如哈希马普一样,一切都可能同步。

如果你确实需要多面通道,我建议使用一个连接式的HashMap而不是哈希特。 并行 HashMap表现得较好,因为在进入时,它打开了整个地图。

So,

private Map<String, Typeface>     _fonts;

以及

_fonts = new HashMap<String, Typeface>();

最后,许多 Java发展中国家宁愿你不要用强调来开始成员变量。 虽然这是可争辩的偏好。

EDIT: A final nitpick。 你似乎对登记处采用单一吨模式。 以后,这可激励大家考虑避免单吨。 但是,无视这一点,你可能最好不要在宣言中立即出现单一吨的静态。 它可以避免在首次提出这种论点时可能出现的争论。

So:

private static FontRegistry _instance = new FontRegistry;
问题回答




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签