translation

This is an AI translated post.

제이온

[Objects] Chapter 1. Objects, Design

Select Language

  • English
  • 汉语
  • Español
  • Bahasa Indonesia
  • Português
  • Русский
  • 日本語
  • 한국어
  • Deutsch
  • Français
  • Italiano
  • Türkçe
  • Tiếng Việt
  • ไทย
  • Polski
  • Nederlands
  • हिन्दी
  • Magyar

Summarized by durumis AI

  • In software development, practice is more important than theory, and through simple programs, we can learn the importance of object-oriented design.
  • Using a ticket sales application as an example, we will explain how to write code that is flexible to changes by reducing dependencies and coupling between objects, and compare the differences between procedural and object-oriented programming.
  • Object-oriented design creates code that can be changed, and objects are autonomous entities that take responsibility for their own data, so dependencies between collaborating objects must be managed appropriately.

Introduction

Robert L. Glass argued that practice precedes theory. Especially in software development, practice is a field where practice precedes theory. Developers gain the most by getting their hands dirty, touching concrete code. Therefore, we will put theory and concepts aside for now and take a look at a simple program.


Implementing a Ticket Sales Application

  • We are planning a small event to promote the small theater.
    • Event Content: Sending invitations to watch the performance for free to randomly selected audience members.
  • We need to separate the audience members who won the event from those who didn't.
    • Event winners: Exchange the invitation for a ticket.
    • Event losers: Purchase a ticket for money.



What is the Problem?

Robert Martin explains the three functions that a software module (any element that makes up a program, regardless of size, such as a class, package, or library) should have.


  • It works properly during execution.
  • It exists for change.
    • Changes should be possible with simple tasks.
  • It communicates with the person reading the code.
    • Developers should be able to easily read and understand.


The previous ticket sales application satisfies the first constraint of working properly, but it fails to meet the goals of changeability and communication.


Code that misses the mark

Let's look at why it doesn't meet the communication goal.


  • What the enter() method of the Theater class does
    • The theater opens the audience's bag and checks if there is an invitation inside.
    • If there is an invitation in the bag, it tells the salesperson to move the ticket stored in the ticket office into the audience's bag.
    • If there is no invitation in the bag, it takes out the ticket price from the audience's bag, buys the ticket, and puts the ticket in the bag.
  • From the audience's perspective, the theater, a third party, has to look through the bag, take money, and put in a ticket.
  • From the salesperson's point of view, the theater, a third party, manipulates the value of the tickets and cash in the ticket office without permission.
  • Understandable code means code that doesn't deviate much from our expectations. The above theater violates our expectations.
    • Audience members should take out money from their bags themselves and pay the salesperson to receive a ticket.
    • The salesperson should take the ticket directly from the ticket office and give it to the audience. The salesperson should also receive money from the audience and keep it in the ticket office.
  • In addition, you need to remember all the details to understand the enter() method.
    • Audience has a Bag.
    • Bag contains cash and a ticket.
    • TiketSellet sells tickets from TicketOffice. There is money and a ticket in TicketOffice.


Code vulnerable to changes

The enter() method assumes 2 conditions.

  • The audience always carries a bag to hold cash and an invitation.
  • The salesperson sells tickets only at the ticket office.


Then what about the situation below?

  • The audience may not have a bag.
  • The audience may use a credit card instead of cash.
  • The salesperson may sell tickets outside the ticket office.


For example, to meet the first requirement, we need to remove the Audience's Bag object and modify the Theater class's enter() method accordingly. This is because the Theater class is overly dependent on the fact that the audience is carrying a bag and the salesperson is only selling tickets at the ticket office. If any of these details change, we need to modify both the relevant class and the dependent class (ex. Theater).


This is a problem related to dependency between objects, and dependency implies an impact on changes. However, object-oriented design aims to build a community of objects that cooperate while depending on each other, so we need to minimize the dependencies needed to implement the functionality of the application, not just eliminate them.Keep the minimum dependenciesand remove unnecessary dependencies.


High coupling when there are too many dependencies between objectsis said to be high, and the higher the coupling between two objects, the higher the probability that they will change together. Therefore, the goal of design is to create a design that is easy to change by reducing the coupling between objects.


Improving the Design

Theater does not need to know that the audience has a bag and that the salesperson sells tickets at the ticket office. Theater just wants the audience to enter the theater. Therefore, we need to make the audience and salesperson autonomous entities so that the audience can handle the cash and invitations in their bag themselves, and the salesperson can handle the tickets and sales fees in the ticket office themselves.


Let's increase autonomy

Objects that perform only closely related tasks and delegate unrelated tasks to other objects are said to have high cohesion. Creating autonomous objects that handle their own data can reduce coupling and increase cohesion.


