The main question about OOP

I often have the same problems when I have to create my own class for a web application. Requirements: - supported (for example, without copying) - the layers are completely separated (the business layer does not need to know which method is used for the data layer) - high performance: do not load useless data.

First, I have a table with all my clients and their addresses: Code:

Customer
--Id
--Name
--Address
----City
----ZC
----Street

Now I need a table (on another page) with all my clients and the books they bought, I have several options:

1 / I create a new class:

The code:

CustomerWithBooks
--Id 
--Name
--Books[]
----ID
----name

PRO: I load only useful data CONS: I create my class after my user interface, and there is a copy.

2/ Books [] . PRO: , CONS: . , : , , , , DAL , .

3/ : :

ClientBase
--ID
--Name

ClientWithBooks : ClientBase
--Books[]

ClientWithAdress : ClientBase
--Address

PRO: , CONS: , ?

4/?? ,

+3
5

1 , . - . , / - ( ).

"" : , -.

  • A. , , , , .
  • B. /, . -, , ( ), , . /, .. , , , .

, , , , . , db/object-model, , , . , , , 1 , , . , , ,

  • ,
  • ,
  • , Order.

. , , , - , , . , , sql-, , Orders db. ( ) , . , , , . , .

+3

2 3, , . , , , , , , , , . , , . - , . , , .

, 1. , , CustomerBook, , . , scenerio.

+1

Customer .

Bookshelf
--Books[]

, Customer , , . PHP-:

class BookshelfFactory {
  public static function getBookshelf(Customer $customer) {
     // perform some fetching here
     return $bookshelf;
  }
}
0

OOA & D. ( ). OOA & D , ( , ). :

Customer
+getBooks():List<Book>
+getAddress():Address
0

, .

JOINS, , .

1- .

, , , .

0

Source: https://habr.com/ru/post/1733542/


All Articles