Preface: [ Mode Overview ] ---------- by xingoo
Intention mode
This mode is not much use, but ideology is indeed very common. It is to separate the abstract part and implementation part.
Realization weak association that create dependency at runtime.
Decouple code.
Mode structure
Abstraction portion abstract base class, an abstract definition of the underlying content portion.
Abstract RefinedAbstraction expansion portion, the base class for supplementary content, add a specific operational scenarios.
Implementor realize the base part, the definition of basic content portion.
ConcreteImplementor a specific category.
Scenarios
1 does not want a fixed relationship between the binding and the abstract from its implementation
2 wants to achieve some abstract part and the extensions of certain generated through subclassing
Three pairs of abstract achieve a modification does not affect the part of the customer
Code structure
1 package com.xingoo.test;
2 /**
3 * 抽象类基类
4 * @author xingoo
5 */
6 abstract class Abstraction{
7 abstract public void operation(Implementor imp);
8 }
9 /**
10 * 实现类 基类
11 * @author xingoo
12 */
13 abstract class Implementor{
14 abstract public void operation();
15 }
16 /**
17 * 重新定义的抽象类
18 * @author xingoo
19 */
20 class RefinedAbstraction extends Abstraction{
21 public void operation(Implementor imp){
22 imp.operation();
23 System.out.println("RefinedAbstraction");
24 }
25 }
26 /**
27 * 具体的实现类
28 * @author xingoo
29 */
30 class ConcreteImplementorA extends Implementor{
31 public void operation() {
32 System.out.println("ConcreteImplementorA");
33 }
34 }
35 /**
36 * 具体的实现类
37 * @author xingoo
38 */
39 class ConcreteImplementorB extends Implementor{
40 public void operation() {
41 System.out.println("ConcreteImplementorB");
42 }
43 }
44 public class test {
45 public static void main(String[] args){
46 RefinedAbstraction abstraction = new RefinedAbstraction();
47 abstraction.operation(new ConcreteImplementorA());
48
49 abstraction.operation(new ConcreteImplementorB());
50 }
51 }
52
operation result
ConcreteImplementorA
RefinedAbstraction
ConcreteImplementorB
RefinedAbstraction
Reproduced in: https: //my.oschina.net/u/204616/blog/544971