Download The Object-Oriented Thought Process

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

United States contract law wikipedia , lookup

Stipulatio wikipedia , lookup

Transcript
The Object-Oriented
Thought Process
Chapter 08
Frameworks and Reuse:
Designing with Interfaces and Abstract Classes
What Is a Framework?
Hand in hand with the concept of code reuse is
the concept of standardization, which is
sometimes called plug and play.
–
The idea of a framework revolves around these
plug-and-play and reuse principles.
– A common framework makes it easier to learn
various applications within the framework.
• It also makes a developer’s life easier by
promoting maximum code reuse
Code Reuse Revisited

Inheritance and composition allow for reuse for
basically one class.
– Frameworks focus on reusing whole or
partial systems.
What Is a Contract?
In the context of this chapter, we will consider a
contract to be any mechanism that requires a
developer to comply with the specifications of
an API.
– Often, an API is referred to as a framework.
–
The online dictionary, Dictionary.com
(http://www.dictionary.com), defines a contract as “an
agreement between two or more parties for the doing or not
doing of something specified” and “an agreement
enforceable by law”.
The Term Contract
The term contract is widely used in many aspects
of business, including software development.
– Do not confuse the concept presented here
with other possible software design
concepts called contracts.
– Enforcement is vital because it is always
possible (perhaps even easy) for a
developer to break a contract.
Abstract Classes
One way a contract is implemented is via an
abstract class.
– An abstract class is a class that contains
one or more methods that do not have any
implementation provided.
• An abstract cannot be instantiated.
• Concrete classes can be instantiated.
Framework Caution
Be aware that in the cases of Shape, Circle, and
Rectangle, we are dealing with a strict
inheritance relationship.
• As opposed to an interface.
– This is an important point because contracts
are not used in cases of composition, or
has-a relationships.
Protocols
Some languages, such as C++, use only abstract
classes to implement contracts.
–
Java and .NET have another mechanism that
implements a contract called an interface.
– In other cases, such as Objective-C, abstract
classes are not provided by the language.
• Thus, to implement a contract in Objective-C,
you need to use a protocol, which is ObjectiveC’s version of an interface.
Interfaces
An interface is similar to an abstract class but it
does not provide any implementation.
 The obvious question is this: If an abstract
class can provide the same functionality as an
interface, why do Java and .NET bother to
provide this construct called an interface?
– And why does Objective-C provide the
protocol?
Interfaces
When using an interface, you do not have to
concern yourself with a formal inheritance
structure.
– You can theoretically add an interface to
any class if the design makes sense.
– An abstract class requires you to inherit
from that abstract class and, by extension,
all of its potential parents.
Interface Terminology
Be aware that you can use the term interface in
several ways, so be sure to use each in the
proper context.




First, the graphical user interface (GUI) is widely used when
referring to the visual interface that a user interacts with—often on
a monitor.
Second, the interface to a class is basically the signatures of its
methods.
Third, in Objective-C you break the code up into physically
separate modules called the interface and implementation.
Fourth, a Java-style interface and an Objective-C protocol are
basically a contract between a parent class and a child class.
Tying It All Together
If both abstract classes and interfaces provide
abstract methods, what is the real difference
between the two?
–
–
Abstract classes require a strict inheritance
relationship and must be related and can provide
implementation.
Interfaces can be used for classes that are not
related and do not provide implementation.
Using Interfaces
An interface specifies certain behavior, but not the
implementation.
– By implementing an interface, you are
saying that you will provide concrete
behavior by implementing methods abstract
methods.
– How you implement these methods is up to
you.
•
All you have to do is to provide the concrete methods.
System Plug-in Points
Basically, contracts are “plug-in points” into your
code.
– Anyplace where you want to make parts of a
system abstract, you can use a contract.
– Instead of coupling to objects of specific
classes, you can connect to any object that
implements the contract.