Odgiiv
Odgiiv

Reputation: 713

Java reusablitity

What is the proper way to make java reusable components that is capable of used in various applications. I mean for example I'm doing a application that has its own user interface and database and etc. If I want to make this app reusable for many other applications as a component of other applications. For example one feature of my first app may needed by other app. So how to make it possible for other apps to use this feature of my app without changing my original code. What are the proper ways to achieve this re usability.

Upvotes: 0

Views: 469

Answers (5)

matthudson
matthudson

Reputation: 182

This is a rather broad question. As such, I am offering broad suggestions:

  • Know your OO basics. Inheritance, encapsulation, polymorphism. It gets crazier from there on out.
  • Learn about design patterns, start observing them in applications you already use.
  • Look at popular open libraries to see how they implement patterns and modules.
  • Try things in sandbox projects. Grow your knowledge in clean environments.
  • Since you mention Java, check out the Spring Framework.

Hope that helps.

Upvotes: 1

Ashish Chaurasia
Ashish Chaurasia

Reputation: 1737

Sun Microsystems, the creators of the Java language, have at last recognized this need, and have released the Java Beans Component Architecture. Java Beans are, quite simply, reusable controls written in Java, for Java application development.

Beans are "capsules" of code, each designed for a specific purpose. The advantage of Java Beans over standard programming controls is that Beans are independent. They are not specific to operating systems or development environments. A Bean created in one development environment can be easily copied and modified by another. This allows Java Beans greater flexibility in enterprise computing, as components are easily shared between developers.

Upvotes: 0

Raja Asthana
Raja Asthana

Reputation: 2100

You need code in such a way that your components are loosely coupled. Then the re-usability is very much high. Take a look at this and this.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533530

Write something simple which does what it does very well. Document it and unit test it and make it open source.

Upvotes: 3

Bozho
Bozho

Reputation: 597114

Have the the reusable components in another project (e.g. "common") and package them as .jar. Then include that jar in the projects where it's needed.

Extracting a separate project might be tricky though. You should observer the following:

  • the common components should not be dependent on anything in the higher level of abstraction (i.e. you services must not have any UI-related dependencies)
  • the internals of the components must not be visible to the application using them. I.e. your jar should expose a minimum API.

You have a couple of options for the mechanics of packaging:

  • simple IDE-dependant packaging - declare a inter-project dependency. On build export the jar and put on the classpath of the client application
  • Maven/Ivy - install the dependency in a repository (local or remote) and use the dependency resolution mechanisms of maven/ivy

Upvotes: 2

Related Questions