java静态接口方法使用_使用静态方法对接口进行Java编程

java静态接口方法使用

In this quick tutorial, we would understand how static methods on interfaces helps us design better code and support the basic design principle: “Program to an interface, not an implementation”. You can find basic details of static methods at java 8 interface changes. Let’s get started with programming to interfaces design principle.

在本快速教程中,我们将了解接口上的静态方法如何帮助我们设计更好的代码并支持基本的设计原则:“编程到接口,而不是实现”。 您可以在java 8接口更改时找到静态方法的基本详细信息。 让我们开始编程接口设计原理。

Java编程接口 (Java Programming to Interfaces)

java programming to interfaces, java interface programming

This has been one of the most basic design principles. We should always use interfaces to effectively enforce development contracts while maintaining loose coupling of code.


这一直是最基本的设计原则之一。 我们应该始终使用接口来有效地执行开发合同,同时保持代码的松散耦合。

In Java we have seen several Collection classes follow these principles, where the concrete implementation only exposes the methods they implement from the interfaces, leaving the rest of the implementation in the private methods.

在Java中,我们已经看到几个Collection类遵循这些原则,其中具体的实现只公开了它们从接口实现的方法,而其余的实现则保留在私有方法中。

编程到接口实现的问题 (Problem with programming to interface implementation)

Let’s introduce an interface SuperHeros which gives us list of characters.

让我们介绍一个SuperHeros接口,它为我们提供了字符列表。

public interface SuperHeros {
	public List<String> getCharacters();
}

There are many comic studios and each studio would have their own concrete implementation.

有许多漫画工作室,每个工作室都有自己的具体实现。

public class DCSuperHeros implements SuperHeros {
	List<String> characters = List.of("Super Man", "Bat Man", "Wonder Woman", "Cyborg", "Flash");

	public List<String> getCharacters() {
		return characters;
	}
}
public class MarvelSuperHeros implements SuperHeros {
	List<String> characters = List.of("Spider Man", "Captain America", "Daredevil", "Wolverine", "Iron Man");

	public List<String> getCharacters() {
		return characters;
	}
}

The customers/client would use the code as below to write program against interfaces.

客户/客户将使用以下代码针对接口编写程序。

SuperHeros marvelHeros = new MarvelSuperHeros();
SuperHeros dcHeros = new DCSuperHeros();

What this does is that it inherently ties down the client code with concrete implementation and then it becomes difficult to change the concrete implementations later as this has already been exposed to the clients.

这是因为它固有地将客户端代码与具体实现绑定在一起,然后由于后来已​​经暴露给客户端,因此很难更改具体实现。

Even though we have interfaces, but we have provided our concrete implementation details to the customer and broken the design principle: Program to an interface, not an implementation. So how do we fix it and make it better?

尽管我们有接口,但是我们已经向客户提供了具体的实现细节,并打破了设计原则: 对接口进行编程,而不是实现 。 那么我们如何解决它并使其变得更好呢?

接口上的Java 8静态方法 (Java 8 static methods on interfaces)

Java 8 brought in a lot of new features and one of them is static method on interfaces. This very interesting and important feature is mostly overlooked by the developer community.

Java 8引入了许多新功能,其中之一是接口上的静态方法。 开发人员社区通常忽略了这个非常有趣且重要的功能。

In order to follow our design principle we would introduce static methods which are static factories to create an instance of the Concrete Implementation of the Superheroes. Let’s add the static method to the interfaces:

为了遵循我们的设计原则,我们将引入静态方法,这些方法是静态工厂,以创建Superheroes的具体实现的实例。 让我们向接口添加静态方法:

public interface SuperHeros {
	public List<String> getCharacters();

	static SuperHeros createDCSuperHeros() {
		return new DCSuperHeros();
	}

	static SuperHeros createMarvelSuperHeros() {
		return new MarvelSuperHeros();
	}
}

Additionally make your concrete implementations non public. Now the customers/client would use the code as:

另外,使您的具体实现不公开。 现在,客户/客户将使用以下代码:

SuperHeros marvelHeros = SuperHeros.createMarvelSuperHeros();
SuperHeros dcHeros = SuperHeros.createDCSuperHeros();

Now with help of static methods we were able to write static factories for our SuperHeros implementations.

现在,借助静态方法,我们能够为SuperHeros实现编写静态工厂。

Limitation: This approach can be applied only when you have control over the interface, if it’s a third party interface then it won’t work and you are better off with the general approach.

局限性 :仅当您对界面有控制权时,才可以应用此方法,如果它是第三方界面,那么它将无法工作,并且您最好使用常规方法。

摘要 (Summary)

In this quick tutorial we learnt how to program to interfaces and how a static method in interfaces helps us achieve this.

在本快速教程中,我们学习了如何对接口进行编程以及接口中的静态方法如何帮助我们实现这一目标。

And to wrap up, you’ll find the source code to this article on Github.

最后,您可以在Github上找到本文的源代码。

翻译自: https://www.journaldev.com/18278/java-programming-interfaces-static-methods

java静态接口方法使用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值