- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
假设我有一个抽象类 (BaseThing)。它有一个必需参数(“base required”)和一个可选参数(“base optional”)。我有一个扩展它的具体类(Thing)。它还具有一个必需参数(“required”)和一个可选参数(“optional”)。所以像这样:
public abstract class BaseThing {
public static final String DEFAULT_BASE_OPTIONAL = "Default Base Optional";
private final String baseRequired;
private String baseOptional = DEFAULT_BASE_OPTIONAL;
protected BaseThing(final String theBaseRequired) {
this.baseRequired = theBaseRequired;
}
final void setBaseOptional(final String newVal) {
this.baseOptional = newVal;
}
public final void selfDescribe() {
System.out.println("Base Required: " + baseRequired);
System.out.println("Base Optional: " + baseOptional);
selfDescribeHook();
}
protected abstract void selfDescribeHook();
}
和:
public final class Thing extends BaseThing {
public static final String DEFAULT_OPTIONAL = "Default Optional";
private final String required;
private String optional = DEFAULT_OPTIONAL;
Thing(final String theRequired, final String theBaseRequired) {
super(theBaseRequired);
required = theRequired;
}
@Override
protected void selfDescribeHook() {
System.out.println("Required: " + required);
System.out.println("Optional: " + optional);
}
void setOptional(final String newVal) {
optional = newVal;
}
}
我想为 Thing 对象创建一个 Joshua Bloch 风格的构建器。不过,更一般地说,我想让 BaseThing 的具体实现更容易拥有构建器,所以我真正想要的(我认为)是一个 BaseThing 构建器,它可以很容易地用于制作 ThingBuilder、OtherThingBuilder 或 SuperThingBuilder .
有没有比我提出的以下方法更好的方法(或者我提出的方法有问题)?
public abstract class BaseThingBuilder<T extends BaseThing> {
private String baseOptional = BaseThing.DEFAULT_BASE_OPTIONAL;
public BaseThingBuilder<T> setBaseOptional(final String value) {
baseOptional = value;
return this;
}
public T build() {
T t = buildHook();
t.setBaseOptional(baseOptional);
return t;
}
protected abstract T buildHook();
}
和:
public final class ThingBuilder extends BaseThingBuilder<Thing> {
private final String baseRequired;
private final String required;
private String optional = Thing.DEFAULT_OPTIONAL;
public ThingBuilder(final String theRequired,
final String theBaseRequired) {
required = theRequired;
baseRequired = theBaseRequired;
}
public ThingBuilder setOptional(final String value) {
optional = value;
return this;
}
protected Thing buildHook() {
Thing thing = new Thing(required, baseRequired);
thing.setOptional(optional);
return thing;
}
}
可用于以类似于以下的方式构建 Thing 对象:
BaseThingBuilder<Thing> builder =
new ThingBuilder("Required!", "Base Required!")
.setOptional("Optional!")
.setBaseOptional("Base Optional!");
Thing thing = builder.build();
thing.selfDescribe();
哪些输出:
Base Required: Base Required!
Base Optional: Base Optional!
Required: Required!
Optional: Optional!
我知道但我认为不是特别重要的一个问题(尽管如果可以改进它会很好)是您必须在设置任何基础之前设置所有非基础选项选项:否则会导致语法错误,因为 setBaseOptional() 返回 BaseThingBuilder 而不是 ThingBuilder。
提前致谢。
最佳答案
我认为以这种方式考虑构建器并不是一个好主意。构建器的层次结构通常会导致令人头疼的问题和脆弱的代码。
减少需要在具体构建器中编写的代码量并重用基础构建器中的逻辑与领域密切相关。开发通用解决方案并不容易。但是,无论如何,让我们尝试通过一个示例:
public interface Builder<T> {
T build();
}
public class Person {
private final String name;
//the proper way to use a builder is to pass an instance of one to
//the class that is created using it...
Person(PersonBuilder builder) {
this.name = builder.name;
}
public String getName(){ return name; }
public static class PersonBuilder implements Builder<Person> {
private String name;
public PersonBuilder name(String name){ this.name = name; return this; }
public Person build() {
if(name == null) {
throw new IllegalArgumentException("Name must be specified");
}
return new Person(this);
}
}
}
绝妙的宝贝!怎么办?也许您想添加一个类(class)来代表学生。你做什么工作?你扩展人吗?当然,这是有效的。采取更“奇怪”的路线并尝试聚合怎么样?是的,您也可以这样做……您的选择会影响您最终如何实现构建器。假设您坚持传统路径并扩展 Person(您应该已经开始问自己,Person
成为一个具体类是否有意义?如果我将其抽象化,我真的需要一个构建器? 如果类是抽象的,构建器应该是抽象的吗?):
public class Student extends Person {
private final long id;
Student(StudentBulder builder) {
super(builder);
this.id = builder.id;
}
public long getId(){ return id; }
//no need for generics, this will work:
public static class StudentBuilder extends PersonBuilder {
private long id;
public StudentBuilder id(long id){ this.id = id; return this; }
public Student build() {
if(id <= 0) {
throw new IllegalArgumentException("ID must be specified");
}
return new Student(this);
}
}
}
好的,这看起来正是您想要的!所以,你试试看:
Person p = new PersonBuilder().name("John Doe").build();
Student s = new StudentBuilder().name("Jane Doe").id(165).build();
看起来很棒!除了,它不编译...第 2 行有一个错误,它声明 The method id(int) is undefined for the type Person.PersonBuilder
。问题是 PersonBuilder#name
返回类型为 PersonBuilder
的构建器,这不是您想要的。在 StudentBuilder
中,您实际上希望 name
的返回类型为 StudentBuilder
。现在,您提前考虑并意识到,如果有任何东西扩展 StudentBuilder
,您会希望它完全返回其他东西……这可行吗?是的,使用泛型。然而,它非常丑陋,并且引入了相当多的复杂性。因此,我拒绝发布说明它的代码,因为担心有人会看到这个线程并实际在他们的软件中使用它。
您可能认为重新排列方法调用会起作用(在调用 name
之前调用 id
):new StudentBuilder().id(165).name("Jane Doe").build()
,但它不会。至少不是没有对 Student
的显式转换:(Student)new StudentBuilder().id(165).name("Jane Doe").build()
因为,在这种情况下,PersonBuilder#build
被调用,返回类型为 Person
...这是 Not Acceptable !即使它在没有显式强制转换的情况下也能工作,它应该会让您畏缩,因为它知道必须按特定顺序调用构建器的方法。因为如果你不这样做,有些东西将无法工作......
如果您继续尝试让它工作,将会出现更多问题。即使你确实让它工作了,我也不认为它会很容易理解,当然也不优雅。当然,请随时证明我的错误并在此处发布您的解决方案。
顺便问一下,您还应该问问自己什么是抽象 构建器?因为,这听起来很矛盾。
最后,我认为这个问题的范围太大了。答案是特定领域的,如果没有您的要求,很难得出答案。请记住,构建器的一般准则是让它们尽可能简单。
另外,看看 related question .
关于java - 用于具体实现抽象类的生成器(Joshua Bloch 风格)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13255985/
我是 Robert,我在使用 JavaScript 时遇到了一些问题。 我得到了一个 (这是隐藏的)。我唯一想问你的是:我想检查日期是否在 中已通过。如果通过了我想改变CSS中容器的背景颜色。不幸的
所以我的问题是我想要求输入使用扫描仪的信息,但它根本不打印出来。当它显示跳过的扫描仪的值时,Scanner CheeseType = new Scanner(System.in);,我得到 null。
Fe_Order_Items fe_order_items_id fe_order_specification_id fe_users_id fe_menu_items_id fe_order_ite
人们普遍提到 - “Celery 是一个基于分布式消息传递的异步任务队列/作业队列”。虽然我知道如何使用 Celery 工作人员等。但内心深处我不明白分布式消息传递的真正重要性和意义以及任务队列在其中
我试图理解下面的代码,但有一些我以前从未见过的东西,那就是:“\&\&” 这是代码: int main() { fork() \&\& (fork() || fork()); exit(EXIT_SU
您好,我是论坛新手。 我有很多使用 python 的经验,但没有使用 tkinter 的经验。 这是我的代码: from tkinter import * def Done(): celEn
在 C# 中,假设我们有一个通用类和一个具体类 [Serializable] public class GenericUser { ... [Serializable] public class Co
我尝试使用的库有一个通用抽象类,其中有两个实现该基础的子类。我想编写一个类,它将根据构造函数参数的参数类型自动创建其中一个子级的实例。 基类没有默认构造函数 基类的构造函数也需要其他通用类的实例 代码
我是 Angular 的新手,我一直在尝试了解它的工作原理。我正在制作一个简单的应用程序,其中有人可以通过简单的 html 界面添加用户并使用 SQLite 将其存储在数据库中,然后他们可以编辑或删除
我想创建一个用于存储数据的对象,限制读/写访问。 例如: OBJ obj1; OBJ obj2; // DataOBJ has 2 methods : read() and write() DataO
注入(inject)/隔离密封在 dll 中且不实现接口(interface)的类的首选方法是什么? 我们使用 Ninject。 假设我们有一个类“Server”,我们想要注入(inject)/隔离“
在花费了至少 10 个小时的时间浏览在线资源、视频和教程之后,我有两个关于将我的 Android 应用程序与 mySQL 数据库连接的问题。 保存文件 1) 所有教程都将 php 文件保存在 C/WA
许多有经验的开发人员建议不要使用 Django multi-table inheritance因为它的性能不佳: Django gotcha: concrete inheritance通过 Jacob
我知道我冒着挨揍的风险,但我觉得我在这件事上要绕圈子。为了让模型可用于多个项目,我们已将模型移出到一个单独的项目(一个 DLL)中,作为一系列要实现的接口(interface)。我们的界面上有这一行:
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
我遇到了一个特定 mac 的问题,它没有显示我正确构建的某个网站。我测试过的所有其他 mac 和 pc 都能正确显示网站,但是在所有浏览器中这个特定的 mac 显示不正确就像提到的那样,这在其他每台计
给定这段代码 public override void Serialize(BaseContentObject obj) { string file = ObjectDataStoreFold
我已经搜索了网络和我的服务器,但我无法找到我网站的 php.ini。我的网站出现以下错误。 Class 'finfo' not found Details G:\inetpub\wwwroot\lan
SQL 爱好者: 我正在尝试通过玩以下用例来挖掘我一些生疏的 sql 技能: 假设我们有一家有线电视公司,并且有跟踪的数据库表: 电视节目, 观看我们节目的客户,以及 观看事件(特定客户观看特定节目的
我正在设计一个使用 HTML5 网络组件(HTML 导入、影子 DOM、模板和自定义 HTML 元素)的网络应用程序,这些组件是通过普通 JavaScript(无框架)实现的。 Web 应用程序相当简
我是一名优秀的程序员,十分优秀!