Head first oracle pdf free download






















We can leave them all false because the receiving side ignores them when it gets the POKE command. No software can go out without testing so First compile and start up the MusicServer. MusicServer We use different names here so we know which is which.

Your changes work as advertised. Time to call it a night. Bob finished up the tasks related to his story and ran a quick test on his end. His task is working, so he copies his code up to the server. In order to do the final build he merges his code in with ours, gets everything to compile, and retests sending a picture.

Everything looks good. Once the tasks are finished mov the stories over e to Completed. After the build is y for tomorrow. Q: A: Q: A: On the receiving side we pull off the secret sequence and the array of checkboxes. All of the serialization and deserialization is handled by Java. On the sending side we represent the sequence settings as an array of checkboxes. We still need to send something, though, so the existing code works.

It makes it a lot simpler to clean up and rebuild when you make changes. Wait, did Bob just merge code on the demo server? Your code is written, tested, and copied up to the demo server. Bob did the final build, so we call the customer and prepare to amaze the crowds. Unhappy cust Not good. Our code worked just a few pages ago. So what went wrong? More importantly, what would you do differently in the future to make sure nothing like this ever happens again?

Below is some code we compiled on our machine and the same section of code from the demo machine. See if you can figure out what happened. How did this happen? What would you do? Bob really blew it with that demo. Bob: What are you talking about? My code worked! Laura: But you broke the other story we were trying to demo! It worked fine before you got to it. Bob: Wait a minute—why am I getting blamed for this? You asked me to copy my code up to the demo server so we could build it.

When I did that, I saw you guys had changed a lot of the same stuff. It was a mess. Mark: So you just overwrote it?? Bob: No way—I spent a bunch of time comparing the files trying to figure out what you had changed and what I had changed. To make things worse, you guys had some variables renamed in your code so I had to sort that out, too. I got the button stuff right, but I guess I missed something in the receiver code.

Laura: So do we still have the working Poke code on there? Bob: I doubt it. I copied my stuff up with a new name and merged them into the files you had up there.

Mark: Not good. Laura, do you have it? We really need to find a better way to handle this stuff. You have lots of people working on files—sometimes the same ones, sometimes different. Any serious software project needs version control, which is also often called configuration management, or CM for short. Version control is a tool usually a piece of software that will keep track of changes to your files and help you coordinate different developers working on different parts of your system at the same time.

I need the BeatBox. The version cont l server looks up firo le s returns the latest and to the developers. All done! Checking the code ba ck in means your changes are sent server so others can to the get them. I need the latest BeatBox.

A: There are lots of choices out there for version control tools, both commercial and open source. Version control tools all do pretty much the same thing, but some offer different ways to do it. For example, some commercial systems have strict access control on where you can commit code so that your organization can control what goes into what build. Other tools show you the different versions of files as virtual directories.

A: You bet. In fact, a good version control tool is really the only way you can scale a team. If not, you can download it from the Subversion web site.

This tells Subversion to create a new repository Just go to the directory above your code and tell your version control server to import it. After that runs , we have our reposito ry. Subversion adds each file it finds into your repository for the BeatBox project. Now that your code is in the repository, you can check it out, make your changes, and check your updated code back in.

A version control system will keep track of your original code, all of the changes you make, and also handle sharing your changes with the rest of your team. You just work directly on the files you checked out from your version control system, compile, and save. This is a normal. This is a log message, indicating what you did. Committed revision 2. Bob tries to check in his code You and Bob both made changes to the same file yours into the re;poyou just got sitory first.

Nonconflicting code and methods are easy In BeatBox. Nothing here Bob has no code for playPoke at all. So no problems yet But conflicting code IS a problem But what if you have code in the same method that is different?

When this happens, most systems just punt. Y the update comct ou can use m an into your code, an d to pull the changes lines where there d Subversion will mark the after you sort ou are conflicts in your files What should the final code look like that Bob commits back in?

