preface
In the previous article we learned about the structural pattern of the adapter pattern and the bridge pattern. This article will look at the appearance and decorator patterns of structural patterns.
The appearance model
Introduction to the
Facade mode hides the complexity of the system and provides an interface through which the client can access the system. This type of design pattern is structural in that it adds an interface to an existing system to hide the complexity of the system.
Simply put, this is to provide a simple interface to the outside world and hide the implementation logic. For example, the power button of the common computer, we just press the power button, we can let it start or close, without knowing how it is started (start CPU, start memory, start hard disk), how to close (close hard disk, close memory, close CPU);
Again, we can use the computer game as an example to illustrate the appearance model. There are some online games on the computer, DNF, LOL and WOW, we just need to double click the icon on the computer to start and play the game, no matter how the game starts and works.
The steps to be implemented are as follows:
- Build interfaces to the game;
- Build LOL, DNF and WOW classes and implement the interface of the game;
- Define a facade class to provide for client calls.
- Invoke the appearance class.
Code examples:
interface Game{
void play();
}
class DNF implements Game{
@Override
public void play() {
System.out.println("Playing DNF...");
}
}
class LOL implements Game{
@Override
public void play() {
System.out.println("Playing LOL...");
}
}
class WOW implements Game{
@Override
public void play() {
System.out.println("Playing WOW...");
}
}
class Computer{
private Game dnf;
private Game lol;
private Game wow;
public Computer() {
dnf=new DNF();
lol=new LOL();
wow=new WOW();
}
public void playDNF(){
dnf.play();
}
public void playLOL(){
lol.play();
}
public void playWOW(){
wow.play();
}
}
public static void main(String[] args) {
Computer computer=new Computer();
computer.playDNF();
computer.playLOL();
computer.playWOW();
}
Copy the code
Running results:
Are playing DNF... Are playing LOL... Are playing WOW...Copy the code
In the code example above, when we want to play the game, we can simply instantiate the appearance class and call the game methods, regardless of how the game is started and run. And each game is independent of each other, so that one game doesn’t work and the other doesn’t work. In fact, the appearance mode and we usually use interface is very similar, are external interface, do not need to care about how to achieve.
Benefits of appearance mode:
Reduced coupling and, in some ways, improved security.
Disadvantages of appearance mode:
Inconsistent open and close principle, not easy to change.
Usage scenarios
When a system has multiple complex modules or subsystems.
Decorator mode
Introduction to the
The decorator pattern allows you to add new functionality to an existing object without changing its structure. This type of design pattern is a structural pattern that acts as a wrapper around existing classes.
Decorator mode, as the name suggests, is to decorate something so that it provides some additional function. Like decorating people, wearing different clothes for different things. For example, wearing a jersey is ready to play basketball, wearing a bathing suit is ready to swim and so on.
The decorator pattern can dynamically add additional responsibilities to an object.
Here again, we use an example to illustrate. Among current toy models, there are two popular models, GUNDAM model and MrGu model. When we splicing models, we usually put the models together first, and then add some additional accessories, such as weapons. Here we put together the GUNDAM and MrGu models and loaded them with their respective weapons.
The specific steps are as follows:
- Create an abstract component of the model interface, there is assembly this method;
- Create concrete component classes (GUNDAM class and MrGu class) and implement the above model interface;
- Define a decorator that accepts requests from clients and invokes them accordingly;
- Define a concrete implementation decorator class, used to add the corresponding function to the object.
Code examples:
interface Model{
void assemble();
}
class GUNDAM implements Model{
@Override
public void assemble() {
System.out.println("Assemble a Gundam model.");
}
}
class MrGu implements Model{
@Override
public void assemble() {
System.out.println("Assemble a Zagu model.");
}
}
abstract class AddExtra implements Model{
protected Model model;
public AddExtra(Model model){
this.model=model;
}
public void assemble(){
model.assemble();
}
}
class LightSaber extends AddExtra{
public LightSaber(Model model) {
super(model);
}
public void assemble(){
model.assemble();
addLightSaber();
}
public void addLightSaber(){
System.out.println("Add a lightsaber");
}
}
class RocketLauncher extends AddExtra{
public RocketLauncher(Model model) {
super(model);
}
public void assemble(){
model.assemble();
addRocketLauncher();
}
public void addRocketLauncher(){
System.out.println("Add rocket launcher");
}
}
public static void main(String[] args) {
Model gundam=new GUNDAM();
Model mrgu=new MrGu();
gundam.assemble();
mrgu.assemble();
Model gModel=new LightSaber(new GUNDAM());
gModel.assemble();
Model mModel=new RocketLauncher(new MrGu());
mModel.assemble();
}
Copy the code
Running results:
Assemble a Gundam Model Assemble a Zagu model Assemble a Gundam Model Add lightsaber Assemble a Zagu model Add bazookaCopy the code
In the above code, if we only wanted to assemble the gundam or Zagu model, we could simply instantiate the model class and call its methods. If you need to add a weapon while assembling the model, just add the corresponding functionality through the decorator class. Through this example, we found that when we tried using the decorator pattern, we could extend some classes without affecting previous functionality and increase flexibility.
Advantages of decorator mode:
The decorative class and the decorated class can be independently developed, low coupling degree, easy to expand, flexible and convenient.
Disadvantages of decorator mode:
Decorating a class too much can add complexity.
Use the scene prototype unchanged, dynamically add some functionality when.
other
Music to recommend
Original is not easy, if you feel good, I hope to give a recommendation! Your support is the biggest motivation for my writing! Copyright: www.cnblogs.com/xuwujing CSDN blog.csdn.net/qazwsxpcm Personal blog: www.panchengming.com