Architecture of Object-Oriented Systems
ICT2622 - Object-Oriented Analysis · Designing Object-Oriented Systems
Architecture of Object-Oriented Systems
The architecture of object-oriented systems is a critical aspect of software development. It defines the structure and organisation of the system, guiding the design and implementation of software components. Understanding this architecture helps in creating systems that are maintainable, scalable, and reusable.
Key Concepts in Object-Oriented Architecture
Object-oriented architecture is based on several key concepts:
- Classes: Blueprints for creating objects. A class defines properties (attributes) and behaviours (methods) that the objects created from the class will have.
- Objects: Instances of classes. Each object contains its own data and can perform actions defined by its class.
- Encapsulation: The bundling of data and methods that operate on that data within a single unit, or class. This restricts direct access to some of the object's components.
- Inheritance: A mechanism where a new class can inherit attributes and methods from an existing class. This promotes code reusability.
- Polymorphism: The ability to present the same interface for different underlying data types. This allows methods to be used on objects of different classes.
Layered Architecture
A common architectural pattern in object-oriented systems is the layered architecture. This approach separates concerns into different layers, making the system easier to manage and modify.
Layers in Layered Architecture
The layered architecture typically consists of the following layers:
- Presentation Layer: This is the topmost layer that interacts with the user. It displays information to the user and collects input.
- Business Logic Layer: This layer contains the core functionality of the application. It processes input from the presentation layer and makes decisions based on business rules.
- Data Access Layer: This layer manages data storage and retrieval. It interacts with databases or other data sources.
- Database Layer: This is where the actual data is stored. It can be a relational database, a NoSQL database, or any other data storage system.
Example of Layered Architecture
Consider a simple e-commerce application. The architecture might be structured as follows:
- Presentation Layer: Web pages or mobile app interfaces where users browse products.
- Business Logic Layer: Classes that handle user authentication, product selection, and order processing.
- Data Access Layer: Classes that connect to a database to retrieve product information and store user orders.
- Database Layer: A relational database that contains tables for users, products, and orders.
Component-Based Architecture
Another approach to object-oriented architecture is component-based architecture. This style focuses on building software systems from reusable components.
Characteristics of Component-Based Architecture
Key characteristics include:
- Reusability: Components can be reused across different systems, reducing development time.
- Interoperability: Components can work together, even if they are developed using different programming languages or platforms.
- Encapsulation: Each component encapsulates its own functionality and data.
- Loose Coupling: Components are designed to be independent of one another, making it easier to modify or replace them.
Example of Component-Based Architecture
In a banking application, you might have components for user management, transaction processing, and reporting. Each component can be developed and tested independently. For example:
- User Management Component: Handles user registration, login, and profile management.
- Transaction Processing Component: Manages deposits, withdrawals, and transfers.
- Reporting Component: Generates financial reports for users and administrators.
Microservices Architecture
Microservices architecture is an advanced approach that structures an application as a collection of loosely coupled services. Each service is independent and can be developed, deployed, and scaled individually.
Benefits of Microservices Architecture
Some benefits include:
- Scalability: Each service can be scaled independently based on demand.
- Flexibility: Different services can be developed using different technologies.
- Resilience: If one service fails, it does not affect the entire system.
Example of Microservices Architecture
In an online shopping platform, you might have separate microservices for user management, product catalog, order processing, and payment processing. Each service communicates over a network using APIs (Application Programming Interfaces). For example:
- User Management Service: Handles user authentication and profiles.
- Product Catalog Service: Manages product listings and inventory.
- Order Processing Service: Handles the creation and management of customer orders.
- Payment Service: Processes payments and refunds.
Designing Object-Oriented Systems
When designing object-oriented systems, it is essential to follow best practices to ensure a robust architecture.
Best Practices
- Single Responsibility Principle: Each class should have one reason to change. This ensures that classes are focused and easier to maintain.
- Open/Closed Principle: Classes should be open for extension but closed for modification. This allows you to add new functionality without changing existing code.
- Liskov Substitution Principle: Subtypes must be substitutable for their base types. This ensures that derived classes can stand in for their parent class.
- Interface Segregation Principle: Clients should not be forced to depend on interfaces they do not use. This promotes a clean and understandable API.
- Dependency Inversion Principle: High-level modules should not depend on low-level modules, but both should depend on abstractions. This reduces coupling between components.
Example of Best Practices
Consider a class for managing bank accounts. You could have:
class BankAccount {
private double balance;
public BankAccount() {
balance = 0.0;
}
public void deposit(double amount) {
balance += amount;
}
public void withdraw(double amount) {
if (amount <= balance) {
balance -= amount;
} else {
throw new InsufficientFundsException();
}
}
}
This class follows the single responsibility principle by focusing only on account management. If you need to add features like interest calculation, you can extend the class without modifying the existing code.
Remember: Following best practices leads to better software design and easier maintenance.
Common Mistakes in Object-Oriented Architecture
Watch out: A common mistake is creating classes that do too much. This violates the single responsibility principle and makes the code harder to maintain.
Watch out: Another mistake is tightly coupling classes. This makes it difficult to change one class without affecting others.
Summary
- Object-oriented architecture is essential for structuring software systems.
- Common architectural patterns include layered architecture, component-based architecture, and microservices architecture.
- Best practices such as the single responsibility principle improve maintainability and scalability.
Check your understanding
- What are the main layers in a layered architecture?
- Explain the difference between component-based architecture and microservices architecture.
- What is the single responsibility principle in object-oriented design?
- Identify a common mistake when designing object-oriented systems.