What should the final section look like that Bob commits back in? Now, commit the file to your server, adding a comment indicating what you did. Committed revision 3. You guys really got your stuff together. Send Poke and Se Picture work. That tells the version control server to give you the latest versions of all files in the project. Q: A: This whole conflict thing seems pretty hairy. Some can. Certain version control tools work in a file locking mode, which means when you check out files, the system locks those files so no one else can check them out.

Once you make your changes and check the files back in, the system unlocks the files. This prevents conflicts, since only one person can edit a file at a time. But, it also means you might not be able to make changes to a file when you want to; you might need to wait for someone else to finish up first.

Good design, good division of labor, frequent commits, and good communication help reduce the number of manual merges you actually have to do. Q: A: What is all this trunk business you keep saying to ignore? The Subversion authors recommend putting your code into a directory called trunk. Then, other versions would go into a directory called branches. Q: A: Where are all of my messages going when I do a commit?

Subversion keeps track of each time you commit changes into the repository and associates your message with those changes. This lets you look at why people made a certain change—for instance, if you need to go back and figure out why something was done. Q: A: Do I have to commit all of my changes at the same time? Just put the path to the filename on the commit command like you did for the resolved command.

Subversion will commit just the file s you specify. Things are going well. The customer was happy with our Poke and Picture support, and after one more iteration, felt we had enough for Version 1. Just a few more stories to implement Title: Save a lo g fi le of ch at s Description: Th e use r sh ou ld be able to save a his to ry of th eir ch at me ss ages to a file.

Su int 8 bje ct Obje ct. The Subject has another 14 14 new int. All the observers get another notification, except for the ct Duck Obje Cat Object Mouse who is no longer included. Uh, yeah, you and This is Lori. After all, they are observers. This guy is a real jerk.

Who needs him. Jump on it! Bwahaha, money in the bank, baby! Jill lands her own job! You can take me off your call list. I found my own job! But what has become of our dear Lori?

The Observer Pattern defined A newspaper subscription, with its publisher and subscribers, is a good way to visualize the pattern. The Observer Pattern defines a one-to-many dependency between objects so that when one object changes state, all of its dependents are notified and updated automatically.

The subject and observers define the one-to-many relationship. We have one subject, who notifies many observers when something in the subject changes. This interface has ts e rfa ce. Objec Each subject just one method, update , int s can have many that is called when the t h e S ubject to register ahemselves observers. In addi move getState.

Each observer whenever state ch getting its stat registers with a concrete subject this later. Subscribe Pattern. Is that just another name for the Observer Pattern? A: With the Observer Pattern, the Subject is the object that contains the state and A: Because the subject is the sole owner of that data, the observers are dependent on A: No, although they are related.

The controls it. So, there is ONE subject with the subject to update them when the data Publish-Subscribe pattern is a more complex state. The observers, on the other hand, use changes. There than allowing many objects to control the interest in different types of messages are many observers, and they rely on the same data. It is often used in middleware So there is a relationship between the ONE systems.

Guru and Student Guru: Have we talked about loose coupling? Student: Guru, I do not recall such a discussion. Guru: Is a tightly woven basket stiff or flexible? Student: Stiff, Guru. Guru: And do stiff or flexible baskets tear or break less easily?

Student: A flexible basket tends to break less easily. Guru: And in our software, might our designs break less easily if our objects are less tightly bound together? Student: Guru, I see the truth of it. But what does it mean for objects to be less tightly bound? Guru: We like to call it, loosely coupled. Student: Ah! Guru: We say a object is tightly coupled to another object when it is too dependent on that object.

Guru: Think of nature; all living things depend on each other. Likewise, all objects depend on other objects. Surely not knowing is worse than knowing. Guru: You are doing well in your studies, but you have much to learn.

By not knowing too much about other objects, we can create designs that can handle change better. Designs that have more flexibility, like the less tightly woven basket.

Student: Of course, I am sure you are right. Could you give me an example? Guru: That is enough for today. The Power of Loose Coupling When two objects are loosely coupled, they can interact, but they typically have very little knowledge of each other. And, as it turns out, the Observer Pattern is a great example of loose coupling. First, the only thing the subject knows about an observer is that it implements a certain interface the Observer interface.

