Object oriented thought process pdf free download






















Starting from the preliminaries of the language and the basic principles of OOP, this textbook moves gradually towards advanced concepts like exception handling, multithreaded programming, GUI support by the language through AWT controls, string handling, file handling and basic utility classes.

In addition, the well-planned material in the book acts as a precursor to move towards high-end programming in Java, which includes the discussion of Servlets, Java Server Pages, JDBC, Swings, etc. The book is highly suitable for all undergraduate and postgraduate students of computer science, computer applications, computer science and engineering and information technology. It starts with the first steps in programming and software development like variables, data types, conditional statements, loops and arrays and continues with other basic topics like methods, numeral systems, strings and string processing, exceptions, classes and objects.

After the basics this fundamental programming book enters into more advanced programming topics like recursion, data structures lists, trees, hash-tables and graphs , high-quality code, unit testing and refactoring, object-oriented principles inheritance, abstraction, encapsulation and polymorphism and their implementation the C language. It also covers fundamental topics that each good developer should know like algorithm design, complexity of algorithms and problem solving.

It teaches the major programming concepts and way of thinking needed to become a good software engineer and the C language in the meantime. It is a great start for anyone who wants to become a skillful software engineer.

The books does not teach technologies like databases, mobile and web development, but shows the true way to master the basics of programming regardless of the languages, technologies and tools. It is good for beginners and intermediate developers who want to put a solid base for a successful career in the software engineering industry. The book is accompanied by free video lessons, presentation slides and mind maps, as well as hundreds of exercises and live examples.

This book takes a do-it-yourself approach when it comes to writing code, because the best way to really learn a programming language is by writing code. You are encouraged to type code into Firebug's console, see how it works and then tweak it and play around with it. OOP promotes good design practices, code portability, and reuse—but it requires a shift in thinking to be fully understood. Readers will learn to understand the proper uses of inheritance and composition, the difference between aggregation and association, and the important distinction between interfaces and implementations.

While programming technologies have been changing and evolving over the years, object-oriented concepts remain a constant—no matter what the platform. This revised edition focuses on the OOP technologies that have survived the past 20 years and remain at its core, with new and expanded coverage of design patterns, avoiding dependencies, and the SOLID principles to help make software designs understandable, flexible, and maintainable.

The Object-Oriented Thought Process Third Edition Matt Weisfeld An introduction to object-oriented concepts for developers looking to master modern application practices. OOP promotes code portability and reuse, but requires a shift in thinking to be fully understood. Before jumping into the world of object-oriented programming languages, you must first master The Object-Oriented Thought Process.

Written by a developer for developers who want to make the leap to object-oriented technologies as well as managers who simply want to understand what they are managing, The Object-Oriented Thought Process provides a solution-oriented approach to object-oriented programming. Readers will learn to understand object-oriented design with inheritance or composition, object aggregation and association, and the difference between interfaces and implementations.

Readers will also become more efficient and better thinkers in terms of object-oriented development. This revised edition focuses on interoperability across various technologies, primarily using XML as the communication mechanism. NET, and Java. Weisfeld has published many articles in major computer trade magazines and professional journals.

Provides information on analyzing, designing, and writing object-oriented software. You can find a whole range of programming textbooks intended for complete beginners.

However, this one is exceptional to certain extent. The whole textbook is designed as a record of the dialogue of the author with his daughter who wants to learn programming. The author endeavors not to explain the Java programming language to the readers, but to teach them real programming.

To teach them how to think and design the program as the experienced programmers do. Entire matter is explained in a very illustrative way which means even a current secondary school student can understand it quite simply. OOP promotes good design practices, code portability, and reuse-but it requires a shift in thinking to be fully understood. Programmers new to OOP should resist the temptation to jump directly into a particular programming language or a modeling language, and instead first take the time to learn what author Matt Weisfeld calls "the object-oriented thought process.

While programming technologies have been changing and evolving over the years, object-oriented concepts remain a constant-no matter what the platform. I would expect that readers with a basic understanding of object-oriented programming and design would find this book useful, before approaching design patterns completely. Design Patterns Explained complements the existing design patterns texts and may perform a very useful role, fitting between introductory texts such as UML Distilled and the more advanced patterns books.

Design Patterns Explained, Second Edition is the field's simplest, clearest, most practical introduction to patterns. Using dozens of updated Java examples, it shows programmers and architects exactly how to use patterns to design, develop, and deliver software far more effectively.

