The Biggest Mistake Beginners Make
Most beginners think:
Classes are containers of data.Example:
class Customer
{
string name;
};
class Order
{
int id;
};Professionals think differently.
They think:
Objects Have Responsibilities
The Fundamental Principle
Objects exist to:
Manage Information
Perform BehaviorNot merely store data.
Bad:
class Customer
{
public:
string name;
string email;
};Question:
What does Customer do?
Nothing.
It is just a data structure.
Better:
class Customer
{
public:
void placeOrder();
void updateProfile();
void cancelOrder();
};Now the object has responsibilities.
Object-Oriented Design Starts With Responsibilities
Professional designers ask:
What responsibilities exists?Before asking:
What classes exist?Why?
Because classes emerge from responsibilities.
Example:
Requirement:
Customer places order.Responsibility:
Place OrderPotential Object:
CustomerRequirement:
Payment must be processed.Responsibility:
Process PaymentPotential object:
PaymentServiceNoun Analysis
One of the oldest object discovery techniques.
Idea
Read requirements.
Extract nouns.
Example:
Customer places an order containing products.Nouns:
Customer
Order
ProductPotential classes:
class Cutomer {}
class Order {}
class Product {}Very simple.
But only a starting point.
Example: Library System
Requirement:
Members borrow books from the library.Nouns:
Book
Member
LibraryPotential classes:
class Member {};
class Book {};
class Library {};The Problem With Pure Noun Analysis
Not every noun should become a class.
Requirement:
Customer enters email address.Nouns:
Customer
Email
AddressShould we create:
class Email {};
class Address {};Maybe or maybe not, depends on the domain.
This is why professional design goes beyond nound analysis.
Responsiblity-Driven Design
A powerful mindset.
Question:
Who should do this?Instead of:
What class should exist?Example:
Requirement:
Calculate order total.Who owns the information?
Order contains:
Products
Quantities
PricesTherefore:
class Order
{
public:
double calculateTotal();
};Why?
Because Order has the information.
Information Expert Principle
One of the most important OO principles.
Definition:
Assign responsibility to the class that has the necessary information.
Example:
Who calculates order total?
Customer?
NoPayment?
NoOrder?
YesOrder owns:
Items
Prices
QuantitiesTherefore:
class Order
{
public:
double calculateTotal();
};This principle prevents bad designs.
Example: ATM
Requirement:
Check Account BalanceWho knows balance?
AccountNot:
ATM
Customer
CardGood design:
class Account
{
public:
double getBalance();
};CRC Cards
One of the most useful object design techniques.
CRC stands for:
Class
Responsibilities
CollaboratorsInvented to help discover object models.
Each class is represented as:
+---------------------------+
| Class Name |
+---------------------------+
| Responsibilities |
+---------------------------+
| Collaborators |
+---------------------------+Example: Customer CRC
+--------------------------+
| Customer |
+--------------------------+
| Place Order |
| Update Profile |
| View Orders |
+--------------------------+
| Order |
+--------------------------+Example: Order CRC
+--------------------------+
| Order |
+--------------------------+
| Add Product |
| Calculate Total |
| Track Status |
+--------------------------+
| Product |
| Payment |
+--------------------------+Collaborations
Objects rarely work alone.
Example:
Order Placement
Customer
|
v
Order
|
v
PaymentQuestion:
How do objects cooperate?This is collaboration
Leave a comment
Your email address will not be published. Required fields are marked *