We can add new observers at any time. Because the only thing the subject depends on is a list of objects that implement the Observer interface, we can add new observers whenever we want. In fact, we can replace any observer at runtime with another observer and the subject will keep purring along. Likewise, we can remove observers at any time. We never need to modify the subject to add new types of observers.

Observer interface. We can reuse subjects or observers independently of each other. Changes to either the subject or an observer will not affect the other.

Because the two are loosely coupled, we are free to make changes to either, as long as the objects still meet their obligations to implement the Subject or Observer interfaces. Design Principle Look! We have a new Design Principle! Strive for loosely coupled designs between objects that interact. Loosely coupled designs allow us to build flexible OO systems that can handle change because they minimize the interdependency between objects.

Make sure your diagram shows how all the pieces fit together and also how another developer might implement her own display element. If you need a little help, read the next page; your teammates are already talking about how to design the Weather Station.

Cubicle conversation Back to the Weather Station project. Your teammates have already begun thinking through the problem Sue: Right Mary: Hmm. Mary: That actually makes some sense when you think about it. The WeatherData class certainly has state Mary: Yup, and when those measurements change, we have to notify all the display elements so they can do whatever it is they are going to do with the measurements.

Sue Sue: Like what? Mary: For one thing, how do we get the weather measurements to the display elements? Sue: Well, looking back at the picture of the Observer Pattern, if we make the WeatherData object the subject, and the display elements the observers, then the displays will register themselves with the WeatherData object in order to get the information they want, right?

Mary: Yes Sue: We gotta remember that every display element can be different Even though every component has a different type, they should all implement the same interface so that the WeatherData object will know how to send them the measurements. Mary: I see what you mean. So every display will have, say, an update method that WeatherData will call.

Sue: And update is defined in a common interface that all the elements implement…. This gives the ace to to implement. The display t interface. This should look update the observers. This display shows the weat her ba rom eter. Mary and Sue thought that passing the measurements directly to the observers was the most straightforward method of updating state. Do you think this is wise? Hint: is this an area of the application that might change in the future?

If it did change, would the change be well encapsulated, or would it require changes in many parts of the code? Can you think of other ways to approach the problem of passing the updated state to the observers? You might want to refresh your memory. Here we implement the Subject interface. Weather-O-Rama ordered three: the current conditions display, the statistics display, and the forecast display.

It also implements DisplayElement, This display implements the Observer because our API is going to interface so it can get changes from require all display elements to the WeatherData object. Q: Is update the best place to call display? Q: Why did you store a reference to the WeatherData Subject? A: True, but in the future we may want to un-register ourselves as an observer and it would be handy to already have a reference to the subject.

The Weather Station is ready to go. All we need is some code to glue everything together. Current conditions: Here are the details. The heat index is an index that combines temperature and humidity to determine the apparent temperature how hot it actually feels.

To compute the heat index, you take the temperature, T, and the relative humidity, RH, and use this formula:. So get typing! Just kidding. You can get heatindex. How does it work? When you finish, your output should look like this:. And besides, I do know the most important thing about you—you implement the Observer interface.

Anyway, I know a lot more about you Which gets a little annoying at times Well, excuuuse me. I have to send my state with my notifications so all you lazy Observers will know what happened! I guess that might work. That might be kind of dangerous. Yes, I could let you pull my state. If you have to come to me every time you want something, you might have to make multiple method calls to get all the state you want. Just let us come to you to get the state we need.

It also makes things easier to modify later. Say, for example, you expand yourself and add some more state. You never know, hell could freeze over. I see, always the wise guy One of the advantages of knowing design patterns is recognizing and quickly understanding the design motivation in your favorite libraries. If you look up interface. These methods allow you to add and remove observers—or, as they are called in Swing, listeners—to listen for various types of events that occur on the Swing component.

A little life-changing application Okay, our application is pretty simple. Coding the life-changing application This life-changing application requires very little code. All we need to do is create a JButton object, add it to a JFrame, and set up our listeners. If you ut it; you can continrs. By using How about taking your use of the Obs isten er object. With a lambda the step of creating an ActionL rather than an inner class, you can skip obser ver.

