Java Miscellaneous (7) -- interfaces & components and containers

Source: Internet
Author: User
Finally, I calmed down and continued to write the subsequent articles on this topic. The first six articles mainly talked about some j2se experiences and feelings. At present, the Java application scope has been occupied by J2EE in a considerable field, some even claim that Java has been replaced by J2EE. I don't know how to understand the so-called J2EE (Java2 Enterprise Edition), that is, Java enterprise-level applications?

The author's point of view is that the development of technology conforms to the changing trend of the world, from c/s to B/S mode, from the client perspective, enterprise-level applications or e-commerce fields do not care about client maintenance. This task has been handed over to any PC for maintenance by browsers. From the server perspective, in the past, the TCP/IP protocol implementation carrier serversocket in C/S was replaced by web server iner, such as Tomcat, JBoss, and WebLogic, which everyone is familiar. All in all, the transformation is to make Java technology better serve the human production and life.

Someone may ask, do you want to skip j2se directly to learn J2EE? I certainly disagree. In fact, some people are taking this path, but the author's own experience is that it is because of the strong foundation of j2se that it will lead to smooth sailing on the J2EE learning road, there is no confusion in knowledge points. Here is a simple example:

The author once discussed the differences between the two methods with university students:
Arraylist list = new arraylist (); // The author does not disagree, but at least does not agree
List list = new arraylist (); // supported by the author

I once argued with my classmates for a few hours. He did not say that the first method is more scientific, but the second method is completely unnecessary. I can't fully convince him, but I think that good habits and consciousness are the purpose of programming interfaces at any time to achieve decoupling and scalability. Let's start with the interface to enter the J2EE world:

1. J2EE and interfaces

Each version of J2EE corresponds to a fixed version of JDK, j2ee1.4 corresponds to jdk1.4, and now the newer version is jdk5.0, and there will naturally be J2EE 5.0. In fact, I have been using j2ee1.4, but it does not matter. You can take a look at any version of J2EE API. I want to declare a concept first. J2EE is also derived from Java, so the underlying operations are still called to many j2se libraries. Therefore, we recommend that you master the mainstream j2se technologies first.

J2EE APIs have one feature. Many familiar packages, such as Java. JMS, javax. servlet. HTTP, and javax. EJB, are interface-oriented and have fewer implementation classes. In fact, when you are actually using it, more than 60% of them are repeatedly checking several implementation class API functions under the javax. servlet. http package. Other packages are rarely used. Before learning a technology, the author suggests that it is necessary to have an understanding of the overall framework. J2EE aims to standardize the implementation through interface declarations, any third-party vendor wants to provide its own brand implementation on the premise that it also complies with the rules defined by these interfaces. If you have a good understanding of interfaces on the road of j2se learning, the experience here will be very profound. For example:

Public interface MP3 {
Public void play ();
Public void record ();
Public void stop ();
}

If I define this simple interface and publish it out, it is required that any third-party company wants to launch its own product named MP3, that is, at least provide the specific implementation of methods in the interface. This meaning is far more than object-oriented polymorphism. Only when the vendor complies with the J2EE interface definition can the J2EE programmers in the world design a program for a unified interface, in the end, you don't need to change the code because the implementation classes of different vendors have different features. In essence, no matter which vendor implementation completes the work within the scope of responsibility. This is what I want to emphasize all the time for the idea of interface programming.

What are the advantages of interfaces? We imagine that applemp3, sonymp3, and samsungmp3 all implement the MP3 interface, so they all have the play, record, and stop functions. We don't need to know the specific implementation of the MP3 product when we use a component. As long as we see that the interface definition knows that this object has three functions, we can use it. So the following businesses can be expanded from three brands to any one at any time, the Project Manager writes the method declaration in 10 interfaces, and then sends the details to the programmers, because the interfaces have been unified (that is, the format of each method's input and output has been unified), the manager can move around with a cup of coffee every day just by paying attention to the global business. ^_^:
Public MP3 create ();
Public void copy (MP3 MP3 );
Public MP3 getmp3 ();

Finally, a simple example is used to describe the interface: A flashlight with battery No 5 can be loaded into any brand of battery No 5, as long as it complies with the specifications of battery No 5, after loading, you can only feel that the flashlight is working on its function. Then the manufacturer that produces the flashlight and the manufacturer that produces the No. 5 battery can completely remove the dependency and develop their own products freely, because they comply with the shape and positive and negative position of the No. 5 battery. Can you have more experience with the interface?

2. Components and containers
The interface is a necessary idea for the author to emphasize the J2EE learning path. The other is the conventional concept of components and containers. Many teaching materials and professional websites have said that the core of J2EE is a set of norms and guidelines, emphasizing that the core concept of J2EE is component + container, which is indeed understandable. With the emergence of more and more J2EE frameworks, each framework generally has a corresponding container.

A container is a collection tool used to manage component behaviors. Component behaviors include interactions with external environments, component lifecycles, and cooperative dependencies between components. J2EE includes web containers, application client containers, EJB containers, and Applet client containers. However, in my opinion, the concept of containers has become a bit vague. We are familiar with open-source frameworks with powerful functions, such as Hibernate, struts2, spring, and JSF, among them, Hibernate encapsulates the management of transactions and sessions based on JDBC, greatly facilitating the tedious code for database operations. In this sense, it is close to the concept of container, EJB object beans are gradually replaced by Persistence frameworks represented by hibernate.

A component is a reusable code unit. It generally represents one or more independent functional modules. There are many types of Components in J2EE, generally, EJB components, Dao components, client components, or application components are packaged. war ,. jar ,. jar ,. ear, each component is described by an XML descriptor file in a specific format, and the server-side components must be deployed on the application server for use.

After a little understanding of components and containers, another important concept is the layered model, the most famous of which is the MVC three-layer model. In a large project or project, in order to enable programmers in the front-end and back-end modules to work at the same time to improve development efficiency, the most important thing is to realize the coupling relationship between layers, the purposes of many layered models and the pursuit of open-source frameworks are the same. In my opinion, a complete web project has the following layers:

A) Presentation layer (JSP, HTML, JavaScript, Ajax, flash and other technologies support it)
B) control layer (struts, JSF, webwork, and other frameworks are supported based on Servlet and are responsible for directing specific request data (sometimes unload and reload) to model layer objects suitable for processing it)
C) model layer (I think the best framework at present is spring. The essence is to process the data forwarded by the presentation layer through the control layer, which contains a large amount of business logic)
D) data layer (hibernate, JDBC, EJB, etc. The model layer completes processing and persists to the database)

Of course, this is just my personal point of view. It is only for your reference. If you want to completely separate these layers, it is a big project, the task can be completed simply by adding manpower. Although the Mythical man-month has clearly stated that increasing manpower cannot increase efficiency, this is largely because the work done by each other has a sequential dependency or a huge gap in difficulty and workload. Of course, the ideal state is impossible in the real world, but we should always work in this direction. At the beginning, the programming for interfaces was promoted. Even if it was a small detail, writing a list = new arraylist () statement can also reflect the idea of using interfaces everywhere. Anyway, this is just the beginning. I will discuss some experiences with the J2EE technology and framework I have used.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.