• Tidak ada hasil yang ditemukan

A Book Review"

N/A
N/A
Ahmed Tarek

Academic year: 2024

Membagikan " A Book Review""

Copied!
681
0
0

Teks penuh

Head First Design Patterns' manages to mix fun, belly laughs, insight, technical depth and great practical advice into one entertaining and thought-provoking read. Richard Helm, co-author of "Design Patterns" with the rest of the Gang of Four - Erich Gamma, Ralph Johnson and John Vlissides.

Don’t miss out when something interesting happens!

Just call this chapter "Design for the Legacy Man." We'll re-examine the typical overuse of inheritance and you'll learn how.

Just call this chapter “Design Eye for the Inheritance Guy.” We’ll re-examine the typical overuse of inheritance and you’ll learn how

Get ready to cook some loosely coupled OO designs

The Singleton pattern: your ticket to creating unique objects of which there is only one instance. Observer - defines a one-to-many dependency between objects so that when one object changes state, all its.

In this chapter we take encapsulation to a whole new level: we’re going to encapsulate method invocation

In this chapter, we take encapsulation to a whole new level: we'll encapsulate a method call. In this chapter, we will tackle such impossible feats as fitting a square peg into a round hole.

There are lots of ways to stuff objects into a collection

You are the good cop and you offer all your services in a nice and friendly way, but you don't want everyone to ask for your services, so you have the bad police control access to you. As you will see, there are many ways in which proxies stand for the objects they represent.

Who would have ever guessed that Patterns could work together? You’ve already witnessed the acrimonious Fireside Chats (and be

You don't need to be advanced, and even if you don't know Java, but you do know C#, you'll probably understand at least 80% of the code examples. Suppose you are out for a day walk and a tiger jumps in front of you, what happens inside your head and body.

We think of a “Head First” reader as a learner

The trick is to get your brain to see the new material you're learning as Really Important. We've included over 40 activities because your brain is wired to learn and remember more when you do things than when you read about things.

Puzzles

But we did our best to make sure that when you're working hard, it's on the right things. You'll find a brief look at some of the other models (the ones you're much less likely to use) in the appendix.

CHANGE

We know that fly() and quack() are the parts of the Duck class that differ between ducks. And the same is true for the duck's flying behavior - the MallardDuck constructor initializes the flyBehavior instance variable with an instance of type FlyWithWings (a concrete class implementing FlyBehavior).

Congratulations on your first pattern!

You will find classes for the game characters along with classes for the weapon behaviors that the characters can use in the game.

Design Puzzle

Once you have the vocabulary, you can more easily communicate with other developers and inspire those who don't know patterns to start learning them. A team well-versed in design patterns can move faster with less room for misunderstandings. Think about starting a pattern study group in your organization, maybe you can get paid while you're learning.

Design patterns tell us how to structure classes and objects to solve certain problems, and it's our job to adapt those designs to fit our particular application. Developer: Okay, hmm, but isn't that just good object-oriented design; I mean, as long as I follow encapsulation and know about abstraction, inheritance and polymorphism, do I really need to think about Design Patterns. But if you follow well-thought-out, time-tested design patterns, you'll be way ahead.

Design Puzzle Solution

