Показать сообщение отдельно
  #2 (permalink)  
Старый 01.09.2019, 23:08
Профессор
Отправить личное сообщение для Rise Посмотреть профиль Найти все сообщения от Rise
 
Регистрация: 07.11.2013
Сообщений: 4,662

Мне кажется надо в объектной парадигме писать, т.е. определить абстракции (Витрина, Корзина, ...), их интерфейсы (наборы методов), типа так:
var cart = {
    items: [],
    add: function (item) {},
    remove: function (item) {},
};

Небольшой ликбез, "C++ Primer" (3rd edition, Lippman & Lajoie):
Цитата:
[Algorithms + Data = Program]
There are two primary aspects to the programs we write.
1. A collection of algorithms (that is, the programmed instructions to solve a particular task).
2. A collection of data against which the algorithms are run to provide each unique solution.
[Algorithms ~ Data = Paradigm]
These two primary program aspects, algorithms and data, have remained invariant throughout the short history of computing. What has evolved is the relationship between them. This relationship is spoken of as a programming paradigm.
[Procedural-Based Programming]
In the procedural programming paradigm, a problem is directly modeled by a set of algorithms. A check-out/check-in system for loan materials of a public library, such as books, videos, and so on, is represented as a series of procedures, the two central procedures being the checking-out and checking-in of library materials. The data is stored separately, accessed either at a global location or by being passed into the procedures. [...] Individual procedures, such as check_in(), check_out(), overdue(), fine(), and so on, are referred to as functions. [...]
[Object-Based Programming]
In the 1970s, the focus of program design shifted from the procedural paradigm to that of abstract data types (now generally referred to as object-based programming). In this paradigm, a problem is modeled by a set of data abstractions. [...] we refer to these abstractions as classes. Our library check-out system, for example, under this paradigm is represented as the interaction between object instances of classes such as Book, Borrower, DueDate (an aspect of Time), and the inevitable Fine (an aspect of Money), representing the library abstractions. The algorithms associated with each class are referred to as the class's public interface. The data is privately stored within each object; access of the data is hidden from the general program. [...]
[Object-Oriented Programming]
Object-oriented programming extends abstract data types through the mechanisms of inheritance (a "reuse" of an existing implementation) and dynamic binding (a reuse of an existing public interface). Special type/subtype relationships between previously independent types are now provided. A book, videotape, recording, and children's puppet are each a kind of library material, although each has its own check-out and check-in policy. The shared public interface and private data are placed in an abstract LibraryMaterial class. Each specific library material class inherits the shared behavior from the LibraryMaterial abstract class and need provide only the algorithms and data that support its behavior. [...]
Ответить с цитированием