You'll start with a complete overview of the fundamental principles of patterns, and the role of object-oriented analysis and design in contemporary software development. Then, using easy-to-understand sample code, Alan Shalloway and James Trott illuminate dozens of today's most useful patterns: their underlying concepts, advantages, tradeoffs, implementation techniques, and pitfalls to avoid.

Many patterns are accompanied by UML diagrams. Building on their best-selling First Edition, Shalloway and Trott have thoroughly updated this book to reflect new software design trends, patterns, and implementation techniques. Reflecting extensive reader feedback, they have deepened and clarified coverage throughout, and reorganized content for even greater ease of understanding.

It's the ideal "first book" on patterns, and a perfect complement to Gamma's classic Design Patterns. If you're a programmer or architect who wants the clearest possible understanding of design patterns—or if you've struggled to make them work for you—read this book.

Even for people not working with the Unified Process, the book is still of great use. It describes how the process of OO analysis and design fits into the software development lifecycle as defined by the Unified Process UP. UML 2 and the Unified Process contains a wealth of practical, powerful, and useful techniques that you can apply immediately.

As you progress through the text, you will learn OO analysis and design techniques, UML syntax and semantics, and the relevant aspects of the UP.

This book provides Chapter roadmaps, detailed diagrams, and margin notes allowing you to focus on your needs Outline summaries for each chapter, making it ideal for revision, and a comprehensive index that can be used as a reference New to this edition: Completely revised and updated for UML 2 syntax Easy to understand explanations of the new UML 2 semantics More real-world examples A new section on the Object Constraint Language OCL Introductory material on the OMG's Model Driven Architecture MDA The accompanying website provides A complete example of a simple e-commerce system Open source tools for requirements engineering and use case modeling Industrial-strength UML course materials based on the book.

The Object-Oriented Thought Process, Second Edition will lay the foundation in object-oriented concepts and then explain how various object technologies are used. We will get into the relationships between classes and objects later in this chapter, but for now you can think of a class as a template from which objects are made. When an object is created, we say that the objects are instantiated. Thus, if we create three employees, we are actually creating three totally distinct instances of an Employee class.

Each object contains its own copy of the attributes and methods. For example, consider Figure 1. An employee object called Mary has its own copy of attributes and methods. An Implementation Issue Be aware that there is not necessarily a physical copy of each method for each object. Rather, each object points to the same implementation. From a conceptual level, you can think of objects as being wholly independent and having their own attributes and methods.

In short, a class is a blueprint for an object. When you instantiate an object, you use a class as the basis for how the object is built. In fact, trying to explain classes and objects is really a chicken-and-egg dilemma. It is difficult to describe a class without using the term object and vice versa. For example, a specific individual bike is an object.

However, someone had to have created the blueprints that is, the class to build the bike. An object cannot be instantiated without a class. Thus, many of the concepts in this section are similar to those presented earlier in the chapter, especially when we talk about attributes and methods.

Although this book focuses on the concepts of OO software and not on a specific implementation, it is often helpful to use code examples to explain some concepts, so Java code fragments are used throughout the book to help explain some concepts when appropriate. However, for certain key examples, the code is provided in several languages as downloads. The following sections describe some of the fundamental concepts of classes and how they interact. Creating Objects Classes can be thought of as the templates, or cookie cutters, for objects as seen in Figure 1.

A class is used to create an object. A class can be thought of as a sort of higher-level data type. Remember that each object has its own attributes data and behaviors functions or routines. A class defines the attributes and behaviors that all objects created with this class will possess. Classes are pieces of code. Objects instantiated from classes can be distributed individually or as part of a library. Because objects are created from classes, it follows that classes must define the basic building blocks of objects attributes, behavior, and messages.

In short, you must design a class before you can create an object. Each class must define the attributes that will store the state of each object instantiated from that class. In the Personclass example in the previous section, the Person class defines attributes for name and address. Access Designations When a data type or method is defined as public, other objects can directly access it. When a data type or method is defined as private, only that specific object can access it.

Another access modifier, protected, allows access by related objects, which you'll learn about in Chapter 3. Methods As you learned earlier in the chapter, methods implement the required behavior of a class. Every object instantiated from this class includes methods as defined by the class. Methods may implement behaviors that are called from other objects messages or provide the fundamental, internal behavior of the class.

