- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我自己尝试设计模式和原则并提出了一个问题。之前,为糟糕的编码风格习惯感到抱歉!!
在这种情况下,我有一个类似 ITest 的接口(interface):
public interface ITest
{
public void method1();
}
然后将方法和字段(如果有的话)实现到具体类 B 中,如下所示:
public class B implements ITest
{
//This is the method from the interface
@Override
public void method1()
{
System.out.println("method1");
}
//This is another method in class B
public void method2()
{
System.out.println("method2");
}
}
现在在应用程序代码中我是这样写的:
public class Main
{
public static void main(final String args[]) throws Exception
{
//One principle says:
//programm to an interface instead to an implementation
ITest test = new B();
//method from interface
test.method1();
//this method is not accessible because not part of ITest
test.method2(); //compile-time error
}
}
您看到类 B 中的方法 2() 不可用,因为对于 ITest 的接口(interface)。现在,如果我需要这个“重要”方法怎么办?有几种可能性。我可以在接口(interface)中对其进行抽象,或者使 B 类抽象并扩展到另一个类等,或者在 main() 方法中进行引用,例如:
B test = new B();
但是这样就违反了原则。因此,我将界面修改为:
public interface ITest
{
//A method to return the class-type B
public B hook();
public void method1();
}
并将实现放在类 B 中:
public class B implements ITest
{
//this returns the object reference of itself
@Override
public B hook()
{
return this;
}
//This is the method from the interface
@Override
public void method1()
{
System.out.println("method1");
}
//This is the 'important' method in class B
public void method2()
{
System.out.println("method2");
}
}
现在,在我的 main() 方法中,我可以使用一个小钩子(Hook)或链接机制调用这两个方法,而无需引用新对象,也不会违反设计原则,而且我不需要额外的类来进行扩展或抽象。
public class Main
{
public static void main(final String args[])
{
//programm to an interface instead into an implemintation
ITest test = new B();
//method from interface
test.method1();
//method2 will not be accessible from ITest so we referencing B through a method hook()
//benefits: we don't need to create extra objects nor additional classes but only referencing
test.hook().method2();
System.out.println("Are they both equal: "+test.equals(test.hook()));
}
}
此外,我还可以封装、继承和抽象其他方法、字段等。这意味着,我可以创建更复杂、更灵活的层次结构。
我现在的问题:这是一种反模式、糟糕的设计原则,还是我们可以从中受益?
感谢观看。 :-)
最佳答案
Is this a kind of anti-pattern, bad design-principle or could we benefit from this?
是的,这是一个糟糕的模式。
问题源于您已将 ITest
与 B
紧密耦合。假设我想创建一个新的 ITest
实现 - 我们称它为 C
。
public class C implements ITest
{
@Override
public B hook()
{
// How do I implement this?
}
@Override
public void method1()
{
System.out.println("method1");
}
}
我们无法实现此方法。唯一合理的做法是返回 null
。这样做会迫使我们界面的任何用户不断执行防御性空检查。
如果他们每次都必须在使用该方法的结果之前进行检查,那么他们还不如执行一个instanceof
并转换为B
。那么你要增加什么值(value)?您只是让界面变得不那么连贯且更加困惑。
关于java - 这是一种反模式还是违反了一些设计原则?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50253508/
GitOps描述了一种使用植根于 Git 版本控制系统的方法来操作和管理软件的方法。使用基于 GitOps 的工作流,通过要求将系统的特征定义为 Git 存储库中的文件,可以更轻松地开发、部署、维护和
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。 关闭 6 年前。
命令行货币转换器应用程序,提示用户输入源货币、源货币代码和目标货币代码,例如 C:\workspace> java CurrencyConverter 100.50 EUR GBP 应用程序返回源金额
得到这个实体: /** * @ORM\Table(name="shop_payment_details") * @ORM\Entity(repositoryClass="Acme\ShopBund
我有一个原则实体,无需调用 persist 或 flush 即可持久保存到数据库中。 我在下面很简单地重现了这个问题。正如您将看到的,此脚本从名为 MyEntity 的数据库表中加载一行,并获取一个以
在我的编程实践中,我经常遇到客户端和服务器端脚本之间数据重复的问题。 在这种情况下,我们可以讨论客户端的 JavaScript 和服务器端的 PHP 或 C# (ASP.NET)。 比方说,我有一段
简介 我在写关于继承问题的硕士论文并解决了一些问题 表明存在继承问题的指标。 像下面的例子: 示例 public static String getAnimalNoise(Animal animal)
就目前而言,这个问题不适合我们的问答形式。我们希望答案得到事实、引用资料或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
当我注意到this answer时,我一直在阅读里氏替换原理。 。它有一个 Circle 和一个 ColoredCircle 类型,其中 ColoredCircle 的构造函数需要一个额外的参数; 颜
这段代码是否违反了DRY原则? if (notAuthorized) { return sendErrorCode(new ForbiddenException()) } else if (n
我在查询中使用 Doctrine 2 的结果缓存来检索用户(消息传递应用程序)的新消息数量: $query->useResultCache(true, 500, 'messaging.nb_new_m
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 8 年前。 Improve this ques
如何设置包含类名的变量,例如 android.util.Log 中的 TAG,同时尊重 Dont-Repeat-Yourself? 以下是一些可能性: 在 Google 代码中,它的常用用法如下 pu
我有以下查询: $roles = array(); $roles[] = 'ROLE_SUPER_ADMIN'; $roles[] = 'ROLE_ADMIN';
下面的代码违反了哪一条 SOLID 原则? public class A { void hello(){ //some code here } } public class B ext
我目前有一个 Message_Repository 类,它有如下方法: getLocationDetailsByID($messageId), getCustomerDetailsById($mess
我不知道它到底叫什么,但现在我将它称为“非空测试”。在 C# 8 中有一个新的行为允许测试一个对象是否不为空,例如: Foo foo = new Foo(); if(foo is { }) {
我正在学习 Doctrine。我在多对多关系中有两个实体 Article 和 Category,我正在尝试获取所有不是特定文章的类别。 文章实体: class Article extends Base
在阅读了一本书和一篇在线文章中有关 SOLID 代码的内容后,我想重构一个现有的类,使其与“SOLID”兼容。 但我想我迷路了,尤其是依赖注入(inject):当我想实例化类的一个对象时,我需要“注入
我的项目中有类似的东西,这个项目已经完成了(它正在运行)我只想知道 SOLID 原则是否可以接受 static public class Tools { static public GetPr
我是一名优秀的程序员,十分优秀!