23 design patterns (two) - Single Responsibility mode

Single Responsibility mode

Decorator: the decorator pattern

Typically, a class extend functionality will be implemented using inheritance.
Inherited but having static features, high coupling, and with the increase of the extension, it expands subclasses.
At this point you can create a wrapper object (ie, decorative objects) to wrap the real object using a combination of relationship, and in keeping the real object of the class structure of the same premise, to provide additional functionality.

GoF Decorator pattern Definition:
Dynamic (combination) to an object to add some additional responsibilities.
Functionally increased, Decorator pattern is more flexible than a subclass (inherited) (elimination of duplicate code subclass & reduction in the number).

eg1:
/**
 * @author lillcol
 * 2019/6/15-23:56
 */
public abstract class Component { //基础接口
    abstract void operation();
}

class ConcreteComponent1 extends  Component{//具体实现类1

    @Override
    void operation() {
        System.out.println("from ConcreteComponent1 operation");
    }
}

class ConcreteComponent2 extends  Component{//具体实现类2

    @Override
    void operation() {
        System.out.println("from ConcreteComponent2 operation");
    }
}
class Decorator extends Component{ //装饰类
    Component component ;
    public Decorator( Component component){
        this.component=component;
    }
    @Override
    void operation() {
        component.operation();
    }
}

class ConcreteDecoratorA extends  Decorator{//具体装饰类A
    public ConcreteDecoratorA(Component component) {
        super(component);
    }

    public  void operation(){
        System.out.println("ConcreteDecoratorA operation");
        this.component.operation();
    }

}


class ConcreteDecoratorB extends  Decorator{//具体装饰类B
    public ConcreteDecoratorB(Component component) {
        super(component);
    }

    public  void operation(){
        System.out.println("ConcreteDecoratorB operation stag1");
        this.component.operation();
        System.out.println("ConcreteDecoratorB operation stag2" );
    }

}

class DecoratorTest{ //测试
    public static void main(String[] args) {
        ConcreteComponent1 concreteComponent1 = new ConcreteComponent1();
        ConcreteDecoratorA concreteDecoratorA = new ConcreteDecoratorA(concreteComponent1);
        concreteDecoratorA.operation();
        ConcreteDecoratorB concreteDecoratorB = new ConcreteDecoratorB(concreteComponent1);
        concreteDecoratorB.operation();

        System.out.println("------------------------------------------------------------------");

        ConcreteComponent2 concreteComponent2 = new ConcreteComponent2();
        ConcreteDecoratorA concreteDecoratorA2 = new ConcreteDecoratorA(concreteComponent2);
        concreteDecoratorA2.operation();
        ConcreteDecoratorB concreteDecoratorB2 = new ConcreteDecoratorB(concreteComponent2);
        concreteDecoratorB2.operation();

    }
}
//输出结果:
ConcreteDecoratorA operation
from ConcreteComponent1 operation
ConcreteDecoratorB operation stag1
from ConcreteComponent1 operation
ConcreteDecoratorB operation stag2
------------------------------------------------------------------
ConcreteDecoratorA operation
from ConcreteComponent2 operation
ConcreteDecoratorB operation stag1
from ConcreteComponent2 operation
ConcreteDecoratorB operation stag2

Decorator class diagram:
FIG Decorator class .png

Decorative pattern mainly includes the following roles:

  1. Abstract component (Component) Roles: define an abstract interface to regulate an object ready to receive additional responsibilities.
  2. Specific components (Concrete Component) role: to achieve abstract components, add some of its responsibilities by decorative role.
  3. Abstract decorative (the Decorator) Role: inherit the abstract member, and includes examples of specific member, the specific member function can be extended by its subclasses.
  4. Specific decoration (ConcreteDecorator) role: decorative abstract method implementation-dependent and subject to specific components or add extensions == == responsibility.

Decorator decorative value lies, he does not affect the class itself is decorated core functionality. In a system of inheritance, the subclass usually mutually exclusive.

  • Bridge: Bridge mode

Schema definition: an abstraction (business function) and implement part (platform) separated, so that they can be independently varied.

Some classes have two or more dimensions of change, support different platforms and media players of different file formats.
How to design playback software, Media Player can play different platforms and different file formats it?
If inheritance, internet and a combination of m kinds of file formats have n m × n kinds, not only many corresponding subclass, and is hard to expand.

Bridge class diagram:
Bridge class diagram .png

Bridge (Bridge) mode contains the following main roles:
abstraction (Abstraction) roles : the definition of abstract class and contains a reference to the realization of the object.
Extended abstract (RefinedAbstraction) angle color: the role of abstract subclasses to achieve business method parent class, and by a combination of methods to achieve business relationships calling roles.
Realization of (Implementor) roles : defining the role of the realization of the interface for extended abstract role call.
Concrete realization of (ConcreteImplementor) role : to achieve given the role of a specific implementation of the interface.

Case Code:

eg1
/**
 * @author lillcol
 * 2019/6/16-17:17
 */
public class Bridge {
    public static void main(String[] args) {
        ConcreteImplementorA1 concreteImplementorA1 = new ConcreteImplementorA1();
        ConcreteImplementorB2 concreteImplementorB2 = new ConcreteImplementorB2();
        RefinedAbstraction refinedAbstraction = new RefinedAbstraction(concreteImplementorA1, concreteImplementorB2);
        refinedAbstraction.operation();
    }
}

abstract class ImplementorA{ //定义实现接口:平台
    abstract void implementA();
}
class ConcreteImplementorA1 extends ImplementorA{//定义实现类:平台 P1

    @Override
    void implementA() {
        System.out.println("采用平台: ConcreteImplementorA1 P1");
    }
}

class ConcreteImplementorA2 extends ImplementorA{//定义实现类:平台 P2

    @Override
    void implementA() {
        System.out.println("采用平台: ConcreteImplementorA2 P2");
    }
}

abstract class ImplementorB{ //定义实现接口:格式
    abstract void implementB();
}

class ConcreteImplementorB1 extends  ImplementorB{//定义实现类:格式 F1

    @Override
    void implementB() {
        System.out.println("采用格式: ConcreteImplementorB1 F1");
    }
}

class ConcreteImplementorB2 extends  ImplementorB{//定义实现类:格式 F2

    @Override
    void implementB() {
        System.out.println("采用格式: ConcreteImplementorB2 F2");
    }
}

abstract class Abstraction{
    ImplementorA implementorA;
    ImplementorB implementorB;
    Abstraction(ImplementorA implementorA,ImplementorB implementorB){
        this.implementorA=implementorA;
        this.implementorB=implementorB;
    };
    abstract void operation();
}

class RefinedAbstraction extends  Abstraction{

    RefinedAbstraction(ImplementorA implementorA, ImplementorB implementorB) {
        super(implementorA, implementorB);
    }

    @Override
    void operation() {
        implementorA.implementA();
        implementorB.implementB();
    }
}
//输出结果:
采用平台: ConcreteImplementorA1 P1
采用格式: ConcreteImplementorB2 F2

Advantages bridge (Bridge) mode are:
due to the abstraction and separation, the expansion capability;
implementation details transparent to the client.

The disadvantage is:
due to the aggregation relationship built on abstraction layer, requires developers to design and programming for the abstraction, which increases the difficulty of understanding the design of the system.

Reference document:
http://c.biancheng.net/view/1364.html
Li Jianzhong 23 design patterns

Guess you like

Origin www.cnblogs.com/lillcol/p/11129937.html