And, when you pass ct instead, and the function object is the expression, we create a function obje ches actionPerformed , the er , Java ensures its signature mat that function object to addActionListen rface.

Using lambda expressions makes this code a lot more concise. For more on lambda expressions, check out the Java docs. Q: I thought Java had Observer and Observable classes? Java used to provide an Observable class the Subject and an Observer interface, which you could use to help changes a particular kind of property, and sends notifications to PropertyChangeListeners.

The Observable class subscriber components in the Flow API for handling asynchronous provided methods to add, delete, and notify observers, so that you streams.

And the Observer interface provided an interface just like ours, with one update method. Folks find it easier to support the basic Observers to arrive in a specific order? Q: Does Java offer other built-in support for Observer to specifically advise you to not depend on any specific notification order. Would it generalize the code a bit more if we allowed the displays to pull their data from the WeatherData object as needed?

That might make it easier to add new displays in the future. Now, whether we pull or push the data to the Observer is an implementation detail, but in a lot of cases it makes sense to let Observers retrieve the data they need rather than passing more and more data to them through the update method.

After all, over time, this is an area that may change and grow unwieldy. Updating the Weather Station code to allow Observers to pull the data they need is a pretty straightforward exercise. To switch to using pull, we need to make a few small changes to our existing code. For the Subject to send notifications Code Magnets The ForecastDisplay class is all scrambled up on the fridge.

Can you reconstruct the code snippets to make it work? Some of the curly braces fell on the floor and they were too small to pick up, so feel free to add as many of those as you need! Go ahead and do that now! This just arrived! Weather-O -Rama, Inc. Your design is fantastic.

Until our next engagement,. Tools for your Design Toolbox Welcome to the end of Chapter 2. OO Basioincs using a common interface. OO Principles Observer interface. Inherite in that the Subject knows Enca nothing about them, other sition over Favor compo than that they implement the. Remd designs are the Subject when using the implementat loosely couplelexible and pattern pull is considered more iv e fo r lo o sely coupled hat much more f ange.

Observer Pattern, as do many GUI frameworks. OO Pat- tdeefinrens asfamily of aalgeoomrniteh-mts,o-many many other places, including RxJava, JavaBeans, and RMI, as well as in other language Strategy bseacrhvoenre, -anddemfainkeess th ctosritsohmthat frameworks, like Cocoa, Swift, encapsulaO tes e e n d e n c t y r atbeegtywleeetns tohbejealg atite.

Design Principle Challenge For each design principle, describe how the Observer Pattern makes use of the principle. Design Principle Identify the aspects of your application that vary and separate them from what stays the same.

This is a hard one. Hint: think about how observers and subjects work together. Design Principle Favor composition over inheritance. He didn't want any more ints so he removed 3.

Observers himself Observer. CurrentConditionsDisplay 6. CEO almost forgot implements this interface. He kind ofsays stormyou should go for it. CurrentConditionsDisplay implements this 7. The Subject 5.

The Subject doesn't have to know much 9. Javalike Observers this for notification on the Subject. How Observers Implement this of Jill got one method to get notified her own. We have no way to add display WeatherData class. With this pattern, you can Identify the aspects of your application that vary and separate them from what stays the vary the objects that are dependent on the state same.

Both the Subject and Observers use interfaces. The Subject keeps track of objects implementing Design Principle the Observer interface, while the Observers Program to an interface, not an implementation.

The Observer Pattern uses composition to compose any number of Observers with their Subject. No, they are set up at runtime by composition! CurrentConditionsDisplay implements this 5. Java framework with lots of Observers 7. I used to think real men subclassed everything. That was until I learned the power of extension at runtime, rather than at compile time.

Welcome to Starbuzz Coffee Starbuzz Coffee has made a name for itself as the fastest- growing coffee shop around. When they first went into business they designed their classes like this Each subclass implements cost to return the cost of the beverage.

