问题
I just started to read the Applying UML and Patterns(2nd edition) book. Is one of the GRASP Patterns the Information Expert, equivalent with the Modell form MVC?
Do they have the same responsibilities(Storing and retrieving information)?
回答1:
Is Information Expert from GRASP and Modell from MVC the same?
Yes and No.
Do they have the same responsibilities(Storing and retrieving information)?
No. Information expert's eligibility is not restricted to storing and retrieving information. It is more commonly done by a persistent layer which we may call "pure fabrication".
GRASP patterns (I prefer them as principles) are used to determine which responsibility belongs to which class or if this responsibility necessitates a new class. It helps us keep our design tidy at the class level. On the other hand, MVC is about design of the whole system - how you divide it. Each of the 3 parts of MVC usually contains multiple classes.
Architectural pattern and/or design pattern is decided first. Then we apply the GRASP principles for each of the responsibilities the system has to perform.
Suppose, we have an application but the clients want it in different flavours or UIs. The data and business logic are the same. So MVC is a good candidate to keep the model separate and reuse it for all clients.
Now in our model, we have a Customer class. One responsibility of the system is to send an email to the customer. Customer
class stores information about the customer and so it is an information expert. We store email address in Customer
class and add a method getEmailAddress()
. If we add the responsibility of "sending email" to Customer
class, it violates "high cohesion". So we can apply "pure fabrication" and add a new class EmailSender
which takes an email address, subject and email body. This example may sound silly but that's the best I could come up with.
(In MVC architecture, model has all the data that controller and view need. I guess, OP has thought model as the information expert from that perspective.)
来源:https://stackoverflow.com/questions/22718037/is-information-expert-from-grasp-and-modell-from-mvc-the-same