OO design recommendations for J2EE applications

Source: Internet
Author: User
Tags coding standards

 

OO design recommendations for J2EE applications

OO design recommended for J2EE applications

It's possible to design a J2EE application so badly that, even if it contains beautifully written Java code at an individual object level, it will still be deemed a failure. A J2EE application with an excellent overall design but poor implementation code will be an equally miserable failure. unfortunately, many developers spend too much time grappling with the J2EE APIs and too little ensuring they adhere to good coding practice. all of Sun's J2EE sample applications seem to reflect this.

In my experience, itIsn' tPedantry to insist on adherence to good oo principles: it brings real benefits.

In my experience, we don't need to follow the good J2EE design principles: It really brings benefits.

Important

OO design is more important than any particle implementation technology (such as J2EE, or even Java). Good programming practices and sound OO design underpin good J2EE applications. Bad Java code is bad J2EE code.

OO design is more important than other special implementation technologies (such as J2EE or even Java. Good programming practices and good OO design support good J2EE applications. Bad JavaCodeIs a bad J2EE code.

Some "coding standards" issues-especially those relating to OO design-are on the borderline between design and implementation: for example, the use of design patterns.

Some "code standards" problems-especially those related to OO design-are between design and implementation:

The following section covers some issues that I 've seen cause problems in large Code bases, especially issues that I haven't seen covered elsewhere. this is a huge area, so this section is by no means complete. some issues are matters of opinion, although I'll try to convince you of my position.

The following sections cover some of the issues I have encountered in large code libraries, especially those I have not met elsewhere. This is a huge area, so this chapter is not thorough. Some questions are my opinion, though I will try to persuade you.

Take every opportunity to learn from the good (and bad) Code of others, inside and outside your organization. useful sources in the public domain include successful open source projects and the code in the core Java libraries. license permitting, it may be possible to decompile interesting parts of specified cial products. A professional programmer or effecect cares more about learning and discovering the best solution than the buzz of finding their own solution to a particle problem.

Seize every opportunity to learn from good code elsewhere, within your group. UsefulSource codeThe public domain contains successful open-source projects and core Java package code. If permitted, You Can decompile the sections of interest to commercial products. Compared to finding their own solutions for a specific problemProgramMembers or architects are more interested in learning and discovering the best solutions.

Achieving loose coupling with Interfaces

Reducing coupling with Interfaces

The "first principle of reusable object-oriented design" advocated by the classic Gang of Four design patterns book is: "program to an interface, not an implementation ". fortunately, Java makes it very easy (and natural) to follow this principle.

"Principle 1 reusable OO design" was proposed by four typical design models. "Programming interfaces, not specific implementation ". Fortunately, Java makes it simple and natural to use this principle.

Important

Important

Program to interfaces, not classes. this decouples interfaces from their implementations. using loose coupling between objects promotes flexibility. to Gaim maximum flexibility, declare instance variables and method parameters to be of the least specific type required.

Interface-oriented programming rather than classes. This decouples the interfaces and their implementations. The use of loose coupling between objects improves scalability. To achieve maximum scalability, declaring instance variables and method parameters should give the maximum type requirement.

Using interface-based architecture is particle ly important in J2EE applications, because of their scale. programming to interfaces rather than concrete classes adds a little complexity, but the rewards far outweigh the investment. there is a slight performance penalty for calling an object through an interface, but this is seldom an issue in practice.

The interface-based architecture is particularly important in J2EE because of its scale. Interface-oriented programming, rather than a specific class, adds a little more complexity, but the benefits are far greater than investment. There is a slight loss of performance here. An object is referenced through the interface. However, in practice, this is not a problem.

A few of the specified advantages of an interface-based approach include:

Many advantages of this interface-based programming include:

    • the ability to change the implementing class of any application object without affecting calling code. this enables us to parameterize any part of an application without breaking other components.

    • change the implementation class of any application object without affecting the ability to call code. This allows us to parameterize any part of the application without breaking other components.

    • total freedom in implementing interfaces. there's no need to commit to an inheritance hierarchy. however, it's still possible to achieve code reuse by using concrete inheritance in interface implementations.

    • completely free implementation interface. You do not need to specify an inheritance level here. However, it may still complete code reuse and use a specific inheritance layer in interface implementation.

    • the ability to provide simple test implementations and stub implementations of application interfaces as necessary, facilitating the testing of other classes and enabling multiple teams to work in parallel after they have agreed on interfaces.

    • provides the necessary capabilities for simple test implementation and application interface stub implementation, promote testing of other classes and work parallel to multiple teams after they agree to the interface.

Adopting interface-based architecture is also the best way to ensure that a J2EE application is portable, yet is able to leverage vendor-specific optimizations and enhancements.

Interface-based architecture is also the best way to ensure that J2EE applications are portable and can be optimized and improved based on specific vendors.

Interface-based architecture can be used tively combined with the useReflection for configuration(See below ).

The interface-based architecture can effectively combine with reflection.

Prefer object composition to concrete inheritance

Try to use combination instead of Inheritance

The second basic principle of object-oriented design emphasized in the gof book is "favor object composition over class inheritance". Few developers appreciate this wise advice.

The 2nd Basic Principle of OO design highlighted in the gof book is "Support for object combinations over class inheritance ". A large number of developers appreciate this wise advice.

Unlike terraolder versions, such as C ++, Java distinguishes at a language levelConcrete inheritance(The inheritance of method implementations and member variables from a superclass) andInterface inheritance(The implementation of interfaces ). java allows concrete inheritance from only a single superclass, but a Java class may implement any number of interfaces (including, of course, those interfaces implemented by its ancestors in a class hierarchy ). while there are rare situations in which multiple concrete inheritance (as permitted in C ++) is the best design approach, java is much better off avoiding the complexity that may arise from permitting these rare legitimate uses.

Unlike many older languages, such as C ++, Java distinguishes a language hierarchy from class inheritance (from parent class relay method and member variable) and interface inheritance (Implementation interface ). Java only allows single inheritance, but Java classes can implement any number of interfaces (including, of course, these interfaces are implemented at a class level ). However, in rare cases, multi-class inheritance (for example, allowed in C ++) is the best design method. Java better avoids the complexity of allowing these rare legal uses.

Concrete inheritance is enthusiastically embraced by most developers new to Oo, but has disadvantages. class hierarchies are rigid. it's impossible to change part of a class's implementation; by contrast, if that part is encapsulated in an interface (using delegation and the strategy design pattern, which we'll discussed below), this problem can be avoided.

Class inheritance is popular for most people who are new to Oo, but it also has many disadvantages. Class inheritance is rigid. It is impossible for him to change the implementation of some code: on the contrary, if this part is encapsulated in an interface (we will discuss it later with the delegate and policy design patterns ), this problem will be avoided.

Object Composition(In which new functionality is obtained by grouping or composing objects) is more flexible than concrete inheritance, and Java interfaces make delegation natural. Object composition allows the behavior of an object to be alteredRun Time,Through delegating part of its behavior to an interface and allowing callers to set the implementation of that interface. The Strategy and State Design Patterns rely on this approach.

Object combinations are more extensible than class inheritance, and Java interfaces have long been delegated. Object combination allows an object's behavior to be changed at runtime. By entrusting some of its behavior to an interface and allowing callers to set the implementation of this interface, the policy and status design pattern depend on this path.

To clarify the distinction, let's consider what we want to achieve by inheritance.

To explain the difference, we use inheritance to accomplish what we want.

Abstract inheritance enablesPolymorphism:The substitutability of objects with the same interface at run time. This delivers much of the value of object-oriented design.

Abstract inheritance has polymorphism: Alternative objects have the same interface at runtime. This shows the value of OO design.

Concrete inheritance enables both polymorphism and more convenient implementation. code can be inherited from a superclass. thus concrete inheritance is an implementation, rather than purely a design, issue. concrete inheritance is a valuable feature of any OO language; but it is easy to overuse. common mistakes with concrete inheritance include:

Class inheritance ensures polymorphism and more implementation. The code can be inherited from a parent class. Class inheritance is an implementation method, compared to pure design issues. Class inheritance is a valuable feature for any OO language, but it is also easily abused. Common Errors inherited from classes include:

  • Forcing users to extend an abstract or concrete class, when we cocould require implementation of a simple interface. this means that we deprive the user code of the right to its own inheritance hierarchy. if there's normally no reason that a user class wocould need it's own custom superclass, we can provide a convenient abstract implementation of the method for subclassing. thus the interface approach doesn't preclude the provision of convenient superclasses.

  • Forces users to expand an abstract class or a specific class when a simple interface is required. This means that we deprive the user code of its own class inheritance. If there is no reason why a user class requires its own superclass, we will provide a convenient abstract implementation method as a subclass. Therefore, the interface path does not include the convenience parent class specification in advance.

  • Using concrete inheritance to provide helper functionality, by subclasses calling helper methods in superclasses. What if classes outside the inheritance hierarchy need the Helper functionality? Use object composition, so that the Helper is a separate object and can be shared.

  • Use class inheritance to improve the help function, and use subclass to call the help methods in the parent class. What if this class needs this help function outside the hierarchy? Object combination, so that this help class is a separate object that can be shared.

  • Using abstract classes in place of interfaces. abstract classes are very useful when used correctly. The template method design pattern (discussed below) is usually implemented with an abstract class. However, an abstract class isNotAn alternative to an interface. It is usually a convenient step inImplementationOf an interface. don't use an abstract class to define a type. this is a recipe for running into problems with Java's lack of multiple concrete inheritance. unfortunately, the core Java libraries are poor examples in this respect, often using abstract classes where interfaces wocould be preferable.

  • Use abstract classes instead of interfaces. Abstract classes are very useful when we consider them correctly. This template method design pattern (discussed below) is usually implemented using an abstract class. However, an abstract class is not the only option to replace interfaces. It is usually a convenient step in the implementation of the interface. Do not use an abstract class to define a type. This is because Java lacks multi-inheritance. Unfortunately, there are too few examples of Java core packages in this regard. It is better to use abstract classes in interfaces.

Interfaces are most valuable when kept simple. the more complex an interface is, the less valuable is modeling it as an interface, as developers will be forced to extend an abstract or concrete implementation to avoid writing excessive amounts of code. this is a case where correct interface granularity is vital; interface hierarchies may be separate from class hierarchies, so that a particle class need only implement the exact interface it needs.

Keeping interfaces simple is the most valuable. The more complex this interface is, the smaller the construction value of this interface. Developers will be forced to inherit an abstract or specific implementation to avoid a large amount of code. Correct interface granularity is also important; interface inheritance may be separated from class inheritance, so that a specific class only implements the class it requires.

Important

Important

Interface inheritance (that is, the implementation of interfaces, rather than inheritance of functionality from concrete classes) is much more flexible than concrete inheritance.

Interface inheritance (implementing interfaces rather than inheriting specific functions from classes) is more scalable than class inheritance.

Does this mean that concrete inheritance is a bad thing? Absolutely not; concrete inheritance is a powerful way of achieving code reuse in OO ages. however, it's best considered an implementation approach, rather than a high-level design approach. it's something we shoshould choose to use, rather than be forced to use by an application's overall design.

Which means class inheritance is a bad thing? No; class inheritance is a powerful way to complete code reuse in the OO language. However, it is better to be recognized as an implementation approach, rather than an advanced design approach. It is something we can choose to use, rather than being forced to use the excessive design through an application.

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.