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

A case study of Java decorator pattern

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

Today, the editor will share with you the relevant knowledge points of Java decorator pattern instance analysis, the content is detailed, the logic is clear, I believe most people still know too much about this knowledge, so share this article for your reference, I hope you can get something after reading this article, let's take a look at it.

Advantages

1. The decoration class and the decorated class can develop independently and will not be coupled with each other. The decoration mode is an inherited alternative mode, and the decoration pattern can dynamically expand the function of an implementation class.

Shortcoming

two。 Multi-layer decoration is more complicated.

Working with scen

1. Extend the functionality of a class.

two。 Dynamic add function, dynamic undo.

I. the way of realization

Suppose a scene, our room to get up every day to brush teeth, sleep to brush teeth, brushing teeth is the role of a decorator, which is more conducive to our oral health. Next, let's look at how the specific decorator is implemented.

1. The interface of daily life

Package com.asurplus.common.decorator;/** * Daily Life Interface * / public interface Live {/ * what needs to be done every day * / void live ();}

2. The implementation class for getting up

Package com.asurplus.common.decorator;import lombok.extern.slf4j.Slf4j;/** * get up to implement class * / @ Slf4jpublic class GetUpLive implements Live {@ Override public void live () {log.info ("get up");}}

3. The implementation class of sleeping

Package com.asurplus.common.decorator;import lombok.extern.slf4j.Slf4j;/** * sleep implementation class * / @ Slf4jpublic class SleepLive implements Live {@ Override public void live () {log.info ("sleep");}}

4. Decorator class

Package com.asurplus.common.decorator;import lombok.extern.slf4j.Slf4j;/** * decorator class * / @ Slf4jpublic class LiveDecorator {private Live live; public LiveDecorator (Live live) {this.live = live;} public void live () {/ / Life live.live (); / / brushTeeth () } private void brushTeeth () {log.info ("brush your teeth");}} II. Test package com.asurplus.common.decorator;/** * decorator mode * / public class TestMain {public static void main (String [] args) {/ / get up LiveDecorator getUp = new LiveDecorator (new GetUpLive ()); getUp.live (); System.out.println () / / sleep LiveDecorator sleep = new LiveDecorator (new SleepLive ()); sleep.live ();}}

Output result

It can be seen that we get up and go to bed, brushing our teeth, thus realizing our decorator mode.

The above is all the content of this article "Java decorator pattern example Analysis". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.

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

Development

Wechat

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

12
Report