Internal behaviors are private methods that are not accessible by other objects. This is a common technique in OO systems.

In all cases, access to attributes within an object should be controlled by the object itself—no other object should directly change an attribute of another. Messages Messages are the communication mechanism between objects. Only the public methods, not the private methods, of an object can be invoked by another object. We address the code in detail as we progress through the book. Right from the start, I have used UML class diagrams to assist in the educational process. Although it is beyond the scope of this book to describe UML in any detail, we will use UML class diagrams to illustrate the classes that we build.

In fact, we have already used class diagrams in this chapter. As we saw previously, notice that the attributes and methods are separated the attributes on the top and the methods on the bottom. As we delve more deeply into OO design, these class diagrams will get much more sophisticated and convey much more information on how the different classes interact with each other.

In good OO design at least what is generally accepted as good , an object should reveal only the interfaces that other objects must have to interact with it. Encapsulation is defined by the fact that objects contain both the attributes and behaviors. Data hiding is a major part of encapsulation. For example, an object that calculates the square of a number must provide an interface to obtain the result.

However, the internal attributes and algorithms used to calculate the square need not be made available to the requesting object. Robust classes are designed with encapsulation in mind. In the next sections, we cover the concepts of interface and implementation, which are the basis of encapsulation.

Interfaces We have seen that the interface defines the fundamental means of communication between objects. Each class design specifies the interfaces for the proper instantiation and operation of objects. Any behavior that the object provides must be invoked by a message sent using one of the provided interfaces. The interface should completely describe how users of the class interact with the class. In most OO languages, the methods that are part of the interface are designated as public.

Private Data For data hiding to work properly, all attributes should be declared as private. Thus, attributes are never part of the interface. Only the public methods are part of the class interface. Declaring an attribute as public breaks the concept of data hiding. If a user then wants to obtain the value of an attribute, a method is called to return its value.

In this way, the object that contains the attribute controls access to it. This is of vital importance, especially in security, testing, and maintenance. If you control the access to the attribute, when a problem arises, you do not have to worry about tracking down every piece of code that might have changed the attribute—it can be changed in only one place the setter. Signatures—Interfaces Versus Interfaces Don't confuse the interfaces used to extend classes with the interface of a class.

The user should not see any part of the internal implementation, interacting with an object solely through class interfaces. In the previous example, for instance the Employee class, only the attributes were hidden. In many cases, there will be methods that also should be hidden and thus not part of the interface.

For example, the company that produces the calculator can change the algorithm perhaps because it is more efficient without affecting the result. The toaster requires electricity. To get this electricity, the cord from the toaster must be plugged into the electrical outlet, which is the interface.

All the toaster needs to do to obtain the required electricity is to implement a cord that complies with the electrical outlet specifications; this is the interface between the toaster and the power company actually the power industry. That the actual implementation is a coal-powered electric plant is not the concern of the toaster. In fact, for all the toaster cares, the implementation could be a nuclear power plant or a local power generator. With this model, any appliance can get electricity, as long as it conforms to the interface specification as shown in Figure 1.

Assume that you are writing a class that calculates the squares of integers. You must provide a separate interface and implementation. That is, you must specify a way for the user to invoke and obtain the square value.

You must also provide the implementation that calculates the square; however, the user should not know anything about the specific implementation.

Thus, you can identify the interface by the methods, prefaced with plus signs. The implementation of the square algorithm is in the method calculateSquare, which is private. Also notice that the attribute SquareValue is private because users do not need to know that this attribute exists.

Therefore, we have hidden the part of the implementation: The object reveals only the interfaces the user needs to interact with it, and details that are not pertinent to the use of the object are hidden from other objects.

Here the code uses the Java library method Math. This provides the ability to create new classes by abstracting out common attributes and behaviors from another class. One of the major design issues in OO programming is to factor out commonality of the various classes. For example, suppose you have a Dog class and a Cat class, and each will have an attribute for eye color.

In a procedural model, the code for Dog and Cat would each contain this attribute. In an OO design, the color attribute could be moved up to a class called Mammal—along with any other common attributes and methods. In this case, both Dog and Cat inherit from the Mammal class, as shown in Figure 1. The Dog and Cat classes both inherit from Mammal. Thus, Dog has all the properties of its class definition, as well as the properties inherited from the Mammal class.

