Movatterモバイル変換


[0]ホーム

URL:


Jump to content
WikipediaThe Free Encyclopedia
Search

Proxy pattern

From Wikipedia, the free encyclopedia
Software design pattern

Incomputer programming, theproxy pattern is asoftware design pattern which it's a class functioning as an interface to something else.[vague] The proxy could interface to anything: a network connection, a large object in memory, a file, or some other resource that is expensive or impossible to duplicate. In short, a proxy is a wrapper or agent object that is being called by the client to access the real serving object behind the scenes. Use of the proxy can simply beforwarding to the real object, or can provide additional logic. In the proxy, extra functionality can be provided, for example caching when operations on the real object are resource intensive, or checking preconditions before operations on the real object are invoked. For the client, usage of a proxy object is similar to using the real object, because both implement the same interface.

Overview

[edit]

The proxy design pattern is one of the twenty-three well-knownGoF design patterns that describe how to solve recurring design problems to design flexible and reusableobject-oriented software, that is, objects that are easier to implement, change, test, and reuse.[1]

Problem

[edit]

The proxy design pattern is used to solve such problems:[2]

  • The access to an object should be controlled.
  • Additional functionality should be provided when accessing an object.

When accessing sensitive objects, for example, it should be possible to check that clients have the needed access rights.

Solution

[edit]

Define a separateProxy object that

  • can be used as a substitute for another object (Subject), and
  • implements additional functionality to control the access to this subject.

This makes it possible to work through aProxy object to perform additional functionality when accessing a subject, like checking the access rights of clients accessing a sensitive object.

To act as a substitute for a subject, a proxy must implement theSubject interface. Clients can't tell whether they work with a subject or its proxy.

See also the UML class and sequence diagram below.

Structure

[edit]

UML class and sequence diagram

[edit]
A sample UML class and sequence diagram for the Proxy design pattern.[3]

In the aboveUMLclass diagram, theProxy class implements theSubject interface so that it can act as substitute forSubject objects. It maintains a reference (realSubject) to the substituted object (RealSubject) so that it can forward requests to it(realSubject.operation()).

The sequence diagram shows the run-time interactions: TheClient object works through aProxy object thatcontrols the access to aRealSubject object.In this example, theProxy forwards the request to theRealSubject, which performs the request.

Class diagram

[edit]
Proxy inUML
Proxy inLePUS3 (legend)

Possible usage scenarios

[edit]

Remote proxy

[edit]

Indistributed object communication, a local object represents a remote object (one that belongs to a different address space). The local object is a proxy for the remote object, and method invocation on the local object results inremote method invocation on the remote object. An example would be anATM implementation, where the ATM might hold proxy objects for bank information that exists in the remote server.

Virtual proxy

[edit]
Further information:Lazy loading

In place of a complex or heavy object, a skeleton representation may be advantageous in some cases. When an underlying image is huge in size, it may be represented using a virtual proxy object, loading the real object on demand.

Protection proxy

[edit]

A protection proxy might be used to control access to a resource based on access rights.

Example

[edit]

The following is an example of the proxy design pattern inC++:

importstd;usingstd::string;usingstd::unique_ptr;// Subject InterfaceclassImage{public:virtualvoiddisplay()const=0;virtual~Image()=default;};// Real Subject (expensive to load)classRealImage:publicImage{private:stringfilename;public:explicitRealImage(conststring&file):filename{file}{loadFromDisk();}voiddisplay()constoverride{std::println("Displaying image: {}",filename);}private:voidloadFromDisk()const{std::println("Loading image from disk: {}",filename);}};// Proxy (controls access to RealImage)classProxyImage:publicImage{private:stringfilename;mutableunique_ptr<RealImage>realImage;// lazily createdpublic:explicitProxyImage(conststring&file):filename{file}{}voiddisplay()constoverride{if(!realImage){std::println("(Proxy) Loading image on demand...");realImage=std::make_unique<RealImage>(filename);}realImage->display();}};// Client codeintmain(){Image*img=newProxyImage("photo.png");// First display (should load)img->display();// Second display (should use cached image)img->display();deleteimg;return0;}

See also

[edit]

References

[edit]
  1. ^Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides (1994).Design Patterns: Elements of Reusable Object-Oriented Software. Addison Wesley. pp. 207ff.ISBN 0-201-63361-2.{{cite book}}: CS1 maint: multiple names: authors list (link)
  2. ^"The Proxy design pattern - Problem, Solution, and Applicability".w3sDesign.com. Retrieved2017-08-12.
  3. ^"The Proxy design pattern - Structure and Collaboration".w3sDesign.com. Retrieved2017-08-12.

External links

[edit]
Wikimedia Commons has media related toProxy pattern.
Gang of Four
patterns
Creational
Structural
Behavioral
Concurrency
patterns
Architectural
patterns
Other
patterns
Books
People
Communities
See also
Retrieved from "https://en.wikipedia.org/w/index.php?title=Proxy_pattern&oldid=1335703767"
Category:
Hidden categories:

[8]ページ先頭

©2009-2026 Movatter.jp