简体   繁体   English

我试图了解Java中“程序到接口”的含义

[英]I am trying to understand what it means by “Program to an interface” in Java

I am trying to understand what it means by "Program to an interface" in Java. 我试图理解Java中“程序到接口”的含义。 Should we program to interface because of changing client needs. 由于客户需求的变化,我们是否应该编写接口程序。 Is this the only reason 这是唯一的原因

how will below program run into problem from client and developer perceptive? 下面的程序将如何引起客户和开发人员的感知问题?

public interface SuperHeros {
    public List<String> getCharacters();
}
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;
    }
}

**SuperHeros marvelHeros = new MarvelSuperHeros(); //Line 15
SuperHeros dcHeros = new DCSuperHeros(); //Line 16

what is the problem with line 15 and 16. Can somebody please explain with an use case which will run this example into a problem because of coding to implementation?** 第15行和第16行有什么问题?有人可以解释一下用例,由于对实现进行编码,该用例会使该示例陷入问题?**

Imagine you had a library that is used by both a CLI program and a server. 假设您有一个供CLI程序和服务器使用的库。 This library uses a cache that is just a map of key-value pairs. 该库使用的高速缓存只是键值对的映射。

The interface you'd want the library to use would be Map because for the CLI program you might be able to use HashMap, but for the server, you would probably want to use ConcurrentHashMap for thread safety. 您希望库使用的接口是Map,因为对于CLI程序,您可能可以使用HashMap,但是对于服务器,您可能要使用ConcurrentHashMap以确保线程安全。 Which implementation you use would be injected at runtime. 您使用的实现将在运行时注入。 You might even want to use a Redis implementation. 您甚至可能想要使用Redis实现。

Another similar example would be a library that manages a data repository. 另一个类似的示例是管理数据存储库的库。 You might need an implementation that uses AWS or Azure storage, and an implementation that uses MongoDB or SQL. 您可能需要使用AWS或Azure存储的实现,以及使用MongoDB或SQL的实现。 You would want that library to present the same interface to the programs that uses it, and let the implementation that is used be selected at runtime. 您希望该库为使用该库的程序提供相同的接口,并在运行时选择使用的实现。

Or for something less boring, imagine you are programming Super Mario Bros. 2. There are 4 characters that you can choose from: Mario, Luigi, Princess Peach, and Toad. 或者,如果您不想那么无聊,可以想象您正在编写《超级马里奥兄弟》。2.您可以选择4个字符:马里奥(Mario),路易吉(Luigi),公主桃(Peach)和蟾蜍(Toad)。 Each one behaves slightly differently when you press the D-pad or A or B buttons, but they all have the same interface. 当您按下D-pad或A或B按钮时,每个按钮的行为略有不同,但是它们都有相同的界面。

Regarding Line 15/16 关于15/16行

You could have a wiki system that is used by 2 websites, one is DC and one is marvel. 您可能有2个网站使用的Wiki系统,一个是DC,另一个是奇迹。

A class that is used by both would take in the interface 两者都使用的类将进入接口

SuperHeros marvelHeros = new MarvelSuperHeros(); //Line 15
SuperHeros dcHeros = new DCSuperHeros(); //Line 16

SuperHeroDataBase marvelDataBase = new SuperHeroDataBase(marvelHeros);
// these aren't necessarily in the same program, could be 2 totally different server programs that use the same library
SuperHeroDataBase dcDataBase = new SuperHeroDataBase(dcDataBase);

with the ctor for SuperHeroDataBase: 与SuperHeroDataBase的ctor一起使用:

public SuperHeroDataBase(SuperHeros superHeros){
    this.superHeros = superHeros);
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWT:我正在尝试学习 Java,但无法理解以下程序 - AWT: I am trying to learn Java and was unable to understand the following program 我试图理解Java中的注释是什么,但是Google上的所有可用信息都很难理解 - What are Annotations in Java, I am trying to understand but all the information available on Google is very difficult to understand 我试图描述这个 java 打印语句“ (((0xFF &lt;&lt; (i * 8)) &amp; test)))) ”是什么意思 - I am trying to depict what this java print statement “ (((0xFF << (i * 8)) & test)))) “ means 我正在尝试使用Java derby EmbeddedDriver创建表,但我不明白这些错误是什么意思? - I am trying to create a table using java derby EmbeddedDriver but i don't understand what these errors mean? Lambda function 返回 - 试图理解这意味着什么 - Lambda function returned - trying to understand what this means 试图理解这段代码对 HttpURLConnection 的意义 - Trying to understand what this code means for HttpURLConnection 我无法理解 java 中 generics 中传递接口的使用 - I am not able to understand the use of passing interface in generics in java 试图了解Java中的“驱动程序”程序 - Trying to understand “driver” program in java 我的代码中有什么错误? 我想从 2 天开始理解它? - what is the error in my code? I am trying to understand it from 2 days? 我想了解我的作业,Java Lisp算术表达式 - I am trying to understand my homework, java Lisp arithmetic expression
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM