Cosmetic mode (decorator pattern), also known as the decorator mode, is a design pattern that dynamically adds new behavior to a class in the field of object-oriented programming. In terms of functionality, cosmetic patterns are more flexible than generating subclasses, which adds functionality to an object rather than to the entire class. The decorative pattern is ideal for flexible extension of the object's functionality, and the following is a uml diagram of the decorative pattern:
For example, there is a technical forum, the user through the message to communicate, because the beginning of the forum are acquaintances, almost no need to comment on the content of the audit, receive the Message page can be this:
Class Savemsg () {
private $msg;
Public function __construct ($msg) {
$this->msg= $msg;
}
Public Function __store () {
//stored in database
}
}
Later, as the forum became famous, there are some people in the above link, you need to filter the message containing the link, the Forum further development, found in addition to the development of garbage links, there are a lot of useless irrigation, and then there may be attacks and so on a variety of abnormal posts, so the forum posts management, A class can be abstracted separately for management, and dynamic expansion can be performed when the filtering rules need to be expanded.
//base class abstract class filter{abstract public Function isforbid ();}//Base Filter class class Msgfilter Exten
DS filter{Public $content;
Public function __construct ($msg) {$this->content= $msg;
The Public Function isforbid () {if (Preg_match ("/https?/i", $this->content)) {return [true, ' not allowed Urls '];
}else{return [FALSE];
The}}//adorners are used to extend the functionality of the abstract class Filterdecorator extends filter{protected $obj;
The Public function __construct (Filter $obj) {$this->obj= $obj; }//New filter to determine whether to repeat the posting class repeat extends filterdecorator{public function isforbid () {if ($this->obj->isforbid () [0]
= = = True) {//Decide whether to include URL return $this->obj->isforbid ();
}else if ($this->obj->content = = "This is a test") {//Decide whether to repeat the post return [true, "Repeat Posts"];
}else{return [FALSE];
}} $test = new Msgfilter ("Httpsfdjoafdsajof");
Print_r ($test->isforbid ())//prohibited $test 2 = new Repeat (new Msgfilter ("This is a Test"); Print_r ($test 2->isforbid ());/Is prohibited
In Python, there is no abstract class or method, and the implementation is simpler:
#!/usr/bin/env python
class Filter ():
Pass
class Msgfilter (filter):
def __init__ (self,msg):
Self.content=msg
def isforbid (self):
if (' http ' in self.content): Return
[True, ' not allowed URL ']
else: Return
[False]
class Filterdecorator (Filter):
def __init__ (self,obj):
self._obj=obj
class Repeat (filterdecorator):
def isforbid (self):
if Self._obj.isforbid () [0]: return
self._ Obj.isforbid ()
elif self._obj.content = = ' This was a test ': return
[True, Repeat Posts];
else: Return
[False]
test = Msgfilter (' This is a content have HTTP URL ')
print test.isforbid ()
Test2 = Repeat (Msgfilter (' is a Test '))
print test2.isforbid ()
In JavaScript, there is no strict class, and all inheritance is based on prototypes, which can be a bit time-consuming to understand:
function Msgfilter (msg) {
this.content=msg;
This.isforbid=function () {
if (This.content.match (/http/g)) {return
[true, ' not allowed URL '];
} else {return
[false];
}}} function Repeat (obj) {
var _obj=obj;
This.isforbid=function () {
if (_obj.isforbid[0] = = True) {return
_obj.isforbid ();
} else if (_obj.content== ' a test ') {return
[true, ' Repeat Posts '];
} else{return
[false];
}} var test = new Msgfilter ("He is a content have HTTP URL");
Console.log (Test.isforbid ());
var test2 = new Repeat (New Msgfilter ("This is a Test"));
Console.log (Test2.isforbid ());
Because JavaScript lacks the attributes of a class, inheritance for it is a little bit of chicken, the above code looks more like a two function processing, in Python, there is a more simple way to add adorners, directly through the "@" to the function automatically add adorners, to achieve the purpose of extending the function, such as:
def decorator (F):
def NEWF (age):
print ' You Are calling ', f.__name__
F (age) return
NEWF
@ Decorator
#通过 @ add adorners to function showage decorator
def showage (age):
print "Hello, I am%d years old"%age
showage (10)
The purpose of decoration mode is to solve the problem of dynamic expansion function, the essence of decorative mode is to deal with the object flexibly, to understand the decoration mode, not only to understand the object-oriented programming, but also to improve the thinking ability of programming.