Chaper 1 Questions

advertisement
Chaper 4 Notes – Abstract Factory Pattern
Text Discussion
1.
How is the Dependency Inversion Principle different than the design principle that says, “Program to
an interface, not an implementation?”
2. How does the factory method adhere to the Dependency Inversion Principle?
3. What does “inversion” mean in the Dependency Inversion Principle?
4. How do you decide when you should apply the Dependency Inversion Principle?
5. Consider the discussion on pages 146-147. Explain why this is or isn’t the factory method?
6. Consider the discussion on page 150. How is the number of pizza classes reduced?
7. How is the new pizza class (page 150) decoupled from the differences in regional ingredients?
8. Based on the specific example given in the text (pages 150-151), how could you avoid the duplication
of code shown? Would this always be possible?
9. In the abstract factory, the client writes code that uses the abstract factory. How is this different from
the factory method? and/or Discuss in detail the differences between the factory method and abstract
factory.
10. Consider the class diagram on page 157. It is not quite correct. It doesn’t show the PizzaStore class as
a superclass of the NYPizzaStore class. Explain how the createPizza() method is an implementation of
the factory method. In other words, explain how this example actually uses both factory method and
abstract factory. Note: I’m NOT talking about the comment on the top of page 158.
General UML
How does this differ from Factory Method (below)?
1

The Abstract Factory Pattern provides an interface for creating families of related or dependent objects
without specifying their concrete classes.

It helps control the objects that an application creates.

It enforces the dependencies between the concrete product classes.

It is a layer of abstraction higher than Factory Method.

Supporting new kinds of products is difficult. It involves modifying the Abstract Factory and all its
subclasses.

The factory method is a single method that is overridden (implemented) in a subclass. With the factory
method, usually an object (Creator) is calling it's own factory method. The difference is that the
intended purpose of the class containing a factory method is not to create objects, that is just a step in
fulfilling other responsibilities.

The abstract factory is an object. Its methods are implemented with factory methods. In other words,
there is a client that wants to make an object. Instead of creating the object itself (e.g. with a factory
method), it uses a different object (abstract factory) that it is composed with to create the object.
2
Abstract Factory in Java
***Toolkit is an abstract class, not an interface as shown below.
1.
The java.awt.Toolkit interface is an abstract factory. It serves as an abstract layer between the
programmer and the native windowing procedures. Some developers prefer AWT as it generally has
higher fidelity to native look-and-feel than the alternative, Swing.
2.
The SchemaFactory abstract class in Java is a base class used for validating XML. It specifies a number
of methods for obtaining a Schema object as well as static methods for creating a concrete
SchemaFactory.
3.
There are many abstract factories in the Java API. Do a search on Factory in the API and you will see.
Some are simple factories and some are abstract.
3
Other Examples
1.
Text Example
4
5
2.
Data Access Object (DAO) Factory.
3.
Flexible Manufacturing – an AbstractCarFactory, with concrete factories: AltimaFactory,
MaximaFactory, etc. The products are doors, hood, engine, etc. A machine could cut different families
of parts from sheet metal. Or with robotic assembly, a robot could assemble different families of parts
to produce a product.
4.
Flexible Manufacturing – an AbstractComputerFactory, with concrete factories to produce the
specifications for different families of computers.
5.
In a simulation, you could use the Player-Role pattern to model behaviors and a behavior factory to
create them.
6.
An OrderFactory to produce different types of orders. Creation of orders could be complex in the case
of an online store front operation such as Amazon where products that are for sale in the store front
are obtained from different suppliers.
6
7.
The classic example is of a GUIFactory where gui widgets such as buttons, textboxes, etc. need to be
produced for different windowing systems.
8.
EnvironmentFactory produces walls, doors, etc for a game. Different concrete factories can produce
these things for different, but related types of games, or for different levels of the same game:
beginner, intermediate, advanced.
References
1. Wikipedia - Good description.
2. DO Factory - Discusses an example where animals are created for a game. You have Continent
Factories such as Africa, North America that create sets of animals that are native to that area.
3. Net Objectives 4. V.Huston 5. CodeGuru, DevCity - Audio/Video device factory.
6. C# Corner - Automobile factory.
7. C# Corner, D.Hayden, MSDN - Data Access in .NET
8. AllApp - Computer factory.
9. FluffyCat - Soup factory.
10. IToolBox - ASP.NET runtime page processing.
11. Gamelan - UI on multiple windowing systems.
12. Sun - Data Access in Java
13. IBM – XML Validation in Java
14. Rice - List factory.
15. Maze - Maze example
7
Download