This Note contains:
- Object Technology
- Class
- 4 principles of object orientation
Object Technology
A set of principles guiding software construction together with languages, databases, and other tools that support those principles
Basic Ideas
Everything is an object.
A program is a collection of objects telling each others what to do by sending messages.
*Benefit: * Programs closely reflect our perception of the real world.
Object
A object represents an entity:
- Physical entity
- Such as a Boat
- Conceptual entity
- an experiment
- Software entity
- linked list
An object is an entity with:
- Unique identity
- State (attributes)
- Condition in which the object exists, which might change over time
- For example, as a student entity, name, id, status, credits can be in the state
- Behavior (operations)
- How an object acts and reacts to request from other objects
- For example, as a student entity, behavior can be enroll, graduate, set_credits, get_credits, etc.
Class
A class is a description of a set of objects that share the same structure and behavior.
A class include three sections:
- Class name
- Structure (attributes)
- Consider the variable inside of a class
- Behavior (operations)
- Consider the functions inside of a class
A class serves as a template for creating objects.
An object is an instance of a class.
Basic Principle of Object Orientation
Abstraction
Essential characteristics of an entity relative to a given perspective.
An abstraction is a description of an entity, that highlights the most important characteristics and eliminates other details.
Abstraction is context dependent. If you describe a car for a mechanic, you are going to focus on the engine and related system. But if you describe a car for a car dealer, you might focus on the price and other related info. In both cases, you are going to ignore any information that is not relevant to your problem.
Benefit
Abstraction manages complexity by eliminating details.
Encapsulation
Grouping of related elements (attributes and operations) into a single entity
Encapsulation is about grouping a set of the attributes and operations together to form an entity.
Encapsulation does not require any hiding of information. However, encapsulation supports information hiding by providing interfaces.
Information hiding: Limits access to attributes and hides implementation details.
Benefit
Encapsulation manages complexity by combining related elements into a single entity and facilitates information hiding.
Information hiding protects an object’s internal sate from being corrupted by its clients and it protects the client code from changes in the object’s implementation.
Hierarchy
Ranking or ordering of entities into a tree-like structure
Hierarchy can be defined as any ranking or ordering of entities into a tree structure. An entities at the same level if the hierarchy should be at the same level of abstraction.
Aggregation hierarchy is which an entity is made_up of other entities. here is an example of a UML class diagram:
Inheritance hierarchy is which a subclass (or child) inherits from a superclass (or parent).
A subclass inherits its parent’s attributes, operations, and relationships and a subclass can add its own attributes and operations.
Inheritance implements polymorphisms. Subclass can can overwrite any operations from the superclass.
*Polymorphos: *Having many forms
Benefit
Hierarchy manages complexity by organizing entities into a tree-like structure.
Inheritance comes with code reuse and extensibility.
Modularity
Decomposition of a large entity into smaller modules
Modularity is about breaking a large and complex system into smaller and more manageable modules.
This is also applicable at the class level, where you could break a large class into smaller ones.
The goal is to achieve low coupling and high cohesion.
Low coupling: modules are independent. You can change one without affecting the others.
High cohesion:Each module has its own unique responsibility.
Low coupling and high cohesion make systems easy to understand and maintain.
Benefit
Modularity manages complexity by breaking large entities into smaller, independent and cohesive modules.
- 本文作者: Depasinre
- 本文链接: https:/Depasinre.github.io/2024/01/29/18652-week3/
- 版权声明: 本博客所有文章除特别声明外,均采用 MIT 许可协议。转载请注明出处!