Behaviors It is worth noting that behaviors today tend to be described in interfaces and that inheritance of attributes is the most common use of direct inheritance. In this way, the behaviors are abstracted away from their data. Superclasses and Subclasses The superclass, or parent class sometimes called base class , contains all the attributes and behaviors that are common to classes that inherit from it.

For example, in the case of the Mammal class, all mammals have similar attributes, such as eyeColor and hairColor, as well as behaviors, such as generateInternalHeat and growHair.

All mammals have these attributes and behaviors, so it is not necessary to duplicate them down the inheritance tree for each type of mammal. Duplication requires a lot more work, and perhaps more worrisome, it can introduce errors and inconsistencies. The subclass, or child class sometimes called derived class is an extension of the superclass.

Thus, the Dog and Cat classes inherit all those common attributes and behaviors from the Mammal class. The Mammal class is considered the superclass of the Dog and the Catsubclasses, or child classes.

Inheritance provides a rich set of design advantages. By inheriting from the Mammalobject, Cat already has all the attributes and behaviors that make it a true mammal. To make it more specifically a cat type of mammal, the Cat class must include any attributes or behaviors that pertain solely to a cat. Abstraction An inheritance tree can grow quite large. When the Mammal and Cat classes are complete, other mammals, such as dogs or lions, tigers, and bears , can be added quite easily.

The Catclass can also be a superclass to other classes. For example, it might be necessary to abstract the Cat class further, to provide classes for Persian cats, Siamese cats, and so on. The power of inheritance lies in its abstraction and organization techniques. These multiple levels of abstraction are one of the reasons why many developers are wary of using inheritance at all. As we will see often, it is difficult to decide how much abstraction is required. For example, if a penguin is a bird and a hawk is a bird, should they both inherit from a class called Bird—a class that has a fly method?

In most recent OO languages such as Java,. NET, and Swift , a class can have only a single parent class; however, a class can have many child classes. The former case is called single inheritance, and the latter is called multiple inheritance.

Multiple Inheritance Consider a child that inherits from both parents. Which pair of eyes does the child inherit? This is a significant problem when it comes to writing a compiler. Note that the classes GermanShepherd and Poodle both inherit from Dog—each contains only a single method. However, because they inherit from Dog, they also inherit from Mammal. Thus, the GermanShepherd and Poodle classes contain all the attributes and methods included in Dog and Mammal, as well as their own see Figure 1.

This relationship is often referred to as an is-a relationship because a circle is a shape, and a square is a shape. When a subclass inherits from a superclass, it can do anything that the superclass can do. Thus, Circle, Square, and Star are all extensions of Shape. When we design this Shape system, it would be very helpful to standardize how we use the various shapes. Thus, we could decide that if we want to draw a shape, no matter what shape, we will invoke a method called draw.

If we adhere to this decision, whenever we want to draw a shape, only the draw method needs to be called, regardless of what the shape is. This is a common concept in many current software applications, such as drawing and word processing applications.

Although polymorphism is tightly coupled to inheritance, it is often cited separately as one of the most powerful advantages to object-oriented technologies. When a message is sent to an object, the object must have a method defined to respond to that message. In an inheritance hierarchy, all subclasses inherit the interfaces from their superclass. However, because each subclass is a separate entity, each might require a separate response to the same message.

For example, consider the Shape class and the behavior called draw. You must specify a concrete shape. To do this, you provide the actual implementation in Circle. Even though Shape has a draw method, Circle overrides this method and provides its own draw method.

Overriding basically means replacing an implementation of a parent with one from a child. For example, suppose you have an array of three shapes—Circle, Square, and Star. Even though you treat them all as Shape objects, and send a draw message to each Shape object, the end result is different for each because Circle, Square, and Star provide the actual implementations. In short, each class is able to respond differently to the same draw method and draw itself. This is what is meant by polymorphism.

The method getArea includes an identifier called abstract. When a method is defined as abstract, a subclass must provide the implementation for this method; in this case, Shape is requiring subclasses to provide a getArea implementation. The Circle class has a method with the same name, Circle. When a method name is the same as the class and no return type is provided, the method is a special method, called a constructor.

Consider a constructor as the entry point for the class, where the object is built; the constructor is a good place to perform initializations and start-up tasks. The Circle constructor accepts a single parameter, representing the radius, and assigns it to the radius attribute of the Circle class.