In addition to your coffee, you can also ask for several condiments like steamed milk, soy, and mocha otherwise known as chocolate , and have it all topped off with whipped milk. Starbuzz charges a bit for each condiment, so they really need to get them built into their order system.

What happens when the price of milk goes up? What do they do when they add a new caramel topping? This is stupid; why do we need all these classes? Subclasses will still override hasMilk cost , but they will also invoke the super version so setMilk hasSoy that they can calculate the total cost of the basic setSoy beverage plus the costs of the added condiments.

Write the cost methods for the following classes pseudo-Java is okay :. See, five classes total. This is definitely the way to go.

New condiments will force us to add new methods and alter the cost method in the superclass. As we saw1,inthis is We may have new beverages. For some of these beverages iced tea? Guru: It has been some time since our last meeting. Have you been deep in meditation on inheritance?

Student: Yes, Guru. Guru: Ah yes, you have made some progress. So, tell me, my student, how then will you achieve reuse if not through inheritance? Guru: Please, go on Student: When I inherit behavior by subclassing, that behavior is set statically at compile time. In addition, all subclasses must inherit the same behavior.

Guru: Very good; you are beginning to see the power of composition. Student: Yes, it is possible for me to add multiple new responsibilities to objects through this technique, including responsibilities that were not even thought of by the designer of the superclass. Guru: What have you learned about the effect of composition on maintaining your code? Student: Well, that is what I was getting at. By dynamically composing objects, I can add new functionality by writing new code rather than altering existing code.

Guru: Very good. Enough for today. I would like for you to go and meditate further on this topic Remember, code should be closed to change like the lotus flower in the evening, yet open to extension like the lotus flower in the morning. The Open-Closed Principle We're on to one of the most important design principles:. Design Principle Classes should be open for extension, but closed for modification. Feel free to extend our classes with any new behavior you like.

If your needs or requirements change and we know they will , just go ahead and make your own extensions. It must remain closed to modification. Our goal is to allow classes to be easily extended to incorporate new behavior without modifying existing code. What do we get if we accomplish this? Designs that are resilient to change and flexible enough to take on new functionality to meet changing requirements.

Making OO design for modification? That sounds very flexible and open to extension without contradictory. How can a design be both? It certainly sounds contradictory at first. After all, the less every part of our designs and it would probably be wasteful. Following the Open-Closed modifiable something is, the harder it is to Principle usually introduces new levels of extend, right?

You want to concentrate on those areas that are As it turns out, though, there are some most likely to change in your designs and apply clever OO techniques for allowing systems the principles there. Observers, we can extend the Subject at any time, without adding code to the Subject. Q: Okay, I understand Observer, but Looking at other examples will help you learn to identify areas of change in your own designs. A: Many of the patterns give us time-tested designs that protect your code from being While it may seem like a contradiction, modified by supplying a means of extension.

Remember us? Starbuzz Coffee? Do you think you could use some of those design principles to actually Meet the Decorator Pattern help us? Itats ing— The M object it is decor type mirrors thBeeverage. Whip So, a DarkRoast wrapped in Mocha and Whip is still a Beverage and we can do anything with it we can do with a DarkRoast, including call its cost method. We do this by calling cost on the outermost decorator, Whip, and Whip is going to delegate computing the cost to the objects it decorates.

And so on. Mocha 4 DarkRoast returns Whip its cost, 99 cents. The Decorator Pattern attaches additional responsibilities to an object dynamically. Decorators provide a flexible alternative to subclassing for extending functionality.

Each component can be used on its own or wrapped by a decorator. Component component methodA methodB. It extends instance variable that holds a Component. ConcreteComponent Decorator reference to a component. The ConcreteDecorator e Decorators can extendt. Decorators can add new methods; however, new behavior is typically added by doing computation before or after an existing method in the component. The four concrete Milk Mocha Soy Whip components, one per cost cost cost cost.

And here are our condiment decorators; notice they need to implement not only cost but also getDescription. I thought we were going to rely on composition instead?

Sue: What do you mean? Mary: Look at the class diagram. The CondimentDecorator is extending the Beverage class. Sue: True. But where does the behavior come in? Sue: When we compose a decorator with a component, we are adding new behavior. We are acquiring new behavior not by inheriting it from a superclass, but by composing objects together.

