4. B1: Interface, Abstract Class, and
Concrete Class
An interface is an empty shell, there are only the signatures
(name / params / return type) of the methods.
Abstract classes look a lot like interfaces, but they have
something more : you can define a behavior for them.
When you use new you are certainly instantiating a
concrete class, so that’s definitely an implementation, not
an interface.
6. Association is reference based relationship between two
classes.
Dependency is often confused as Association. Dependency
is normally created when you receive a reference to a class
as part of a particular operation / method.
7. An extends relationship (also called an inheritance or an is-a
relationship) implies that a specialized (child) class is based on a
general (parent) class.
An implements relationship exists between two classes when one of
them must implement, or realize, the behavior specified by the other.
8. Aggregation is same as association and is often seen as redundant
relationship. A common perception is that aggregation represents one-
to-many / many-to-many / part-whole relationships
Composition relates to instance creational responsibility. When class B
is composed by class A, class A instance owns the creation or controls
lifetime of instance of class B.
14. Defines an interface for creating objects, but let subclasses
to decide which class to instantiate.
Refers to the newly created object through a common
interface.
15.
16.
17. class PizzaStore!
def order_pizza(pizza_type)!
pizza = create_pizza(pizza_type)!
pizza.prepare!
pizza.bake!
pizza.cut!
pizza.box!
pizza!
end!
!
def create_pizza(pizza_type)!
raise "need to implement"!
end!
end!
!
class NYPizzaStore < PizzaStore!
def create_pizza(pizza_type)!
const_get("NYStyle#{pizza_type.capitalize}Pizza")!
end!
end!
!
class ChicagoPizzaStore < PizzaStore!
def create_pizza(pizza_type)!
const_get("ChicagoStyle#{pizza_type.capitalize}Pizza")!
end!
end!
!
class NYStyleCheesePizza < Pizza!
def prepare!
"prepare for cheese pizza of NY style"!
end!
end
24. Factory Method vs. Abstract Factory
The methods of an Abstract Factory are implemented as factory
methods.
Both Abstract Factory and Factory Method create objects, but
Factory Method do it through inheritance, and Abstract do it
through object composition.
Abstract Factory is used whenever you have families of products
you need to create and you want to make sure your clients create
products that belong together.
Factory Method is used to decouple your client code from the
concrete classes you need to instantiate, or if you don’t know
ahead of time all the concrete classes you are going to need.