The Circle class also provides the implementation for the getArea method, originally defined as abstract in the Shape class. This is because we know that all rectangles and circles inherit from Shape, and all Shape classes have a getArea method. If a subclass inherits an abstract method from a superclass, it must provide a concrete implementation of that method, or else it will be an abstract class itself see Figure 1.

This approach also provides the mechanism to create other, new classes quite easily. A stack is a data structure that is a last-in, first-out system. It is like a coin changer, where you insert coins at the top of the cylinder and, when you need a coin, you take one off the top, which is the last one you inserted. Pushing an item onto the stack means that you are adding an item to the top like inserting another coin into the changer.

Popping an item off the stack means that you are taking the last item off the stack like taking the coin off the top. Now comes the fun part. We can empty the stack, and we do not have to worry about what kind of Shape classes are in it we just know they are shapes : Click here to view code image while! For example, Circle calculates the area for a circle, and Rectangle calculates the area of a rectangle. In effect and here is the key concept , we are sending a message to the Shape classes and experiencing different behavior depending on what subclass of Shape is being used.

This approach is meant to provide standardization for the interface across classes, as well as applications. Consider an office suite application that includes a word processing and a spreadsheet application. This print interface is required for all classes that are part of the office suite. The interesting thing here is that although both the word processor and the spreadsheet invoke the print interface, they do different things: one prints a word processing document and the other a spreadsheet document.

A television set contains a tuner and video display. A computer contains video cards, keyboards, and drives. Although the computer can be considered an object unto itself, the drive is also considered a valid object. In fact, you could open up the computer and remove the drive and hold it in your hand. Both the computer and the drive are considered objects. It is just that the computer contains other objects—such as drives.

In this way, objects are often built, or composed, from other objects: This is composition. Abstraction Just as with inheritance, composition provides a mechanism for building objects. In fact, I would argue that there are only two ways to build classes from other classes: inheritance and composition. As we have seen, inheritance allows one class to inherit from another class.

We can thus abstract out attributes and behaviors for common classes. For example, dogs and cats are both mammals because a dog is-a mammal and a cat is-a mammal. With composition, we can also build classes by embedding classes in other classes. Consider the relationship between a car and an engine. The benefits of separating the engine from the car are evident. By building the engine separately, we can use the engine in various cars—not to mention other advantages.

Rather, we use the term has-a to describe composition relationships. A car has-a n engine. Has-a Relationships While inheritance is considered an is-a relationship, a composition relationship is termed a has- a relationship.

Using the example in the previous section, a television has-a tuner and has- a video display. A television is obviously not a tuner, so there is no inheritance relationship. In the same vein, a computer has-a video card, has-a keyboard, and has-a disk drive. A single object contains both its data and behaviors and can hide what it wants from other objects. For example, you might have a system with many shapes. However, a circle, a square, and a star are each drawn differently.

Using polymorphism, you can send each of these shapes the same message for example, Draw , and each shape is responsible for drawing itself. This chapter covers the fundamental OO concepts, of which by now you should have a good grasp. The rest of the book delves more deeply into these concepts and introduces several others.

Many factors go into a good design, whether it is an OO design or not. The fundamental unit of OO design is the class. The desired end result of OO design is a robust and functional object model—in other words, a complete system.

As with most things in life, there is no single right or wrong way to approach a problem. There are usually many ways to tackle the same problem. What you really need to do is brainstorm and let your thought process go in different directions. Do not try to conform to any standards or conventions when trying to solve a problem because the whole idea is to be creative. The first order of business is to identify and solve business problems.

Work on the conceptual analysis and design first. Think about specific technologies only when they are fundamental to the business problem. However, it is often the case that you will have more than one software solution to consider. Thus, before you start to design a system, or even a class, think the problem through and have some fun! In this chapter we explore the fine art and science of OO thinking.

Any fundamental change in thinking is not trivial. As a case in point, a lot has been mentioned about the move from structured to OO development. As was mentioned earlier, one side effect of this debate is the misconception that structured and object-oriented development are mutually exclusive. This is not the case. As we know from our discussion on wrappers, structured and object-oriented development coexist. In fact, when you write an OO application, you are using structured constructs everywhere.

