What is an adorner pattern
Adorner mode, which dynamically attaches responsibility to an object, and to extend functionality, the adorner provides a more flexible alternative than inheritance. Give an example to explain
We define a drink, it has descriptive information, and there are prices, as follows
Public abstract class Drink {
String desc = ' Nothing ';
Public String GetDesc () {return
desc;
}
public abstract double ();
}
Then the definition of coffee, is a drink, it has its own description and price
public class Coffee extends Drink {public
Coffee () {
This.desc = "Coffee";
}
public double () {return
1.2;
}
}
Coffee can have different tastes, and the different taste of the extra price is different, you can use the adorner method to achieve the taste of the beverage decoration, as follows:
public class Milk extends Drink {
private Drink Drink;
Public Milk (Drink water) {
this.drink = water;
}
public double () {return
drink.cost () + 1;
}
@Override public
String GetDesc () {return
Drink.getdesc () + ", Milk";
}
}
Below we create a test class to demonstrate how to animate the decorations. As follows:
Class Test {public
static void Main (string[] args) {
Drink Drink = new Coffee ();
System.out.println (Drink.getdesc () + ", cost" + drink.cost ());
Drink = new Milk (drink);
System.out.println (Drink.getdesc () + ", cost" + drink.cost ());
Drink = new Milk (drink);
System.out.println (Drink.getdesc () + ", cost" + drink.cost ());
}
The output results are:
Coffee, 1.2
Coffee, Milk, cost 2.2
Coffee, Milk, Milk, cost 3.2
Adapter Mode
Converts the interface of a class into another interface that the customer expects. Adapters allow classes that are incompatible with the original interface to work seamlessly.
For example, we ducks, geese, they have their own method of call and flight methods are as follows:
Public interface Duck {
void quack ();
void Fly ();
}
public class Akindduck implements Duck {public
void quack () {
System.out.println ("A kind Duck");
} Public
void Fly () {
System.out.println ("A kind Duck Fly");
}
Goose class
Public interface Turkey {
void gobble ();
void Fly ();
}
public class Wildturkey implements Turkey {public
void gobble () {
System.out.println ("Wild Gobble");
} Public
void Fly () {
System.out.println ("Wild Run");
}
In order for the geese to fit into the ducks, we made an adaptor
public class Turkeyadapter implements Duck {
private Turkey Turkey;
Public Turkeyadapter (Turkey Turkey) {
this.turkey = Turkey;
}
public void Quack () {
turkey.gobble ();
}
public void Fly () {
turkey.fly ();
Turkey.fly ();
}
We create a test class to illustrate the results of the fit:
Class Test {public
static void Main (string[] args) {
Duck Duck = new Akindduck ();
Turkey Turkey = new Wildturkey ();
Duck duckadapter = new Turkeyadapter (Turkey);
Duck.quack ();
Duck.fly ();
System.out.println ();
Turkey.gobble ();
Turkey.fly ();
System.out.println ();
Duckadapter.quack ();
Duckadapter.fly ();
}
The output results are:
A kind duck quack
a kind duck fly wild gobble wild
run wild gobble wild
run
Wild Run
appearance mode
Provides a unified interface for accessing a group of interfaces in a subsystem. The appearance defines a high-level interface that makes the subsystem easier to use.
For example, watching a movie takes a lot of steps, and we can encapsulate this many steps in a socket called Watchmovie () for the outside world. The end of the movie package is provided to the outside world by Endmovie (). As shown below:
public class Movie {public
void Watchmovie () {
Actiona ();
Actionc ();
}
public void Endmovie () {
actionb ();
Actiond ();
}
private void Actiona () {} private void Actionb () {} private void Actionc () {}
private void
Actiond () {}
}