Ch.6 메시지와 인터페이스

Client-Server Model

  • Client sends a message to the server, which then receives it.

  • Client - message sending object

  • Server - message receiving object

Message and Message Sending

isSatisfied(screening)

  • 메시지 isSatisfied(screening)

    • 메시지 = 오퍼레이션명 + 인자

      • 오퍼레이션명: isSatisfied

      • 인자: screening

  • 메시지 전송: condition.isSatisfied(screening)

    • 메시지 전송 = 메시지 수신자 + 메시지명

      • 메시지 수신자: condition

Messages and Methods

  • Method: A function or a procedure that executes on receiving a message

  • Message sender and receiver cooperates not knowing any details about each other. (which is good)

  • Messages and methods are bound during run-time, which helps decoupling and wrting a code that is both flexibile and extensible.

Public Interfaces and Operations

  • Public Interfaces: a collection of messages publicly open for communication and cooperation.

  • Operations: From a programming perspective, messages includes in public interfaces are called operations. They are abstractions of any action that can be performed.

  • Methods: The code that actually runs on receiving a message.

Signatures

  • Signature = Operation(or method) name + Parameters.

  • Does not include executable codes.

  • Method = Signature + implementations(the code).

Interfaces and Design Quality

  • A good interface is minimal and abstract.

    • The one that includes only what is absolutely necessary.

  • The best way to achieving a minimal and abstract interface is following the responsibility driven architecture.

  • Principles and methodologies that affect the quality of a public interface:

    • Law of Demeter

    • Tell, Don't Ask

    • Intention Revealing Interface

    • Command-Query Separation

Law of Demeter

According to the law of demeter, this is bad:

discountable = screening.getWhenScreened()
    .getDayOfWeek()
    .equals(condition.getDayOfWeek()) // ...
  • Limit cooperative paths so that objects are not coupled to their internal structure.

  • Often summarized as "Use only one dot(.)" to avoid train wrecks

  • In short, don't talk to strangers!

  • "shy codes"

  • Generally, a design that violates the Law of Demeter also violates the interface segregation pinciple (from SOLID).

Tell, Don't Ask

  • A good message tells, but not ask.

  • Don't look for details in messages (and method names, ... and etc.)

Intention Revealing Interface

  • Intention Revealing Selector - Name a method so that its name shows WHAT it does, not HOW it does.

  • Intention Revealing Interface - Encapsulate any information that's related to its implementation, and public interface of an object must only show the intention that's related to cooperation.

Command-Query Separation

  • Routines - functional modules that bind a procedure and give it a callable name. Routines can be divided into procedures and functions.

    • Procedures - can cause side effects but cannot return any value.

      • a.k.a. command in OOP

    • Functions - cannot cause side effects but can return a value.

      • a.k.a. query in OOP

The command-query separation principle can be summarized as "Asking a question should not change its answer".

A routine cannot be a procedure and a function at the same time!

CQS, Referential Transparency, Side Effects, and Immutability

An expression is called referentially transparent if it can be replaced with its corresponding value (and vice-versa) without changing the program's behavior. - Wikipedia

  • A value is immutable when it cannot be modified. Immutability also means no side effects.

Last updated