I have never seen a program, OO or otherwise, that does not use loops, if-statements, and so on. Yet making the switch to OO design does require a different type of investment. NET, Visual Basic. This is where the overused phrase OO paradigm rears its ugly head. When moving to an OO language, you must first go through the investment of learning OO concepts and the corresponding thought process.

Thus, when designing a class, what the user needs to know and, perhaps of more importance, what the user does not need to know are of vital importance. The data hiding mechanism inherent with encapsulation is the means by which nonessential data is hidden from the user. Caution Do not confuse the concept of the interface with terms like graphical user interface GUI. Although a GUI is, as its name implies, an interface, the term interfaces, as used here, is more general in nature and is not restricted to a graphical interface.

Remember the toaster example in Chapter 1? The toaster, or any appliance for that matter, is plugged into the interface, which is the electrical outlet—see Figure 2. All appliances gain access to the required electricity by complying with the correct interface: the electrical outlet. For all the toaster cares, a coal plant or a nuclear plant could produce the electricity— the appliance does not care which, as long as the interface works as specified, correctly and safely.

Figure 2. As another example, consider an automobile. The interface between you and the car includes components such as the steering wheel, gas pedal, brake, and ignition switch.

For most people, aesthetic issues aside, the main concern when driving a car is that the car starts, accelerates, stops, steers, and so on. In fact, most people would not even be able to identify certain components, such as the catalytic converter and gasket. However, any driver would recognize and know how to use the steering wheel because this is a common interface.

By installing a standard steering wheel in the car, manufacturers are assured that the people in their target market will be able to use the system. If, however, a manufacturer decided to install a joystick in place of the steering wheel, most drivers would balk at this, and the automobile might not be a big seller except possibly gamers.

It must be stressed that the interchangeable engines must be identical in every way—as far as the interface goes. Replacing a four-cylinder engine with an eight-cylinder engine would change the rules and likely would not work with other components that interface with the engine, just as changing the current from AC to DC would affect the rules in the power plant example.

The engine is part of the implementation, and the steering wheel is part of the interface. A change in the implementation should have no impact on the driver, whereas a change to the interface might. The driver would notice an aesthetic change to the steering wheel, even if it performs in a similar manner.

It must be stressed that a change to the engine that isnoticeable by the driver breaks this rule. For example, a change that would result in noticeable loss of power is actually impacting the interface. What Users See When we talk about users in this chapter, we primarily mean designers and developers—not necessarily end users.

Thus, when we talk about interfaces in this context, we are talking about class interfaces, not GUIs. Properly constructed classes are designed in two parts—the interface and the implementation.

The Interface The services presented to an end user constitute the interface. In the best case, only the services the end user needs are presented. Of course, which services the user needs might be a matter of opinion. If you put 10 people in a room and ask each of them to do an independent design, you might receive 10 totally different designs—and there is nothing wrong with that. However, as a general rule, the interface to a class should contain only what the user needs to know. In the toaster example, the user needs to know only that the toaster must be plugged into the interface which in this case is the electrical outlet and how to operate the toaster itself.

Identifying the User Perhaps the most important consideration when designing a class is identifying the audience, or users, of the class. The Implementation The implementation details are hidden from the user.

This might seem a bit confusing, but this goal is at the heart of the design issue. Good Interfaces If the interface is designed properly, a change to the implementation should not require a change to the user's code.

Remember that the interface includes the syntax to call a method and return a value. If this interface does not change, the user does not care whether the implementation is changed. We see this all the time when using a cell phone. To make a call, the interface is simple—we either dial a number or select an entry in the contact list.

The interface stays the same regardless of how the implementation changes. However, I can think of one situation when the provider did change the interface—when my area code changed. Fundamental interface changes, like an area code change, do require the users to change behavior. Businesses try to keep these types of changes to a minimum, for some customers will not like the change or perhaps not put up with the hassle.

Recall that in the toaster example, although the interface is always the electric outlet, the implementation could change from a coal power plant to a nuclear power plant without affecting the toaster. One very important caveat should be made here: The coal or nuclear plant must also conform to the interface specification. If the coal plant produces AC power but the nuclear plant produces DC power, a problem exists.

The bottom line is that both the user and the implementation must conform to the interface specification. You should do some analysis of the situation and conduct interviews with end users, and then list the requirements for the project. With these requirements in mind, we can make an initial attempt to design the database reader class by creating possible interfaces for these end users.