The behavior comes in through the composition of decorators with the base components as well as other decorators. Mary: Oh, I get it! And because we are using object composition, we get a whole lot more flexibility about how to mix and match condiments and beverages. Very slick. Sue: Yes, if we rely on inheritance, then our behavior can only be determined statically at compile time.

In other words, we get only whatever behavior the superclass gives us or that we override. With composition, we can mix and match decorators any way we like Mary: I get it—we can implement new decorators at any time to add new behavior. Sue: Exactly. Sue: Well, remember, when we got this code, Starbuzz already had an abstract Beverage class. Traditionally the Decorator Pattern does specify an abstract component, but in Java, obviously, we could use an interface.

New barista training Okay, I need for you to make me a double mocha Make a picture for what happens when the order is for a soy latte with whip. Use the menu to get the correct prices, and draw your picture using the same format we used earlier from a few pages back : 2 Whip calls cost on Mocha. Mocha calls cost on was for 3 First, we call cost on the This picturoeast mocha 1 DarkRoast.

Starbuzz Coffee Coffees Draw your picture here. House Blend. Beverage is simple enough. Remember, we need to set a description for the specific beverage and also implement the cost method. First we exten d the Beverage. Now delegate the call tompute the with Mocha. Write and compile the code for the other Soy and Whip condiments. Serving some coffees Congratulations.

Please note that the Builder Pattern is covered in the Appendix. Like wanted my getDescription method to something, like issue a discount. A: That is exactly right. When you need to type, decorators will break that code. As have to manage more objects with the peek at multiple layers into the decorator long as you only write code against the Decorator Pattern and so there is an chain, you are starting to push the decorator abstract component type, the use of increased chance that coding errors will beyond its true intent.

Nevertheless, decorators will remain transparent to your introduce the kinds of problems you suggest. Imagine a code. Our friends at Starbuzz have introduced sizes to their menu. You can now order a coffee in tall, grande, and venti sizes translation: small, medium, and large. The updated Beverage class is shown below. How would you alter the decorator classes to handle this change in requirements?

A text file for reading. ByteArrall of these give us a d bytes. A from which to rea data from a zip file. Decorating the java. InputStream acts as the abstract decorator class:. There are a few more we ors. You should now be in a good position to look over the java. You should be ready to write your own input decorator. No problem. I just have to extend the How about this: write a decorator that converts all uppercase FilterInputStream class and characters to lowercase in the input stream.

In other words, if override the read methods. Head First: Welcome, Decorator Pattern. HeadFirst: Can you perhaps share some of your troubles with us? Decorator: Sure. HeadFirst: Can you give us an example? These are notoriously difficult for people to understand at first. But if they just saw the classes as a set of wrappers around an InputStream, life would be much easier.

But like I said, some code is dependent on specific types and when you start introducing decorators, boom! Bad things happen. HeadFirst: Well, I think everyone understands that you have to be careful when inserting decorators.

Decorator: I know, I try not to be. I also have the problem that introducing decorators can increase the complexity of the code needed to instantiate the component. OO Basics behavior to be extended without the need to modify existing code.

C t closed fo In fact, they are the same type us. Is there another pa t t er overuse can be complex. Write the cost methods for the following classes pseudo-Java is okay. Whip calls cost on Mocha. Soy calls cost on HouseBlend. Get ready to bake some loosely coupled OO designs.

In addition, the book consists of head first servlets and jsp 3rd edition mock exam created in the same pattern as the certification examination. There was no cushion provided to book, it was just a standard plastic bag with Flipkart Bag which resulted in front cover crushed a little.

Hi, Head first servlets and jsp 3rd edition Time though the book was delivered the next day, The packing was not according to Flipkarts standard. Head First Java 2 Edition 4. This book may not be enough for the Oracle Exam.

Educational and Professional Books. Safe and Secure Payments. Spot false dilemmas now, ask me how!



0コメント

  • 1000 / 1000