"Client" concept in OOP Design Patterns?
Asked Answered
I

4

8

I read many topics about OOP Design Patterns of GoF, but i am not sure about "Client" concept. So what is it? How can we realize it in our application. Thank!

Iliad answered 6/4, 2013 at 6:25 Comment(0)
C
6

In the gof book, the client is the code or class that is using the classes in the pattern.

for example, from the abstract factory pattern under motivation:

diagram with client

Consider a user interface toolkit that supports multiple look-and-feel standards, such as Motif and Presentation Manager. Different look-and-feels define different appearances and behaviors for user interface "widgets" like scroll bars, windows, and buttons. To be portable across look-and-feel standards, an application should not hard-code its widgets for a particular look and feel. Instantiating look-and-feel-specific classes of widgets throughout the application makes it hard to change the look and feel later.

We can solve this problem by defining an abstract WidgetFactory class that declares an interface for creating each basic kind of widget. There's also an abstract class for each kind of widget, and concrete subclasses implement widgets for specific look-and-feel standards. WidgetFactory's interface has an operation that returns a new widget object for each abstract widget class. Clients call these operations to obtain widget instances, but clients aren't aware of the concrete classes they're using. Thus clients stay independent of the prevailing look and feel.

There is a concrete subclass of WidgetFactory for each look-and-feel standard. Each subclass implements the operations to create the appropriate widget for the look and feel. For example, the CreateScrollBar operation on the MotifWidgetFactory instantiates and returns a Motif scroll bar, while the corresponding operation on the PMWidgetFactory returns a scroll bar for Presentation Manager. Clients create widgets solely through the WidgetFactory interface and have no knowledge of the classes that implement widgets for a particular look and feel. In other words, clients only have to commit to an interface defined by an abstract class, not a particular concrete class.

A WidgetFactory also enforces dependencies between the concrete widget classes. A Motif scroll bar should be used with a Motif button and a Motif text editor, and that constraint is enforced automatically as a consequence of using a MotifWidgetFactory.

Cavern answered 6/4, 2013 at 7:24 Comment(3)
can you example with Observer PatternFerreous
i added an example from the abstract factory pattern.Cavern
Thank a lot. I read it in GoF Design Pattern : Elements of Reusable Object-Oriented Software.Ferreous
I
3

As a pattern, a client is an actor that initiates an interaction with a server, which is a functional, but typically passive, actor. Acting on the client's behalf as described by a request, the server performs some action and makes a report back in the form of a response.

As such, the point of a client interface is to make it convenient or possible for arbitrary code to formulate a request and attract the attention of a server. Since the request message might be conveyed over a wide variety of media (a different memory space, for example), some kind of transparent transport is usually involved, hidden behind this request interface.

That's pretty much the long and short of it as a concept. One of the drawbacks of a very flexible pattern (which certainly applies to client/server) is one needs to descend into a specific example, framework or library to speak concretely.

Intarsia answered 6/4, 2013 at 6:32 Comment(1)
Thank for your quick question, but maybe i still can not understand this in a OOP software design pattern.Ferreous
U
1

A client is a caller/consumer. A client is not a subclass/implementer. In terms of a method, a client is the caller of that method. In terms of a class, a client is the caller of methods in that class.

You could say that every method has a client, because without a caller a method is dead code; however, the term client is typically reserved for the caller of a public method, since private methods are just implementation details, not relevant to design.

In a design diagram, such as a UML class diagram, a client indicates where the public access points are and how the design is used after it is implemented.

Unutterable answered 7/5, 2022 at 0:36 Comment(0)
P
0

The client is just another module, or class, form the system use the concrete Pattern (all or part of the components construct the pattern)

Pampuch answered 7/4, 2013 at 11:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.