Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Detailed introduction of C language decorator pattern

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "C language decorator mode detailed introduction", in daily operation, I believe many people in the C language decorator mode detailed introduction problems there are doubts, Xiaobian consulted all kinds of information, sorting out simple and easy to use operation methods, I hope to answer "C language decorator mode detailed introduction" doubts helpful! Next, please follow the small series to learn together!

Decorator pattern, also known as wrapper pattern, is an alternative to inheritance by extending the functionality of objects in a transparent manner. Decorator pattern is similar to proxy pattern, but differs in the degree of control over the decorated object; decorator pattern is about enhancing the functionality of the object, while proxy pattern exerts control over the object and does not provide enhancement of the functionality of the object itself.

First look at the class diagram for the decorator pattern

说明下:

Component,给出一个抽象接口,规范实现类的一些方法;

ConcreteComponent:具体的一个组件,实现了接口方法;

Decorator:抽象的装饰者,对接口的一个引用,在 method 方法里面使用这个引用完成任务;(代理模式需要实例化)

ConcreteDecorator:具体的装饰者,对抽象装饰者的抽象部分进行实现

下面给出实现代码

抽象组件接口:

public interface Component { public void method();}

具体组件的实现代码:

public class ConcreteComponent implements Component { @Override public void method() { System.out.println("我是一个具体干活的类"); }}

抽象的装饰者:

public abstract class Decorator implements Component { private Component component; public Decorator(Component component) { this.component=component; } @Override public void method() { first(); component.method(); end(); } public abstract void first(); public abstract void end();}

具体的装饰者:

public class ConcreteDecorator extends Decorator{ public ConcreteDecorator(Component component) { super(component); } @Override public void first() { System.out.println("开始需求调研"); } @Override public void end() { System.out.println("项目收尾工作"); }}

最后测试下:

public class Client { public static void main(String[] args) { Component c=new ConcreteComponent(); Component decorator = new ConcreteDecorator(c); decorator.method(); }}最终结果是:开始需求调研我是一个具体干活的类项目收尾工作

如果有多个具体装饰者,可以互相嵌套包裹,如下,新增一个具体装饰者

public class ConcreteDecorator2 extends Decorator{ public ConcreteDecorator2(Component component) { super(component); } @Override public void first() { System.out.println("开始设计"); } @Override public void end() { System.out.println("项目确认验收"); }}

在客户端修改为:

public class Client { public static void main(String[] args) { Component c=new ConcreteComponent(); Component decorator = new ConcreteDecorator(c); Component decorator2 = new ConcreteDecorator2(decorator); decorator2.method(); }}最终结果:(顺序貌似反了,包装包反了)开始设计开始需求调研我是一个具体干活的类项目收尾工作项目确认验收

这样来看包裹模式更加形象真实了。

装饰者模式可以演变一种更加直观的,就是没有 Component 抽象组件,抽象装饰者直接继承某个类,对这个类进行装饰。

还可以把抽象装饰与具体装饰合二为一,对功能简单的可以这样优化处理。

另外:透明是指对抽象进行编程,比如上面的Component c=new ConcreteComponent();``Component decorator = new ConcreteDecorator(c);这些前端 Component,而不是具体的 ConcreteComponent 和ConcreteDecorator。

到此,关于"C语言装饰者模式的详细介绍"的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注网站,小编会继续努力为大家带来更多实用的文章!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report