如何创建具有根据唯一键生成的属性的对象
我想要拥有具有一系列属性和单个唯一标识键的对象。由于密钥是唯一的,因此它可用于挑选一组特定的属性。我希望能够在实例化对象时使用该键,以便与该键关联的属性成为该对象的属性。
属性规范将是常规的(即,如果一个键具有称为描述的属性,则所有其他键也将如此)。
一个糟糕的例子
这个例子是我最初尝试解决问题的方法,但还不够。我将其发布在这里是为了说明这种方法的问题。
我可以有一个具有名称和描述属性的 Food 类。在这种情况下,名称应该是唯一的键,在这种情况下,仅具有描述。
public class Food {
private String name;
private String description;
public Food(String name, String description) {
this.name = name;
this.description = description;
}
}
这种方法的问题
首先,每当我创建一种特定类型的食物(例如披萨)时,我每次都必须输入相同的描述,例如“一种流行的意大利菜”。我更希望能够创建一个只知道食物名称的对象,然后为我填写其余的属性。换句话说,我应该能够只传入披萨,并且将使用适当的描述创建该对象。
其次,该示例不保证 name 属性的唯一性。我可以创建一个披萨,描述为“一道流行的意大利菜”,另一个描述为“奶酪味!”但是,我希望该名称能够唯一地挑选出一组属性。当然,一个属性可以属于多个键(披萨和意大利面都可以有“意大利菜”的描述),但键只能有一个特定的描述。
潜在的解决方案
我想这至少需要两件事:
某种将名称与一组属性关联起来的方法。 HashMap 似乎是理想的选择,但是哪里是存储它的好位置呢?将它放在 Food 类中似乎很奇怪。
将一些处理 Food 对象构建的创建类(也许这对于上面的 HashMap 来说是一个很好的位置?)我已经研究了 Factory 和 Builder 模式,但 Factory 似乎是用于创建不同的子类和我希望能够存储数千个不同的项目名称(而不是创建数千个不同的子类)。构建器似乎是为了以更简单的方式指定对象的所有属性,即记住构造函数中参数的顺序,同时还保证对象不能半途实例化,但这似乎与我的想法几乎相反正在寻找。
I want to have objects with a series of properties and a single unique identifying key. As the key is unique, it can be used to pick out a particular set of properties. I want to be able to use the key when instantiating the object such that the properties associated with that key become attributes on that object.
The property specification will be regular (i.e. if one key has a property called description then so will every other key).
A Bad Example
This example is how I tried to initially approach the problem and is insufficient. I post it here to illustrate the problems with this approach.
I could have a Food class which has name and description attributes. In this case the name is supposed to be a unique key possessing, in this case, just a description.
public class Food {
private String name;
private String description;
public Food(String name, String description) {
this.name = name;
this.description = description;
}
}
Problems with this Approach
Firstly, whenever I create a particular kind of food, say pizza, I would have to enter the same description each time, say "A popular Italian dish". What I would prefer is to be able to create an object knowing only the name of the food and then have the rest of the attributes filled in for me. In other words, I should be able to just pass in pizza and the object will get created with an appropriate description.
Secondly, this example has no guaranteed uniqueness for the name attribute. I could create a pizza with a description of "A popular Italian dish" and another with a description of "Cheesy goodness!" However, I want the name to uniquely pick out a set of attributes. Of course an attribute could belong to multiple keys (pizza and pasta could both have a description of "An Italian dish") but key can only have a particular description.
Potential Solutions
I imagine that this will require at least 2 things:
Some way of associating a name with a set of attributes. A HashMap seems ideal for this but where would be a good location to store it? It seems very strange to put it inside the Food class.
Some creating class which handles the building of the Food objects (perhaps this would be a good location for the above HashMap?) I've looked into both the Factory and Builder patterns but the Factory seems to be for creating different subclasses and I want to be able to store 1000s of different item names (and not create 1000s of different subclasses). The Builder seems to be for specifying all of the attributes of an object in an easier way that remembering the order of parameters in a constructor whilst also guaranteeing that an object cannot be half-way instantiated but this seems to be almost the opposite of what I am looking for.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我同意 hashmap 是理想的,我推荐的是某种映射器实用程序类,它可以从 xml 文件或数据库中获取您的名称和描述,然后将它们插入到 hash map 中。这将是您的哈希图所在的位置。然后,您可以使用工厂来构造食物对象,并从哈希图中注入名称和描述依赖项。
I agree that the hashmap is ideal, what I would recommend is some kind of mapper utility class that would take your names and descriptions from a xml file or database and then insert them into the hash map. This would be where your hashmap lives. Then you can use a factory to construct your food objects and inject the name and description dependencies from the hashmap.
您可以使用 ORM 框架(如 Hibernate)将 Java 类映射到数据库表,这将负责管理和生成 Java 类的主键。
You can use an ORM framework(like Hibernate) to map the Java class to a database table, this will take care of managing and generating the primary key for the Java class.
Map是一个很好的数据结构来存储预定义的属性。但是您需要一个将属性复制到对象的代码,即调用如下代码:
food.setDescription(attrs.get("description"))
该代码可以在工厂中编写。工厂不一定创建不同类的实例。它可以创建同一类的实例并启动它们。
其他方法是使用名为模板对象的模式。这意味着您可以创建多个具有已预定义属性的对象,例如披萨、意大利面、木桩、汤等。然后,当您需要创建另一个披萨时,您首先创建现有披萨的副本,然后对其进行修改以制作您的特定披萨。 java中有多种方法可以创建现有对象的副本:复制构造函数、克隆、序列化等。
Map is a good data structure to store the predefined attributes. But you need a code that copies the attribute to the object, i.e. calls code like the following:
food.setDescription(attrs.get("description"))
This code could be written in factory. Factory is not necessarily creates instances of different classes. It may create instances of the same class and initiate them.
Other approach is using pattern named template object. It means that your create several objects like pizza, pasta, stake, soup, etc. with already predefined properties. Then when you need to create yet another pizza you first create a copy of already existing pizza and then modify it to make your specific pizza. There are various ways in java to create copy of existing object: copy constructor, clone, serialization etc.
您需要一个工厂和一些重载的构造函数。
You need a Factory and some overloaded constructors.