BowAndArrow behavior useWeapon() { // implements shooting an arrow with a bow } useWeapon() { // implements shooting an arrow with a bow } using a bow.

Solutions

The three players in the system are the weather station (the physical device that retrieves the actual weather data), the WeatherData object (that tracks the data coming from the weather station and updates the displays), and the display that shows users the current weather conditions. The WeatherData object knows how to talk to the physical weather station to get updated data. The WeatherData object then updates its views for the three different view elements: Current Conditions (showing temperature, humidity, and pressure), Weather Statistics, and a simple forecast.

We don't care HOW these variables are set; the WeatherData object knows how to update itself. R The measurementsChanged() method is called whenever new weather measurement data is available. We don't know and don't care what this method is called; we just know it is.). R We need to implement three display elements that use weather data: the current conditions display, the statistics display, and the forecast display.

The observers have subscribed to (registered with) the Subject to receive updates when the Subject's data changes. Cat Object If you understand newspaper subscriptions, you pretty much understand the Observer pattern, except we call the publisher the SUBJECT and the subscribers the OBSERVERS. A Duck object comes along and tells the subject that it wants to become an observer.

Now Duck and all the other observers get a notification that the topic has changed. The observers are dependent on the subject, so that when the state of the subject changes, the observers are notified. As you'll discover, there are a few different ways to implement the Observer pattern, but most revolve around a class design that includes Subject and Observer interfaces.

ONE TO MANY RELATIONSHIP

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. We don't need to make any changes to the subject to accommodate the new class type, all we need to do is implement the Observer interface in the new class and register as an observer. The subject does not care; it will send notifications to any object that implements the Observer interface.

The Observer pattern defines a one-to-many dependency between objects, so that when an object changes state, all its dependents are automatically notified and updated. The Observer interface is implemented by all observers, so they must all implement the update() method. Now it's time to go back and do things with the observer model in mind.

Subject Observer

The most common is the Observer interface and the Observable class in the java.util package. Implement the Observer interface as usual (this time the java.util.Observer interface) and call addObserver() on each Observable object. First of all, you need to be Observable by extending the java.util.Observable superclass.

java.util.Observable has implemented its notifyObservers() method so that the observers are notified in a different order than our own implementation. You also don't have the option to replace the java.util implementation with a different one (eg a new multi-threaded implementation). The java.util implementation of Observer/Observable is not the only place you can find the Observer pattern in the JDK; both JavaBeans and Swing also provide their own implementations of the pattern.

Exercise solutions

Just call this chapter “Design Eye for the Inheritance Guy.”

Each costing method calculates the cost of the coffee along with the other spices in the order. Thinking beyond the maintenance problem, which of the design principles we've covered so far are they violating. Before you go any further, think about how you will implement the cost() method of coffees and spices.

Now that we have our base classes out of the way, let's implement some drinks. If you look back at the class diagram of the decorator model, you'll see that we've now written our abstract component (Beverage), we have our concrete ingredients (HouseBlend), and we have our abstract decorator (CondimentDecorator). Once we've covered these patterns, you'll see that creating the concrete component with its decorator is.

HUFLVHVROXWLRQV

CT 5V DW \\%QHHG

You can now order a coffee in tall, grande and venti sizes (for us regulars: small, medium and large). They also want the spices to be charged by size, so for example soy costs 10¢, 15¢ and 20¢ respectively for long, grande and venti coffees.

Exercise solutions

So now we know that we can better get rid of object creation from the orderPizza() method. We still need to fill in a few details here; For example, what does the orderPizza() method replace its creation code with? What we're going to do is define a class that covers the object creation for all pizzas.

Notice that we replaced the new operator with a create method on the factory object. Once we've built our PizzaStore subclasses, it's time to see if we can order a pizza or two. We've taken out the NYPizzaStore, only two more to go and we're ready to franchise.

The Factory Method pattern encapsulates object creation by letting subclasses decide which objects to create. The Factory method pattern defines an interface for creating an object, but lets subclasses decide which class to instantiate. As in the official definition, you'll often hear developers say that the Factory method lets subclasses decide which class to instantiate.

Now we're going to build a factory to make our ingredients; the factory will be responsible for creating each ingredient in the ingredient family. The Pizza Code uses the factory that put it together to produce the ingredients used in the pizza. Compare this version of the createPizza() method with the one in the Factory Method implementation earlier in this chapter.

Behind the Scenes

The abstract factory pattern provides an interface for creating families of related or dependent objects without specifying their concrete classes. The client is written against an abstract factory and then assembled with the actual factory at runtime. Abstract Factory clients are concrete instances of the Pizza abstract class.

The job of an abstract factory is to define an interface for creating a set of products. Abstract Factory: Yes, but I need a large interface because I'm used to creating entire families of products. Abstract Factory: Yes, I admit, my concrete factories often implement the factory method to create their products.

Referensi

Dokumen terkait