Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"Client" concept in OOP Design Patterns?

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!

like image 725
Trần Minh Avatar asked Apr 06 '13 06:04

Trần Minh


People also ask

What is client code in OOP?

Client Code - the code that uses the classes under discussion. Coupling - code in one module depends on code in another module Change in one forces rewrite (horrible!), or recompile (annoying), of code in the other. Four key concepts of OOP. Abstraction - responsibilities (interface) is different from implementation.

What is the relationship between OOP and design patterns?

Object Oriented Programming is a programming methodology or concept of programming that organizes code into objects and relationships of objects. Design Patterns would suggest proven-successful methods of constructing types/objects to solve a certain scenario in a program.


2 Answers

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.

like image 66
Ray Tayek Avatar answered Sep 21 '22 03:09

Ray Tayek


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.

like image 32
phs Avatar answered Sep 25 '22 03:09

phs