In this case, the database reader class is intended for programmers who require use of a database. Thus, the interface is essentially the application-programming interface API that the programmer will use. These methods are, in effect, wrappers that enclose the functionality provided by the database system.

Why would we do this? We explore this question in much greater detail later in the chapter; the short answer is that we might need to customize some database functionality. For example, we might need to process the objects so that we can write them to a relational database. Writing this middleware is not trivial as far as design and coding go, but it is a real-life example of wrapping functionality.

More important, we may want to change the database engine itself without having to change the code. Note that the methods in this class are all public remember that there are plus signs next to the names of methods that are public interfaces. Also note that only the interface is represented; the implementation is not shown. Take a minute to determine whether this class diagram generally satisfies the requirements outlined earlier for the project.

Public Interface Remember, an application programmer can access it, and thus, it is considered part of the class interface. Do not confuse the term interface with the keyword interface used in Java and. NET—which is discussed in later chapters.

For each of the requirements we listed, we need a corresponding method that provides the functionality we want. On all counts the answer is a resounding no! All you care about is that you get the proper return values and that the operations are performed correctly.

In fact, the application programmer will most likely be at least one more abstract level away from the implementation. The application will use your classes to open the database, which in turn will invoke the proper database API. Minimal Interface Although perhaps extreme, one way to determine the minimalist interface is to initially provide the user no public interfaces.

Thus, you add interfaces only when it is requested. Never assume that the user needs something. Creating wrappers might seem like overkill, but there are many advantages to writing them. To illustrate, there are many middleware products on the market today. Consider the problem of mapping objects to a relational database.

OO databases have never caught on; however, theoretically they may be perfect for OO applications. However, one small problem exists: Most companies have years of data in legacy relational database systems. How can a company embrace OO technologies and stay on the cutting edge while retaining its data in a relational database? First, you can convert all your legacy, relational data to a brand-new OO database. However, anyone who has suffered the acute and chronic pain of any data conversion knows that this is to be avoided at all costs.

Although these conversions can take large amounts of time and effort, all too often they never work properly. Second, you can use a middleware product to seamlessly map the objects in your application code to a relational model.

This is a much better solution since relational databases are so prevalent. Some might argue that OO databases are much more efficient for object persistence than relational databases. In fact, many development systems seamlessly provide this service. Object Persistence Object persistence refers to the concept of saving the state of an object so that it can be restored and used at a later time. An object that does not persist basically dies when it goes out of scope. For example, the state of an object can be saved in a database.

However, in the current business environment, relational-to-object mapping is a great solution. Many companies have integrated these technologies.

It is common for a company to have a website front-end interface with data on a mainframe. If you create a totally OO system, an OO database might be a viable and better performing option; however, OO databases have not experienced anywhere near the growth that OO languages have. Standalone Application Even when creating a new OO application from scratch, it might not be easy to avoid legacy data.

Even a newly created OO application is most likely not a standalone application and might need to exchange information stored in relational databases or any other data storage device, for that matter. When this class is complete, it will probably contain more methods, and it will certainly contain attributes. However, as a programmer using this class, you do not need to know anything about these private methods and attributes.

You simply need to know how to interact with the interfaces. What would the code for this public interface look like assume that we start with a Oracle database example? Now comes the fun stuff—what really makes interfaces so great! Last night we translated all the data from an Oracle database to an SQLAnywhere database we endured the acute and chronic pain. It took us hours—but we did it.

This is because even though the implementation changed, the interface did not! As far as the user is concerned, the calls are still the same. The code change for the implementation might have required quite a bit of work and the module with the one-line code change would have to be rebuilt , but not one line of application code that uses this DataBaseReader class needed to change.

Code Recompilation Dynamically loaded classes are loaded at runtime—not statically linked into an executable file. When using dynamically loaded classes, like Java and. NET do, no user classes would have to be recompiled. By separating the user interface from the implementation, we can save a lot of headaches down the road. In Figure 2. In general, reusable classes tend to have interfaces that are more abstract than concrete.

Concrete interfaces tend to be very specific, whereas abstract interfaces are more general. However, simply stating that a highly abstract interface is more useful than a highly concrete interface, although often true, is not always the case. It is possible to write a very useful, concrete class that is not at all reusable. This happens all the time, and nothing is wrong with it in some situations.



0コメント

  • 1000 / 1000