1. Definition of Decoration Patterns and Use Scenarios
Definition:
Decoration pattern, also known as packaging pattern, is one of the structural design patterns. It uses a transparent way to dynamically expand the functions of objects, and it is also one of the alternatives to inheritance relations. Adding additional responsibilities to an object dynamically. Decoration patterns are more flexible in terms of adding functionality than generating subclasses
Use scenarios:
- You need to extend the functionality of a class, or add additional functionality to a class
- You need to add functionality to an object dynamically, which can be revoked dynamically again
- It is necessary to modify or install functions for a group of brothers, of course, the preferred decoration mode.
2. Advantages and disadvantages of decorative patterns
2.1 advantages
- Decorative and decorative classes can develop independently without coupling each other. In other words, the Component class does not need to know the Decorator class, which extends the functionality of the Component class from the outside, and the Decorator does not need to know the specific components.
- Decoration pattern is an alternative to inheritance relationship. Let's look at the decorative Decorator class, no matter how many layers are decorated, the returned object is still component, and the implemented relationship is-a.
- Decoration patterns can dynamically extend the functionality of an implementation class
2.2 disadvantages
For decoration mode, multi-layer decoration is more complex. To minimize the number of decorative classes in order to reduce the complexity of the system.
3. Realization of Decoration Mode
public abstract class Component {
public abstract void operator();
}
public class ConcreteComponent extends Component {
@Override
public void operator() {
System.out.println("ConcreteComponent operator");
}
}
public class Decorator extends Component {
private Component component;
Decorator(Component component) {
this.component = component;
}
@Override
public void operator() {
if (component != null) {
component.operator();
}
}
}
public class ConcreteDecorator1 extends Decorator {
public ConcreteDecorator1(Component component) {
super(component);
}
private void method1() {
System.out.println("method1 Modification");
}
@Override
public void operator() {
super.operator();
method1();
}
}
public class ConcreteDecorator2 extends Decorator {
public ConcreteDecorator2(Component component) {
super(component);
}
private void method2() {
System.out.println("method2 Modification");
}
@Override
public void operator() {
super.operator();
method2();
}
}
public class Test {
public static void main(String args[]) {
Component component = new ConcreteComponent();
ConcreteDecorator1 decorator1 = new ConcreteDecorator1(component);
ConcreteDecorator2 decorator2 = new ConcreteDecorator2(component);
decorator1.operator();
decorator2.operator();
}
}
4. Application of Decoration Mode in Android
The Context class itself is a pure abstract class. It has two specific implementation subclasses: ContextImpl and ContextWrapper. The ContextWrapper class is just a wrapper. The ContextWrapper constructor must contain a real Context reference, and ContextWrapper provides attachBaseContext () to specify the real Context object in the ContextWrapper object. Calling the ContextWrapper method will be directed to the real Context object it contains.
ContextThemeWrapper class, which contains the interface related to Theme, refers to the topic specified in Android Mainifest. XML for the Application element or Activity element through android: theme. Of course, only Activity needs themes. Service does not need themes. Application is the same.
The ContextImpl class actually implements all the functions in Context, and the methods of various Context classes invoked in the application program come from this class. Context has two distinct subclasses: ContextImpl is the concrete implementation class of Context and ContextWrapper is the packaging class of Context.