Procedural and Object-Oriented

  • Procedural Orientation
    • Theater's enter() method is a process, and Audience, TicketSeller, Bag, and TicketOffice are data.
    • The approach of placing processes and data in separate modules is called procedural programming.
    • There is a lot of code that goes against our intuition. (ex. The audience manages their own money and invitations.)
    • It is difficult to narrow down the impact of data changes.
    • Responsibilities are managed centrally. (Theater manages everything)
  • Object Orientation
    • The approach of placing data and processes inside the same module is called object-oriented programming.
    • We can write code that matches our intuition.
    • The impact of data changes can be effectively narrowed down through encapsulation.
    • Each object is responsible for itself.


It can be further improved.

  • The Bag class of the Audience class is still a passive entity dragged around by the Audience class, so we need to make the Bag class an autonomous object.
  • The TicketOffice of the TicketSeller class is also managed at will by TicketSeller. We need to make TicketOffice an autonomous object.
    • However, after the change, TicketOffice has an additional coupling with Audience.
    • Design requires careful consideration of trade-offs. In this case, we can agree to make TicketOffice a somewhat passive object to reduce coupling with Audience.



That's a lie!

  • Even if it is a passive entity in reality, in the world of object orientation, everything becomes an active and autonomous entity.
  • It's a good idea to use personification to think of passive objects as entities that laugh, talk, and get angry.


Object-Oriented Design

Why is design necessary?

  • Design is the arrangement of code.
  • Good design is one that fully performs the functions required today and can smoothly accommodate tomorrow's changes.


Object-Oriented Design

  • Changeable code is easy to understand code.
  • The object-oriented paradigm helps us write code the way we see the world.
  • Objects are autonomous entities that are responsible for their own data.
  • Great object-oriented design is a design that appropriately manages dependencies between cooperating objects.


Source

  • Objects
제이온
제이온
제이온
제이온
[Objects] Chapter 2. Object-Oriented Programming This document explains the object-oriented programming methodology for implementing a movie reservation system, covering concepts such as collaboration, objects, classes, inheritance, polymorphism, abstraction, and composition. It presents methods for sec

April 28, 2024

[Effective Java] Item 1: Consider Static Factory Methods Instead of Constructors Static factory methods provide a flexible and efficient way to create instances instead of constructors. They can have names, return instances that meet specific conditions, and improve performance through caching. Unlike the singleton pattern, they can c

April 27, 2024

[Effective Java] Item 6. Avoid Unnecessary Object Creation This guide provides advice on how to reduce unnecessary object creation in Java. For immutable objects like String and Boolean, use literals, and for regular expressions, it is better to cache Pattern instances. Also, autoboxing can lead to performance de

April 28, 2024

[Non-Computer Science, Surviving as a Developer] 14. Summary of Frequently Asked Technical Interview Questions for New Developers This is a technical interview preparation guide for new developers. It explains concepts frequently encountered in interviews such as the main memory area, data structures, RDBMS and NoSQL, procedural and object-oriented, overriding and overloading, page
투잡뛰는 개발 노동자
투잡뛰는 개발 노동자
투잡뛰는 개발 노동자
투잡뛰는 개발 노동자

April 3, 2024

Relational Data Modeling Relational data modeling is the process of dividing real-world information into tables and data, going through the stages of requirement analysis, conceptual data modeling, logical data modeling, and physical data modeling. Conceptual modeling is visualiz
제이의 블로그
제이의 블로그
제이의 블로그
제이의 블로그

April 8, 2024

Transfer Love 2 and Organizational Culture: The Power of Observation - 2 Rather than relying on external consultants to improve organizational culture, organizations should analyze and seek change through 'internal' observation, including employee interactions, spatial arrangements, and symbols. Popular program "Transfer Love"
Byungchae Ryan Son
Byungchae Ryan Son
Byungchae Ryan Son
Byungchae Ryan Son
Byungchae Ryan Son

May 9, 2024

What kind of tests should you run for 1-person app development? Learn how to prioritize tests and develop an effective testing strategy when developing an app. The author prioritizes human testing, integration testing, unit testing, and acceptance/widget testing, emphasizing time efficiency. Check out practical testin
Alien Story
Alien Story
Alien Story
Alien Story
Alien Story

May 16, 2024

Transfer Love 2 and Organizational Culture: The Power of Observation -1 The 'front seat' in the hospital waiting room is a value proposition created by the nurse's consideration, but it conflicts with the doctor's value of focusing on patients, showing the reality of organizational culture. Culture is not defined, but underst
Byungchae Ryan Son
Byungchae Ryan Son
Byungchae Ryan Son
Byungchae Ryan Son
Byungchae Ryan Son

May 9, 2024

[Javascript] Object Structure (V8) JavaScript's Object in the V8 engine is optimized to work like a structure in Fast Mode and as a hash map in Dictionary Mode, depending on the state. Fast Mode is fast when keys and values are almost fixed, but if a new key is added or an element is delet
곽경직
곽경직
곽경직
곽경직
곽경직

March 18, 2024