object oriented design principles

JAI BLOG Taste My Java Examples, materials and Many More Of Java http://www.sattvaq.com/jai Object-Oriented Design Prin...

2 downloads 169 Views 47KB Size
JAI BLOG Taste My Java Examples, materials and Many More Of Java http://www.sattvaq.com/jai

Object-Oriented Design Principles Object-Oriented Design Principles

We covered object-oriented programming basics in Chapter 3 and more advanced OOP topics in Chapter 4. In this chapter, we delve still more deeply into OOP concepts. The chapter begins with an in-depth investigation of interfaces. We will discuss how interfaces are different from abstract classes and which construct to use in a given situation. In the second section, we introduce you to the object composition principle that says “favor composition over inheritance.” The third and fourth sections cover one of most popular topics in OOP: design patterns. You will learn the basic concepts behind design patterns and how they relate to OOP. We’ll focus on the singleton, factory, abstract factory, and data access object (DAO) design patterns.

Interfaces In general, an interface refers to a common boundary or interconnection between two entities (which could be human beings, systems, concepts, machines, etc.). For instance, a keyboard in a computer system provides an interface between a human being and the computer. A natural language such as English is an interface between two humans that allows them to exchange their views. In Java, an interface is a set of abstract methods that defines a protocol (i.e., a contract for conduct). Classes that implement an interface must implement the methods specified in the interface. An interface defines a protocol, and a class implementing the interface honors the protocol. In other words, an interface promises a certain functionality to its clients by defining an abstraction. All the classes implementing the interface provide their own implementations for the promised functionality. Let’s elucidate Java interfaces with an example. Consider the java.lang.Comparable interface that specifies the following protocol: public interface Comparable{ public int compareTo(Object o); // Intent is to compare this object with the specified object // The ret urn type is integer. Returns negative, // zero or a positive value wh en this object is less than, // equal to, or greater than the specifi ed object }

page 1 / 2

JAI BLOG Taste My Java Examples, materials and Many More Of Java http://www.sattvaq.com/jai

Unrelated classes can provide their own implementations when they implement Comparable. These unrelated classes have one aspect in common: they all follow the specification given by Comparable, and it is left to the implementation of these individual classes to implement compareTo() accordingly. Data structures supported in the java.util package implement this interface. If you want to use the general algorithms provided in the library, you have to implement this interface. For example, consider the sample implementation of max() method in java.util.Collections. It is meant for finding the maximum element in a collection. It uses the Comparable interface, and the elements in the collection must provide the implementation for the compareTo() method. The algorithms (i.e., clients of the interface) are completely ignorant about how the compareTo() method is implemented. But the clients know the contract that insures the availability of the compareTo() method, hence clients can use it. This confers a very important advantage: when a method takes an interface as an argument, you can pass any object that implements that interface (due to runtime polymorphism).

page 2 / 2 Powered by TCPDF (www.tcpdf.org)