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

What are the commonly used architectural patterns

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

This article mainly introduces "what are the commonly used architecture patterns". In daily operation, I believe many people have doubts about the commonly used architecture patterns. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful for you to answer the questions of "what are the commonly used architecture patterns?" Next, please follow the editor to study!

Hierarchical architecture

Hierarchical architecture model

Hierarchical architecture model is often used in the work, such as MVC, which divides the responsibilities into a certain layer through layering, with a clear hierarchy and a clear structure.

Let's take MVC as an example: controller-> service-> dao

@ RestController @ RequestMapping ("/ order") public class OrderController {@ Autowired private OrderService orderService; / * New order * @ param order * @ return * / @ PostMapping ("/ add") public Response addOrder (Order order) {orderService.add (order); return Response.success () }} public interface OrderService {/ * add order * @ param order * @ return * / boolean add (Order order);} public interface OrderRepository {int save (Order order);}

According to the direction of dependency, the upper layer depends on the lower layer in turn, and each layer handles differently to logic.

Previous articles have discussed changing dependencies through dependency reversal, resulting in less coupling.

Pipeline architecture

Pipeline architecture model

Pipeline architecture, also known as pipeline or pipeline architecture, the processing process is linear, each link has corresponding to the component processing, from front to back sequential execution.

Concept note:

Source: data source, usually using stream data as the source, such as KafkaSource

Channel: a channel or pipeline for processing or converting data, such as JsonChannel

Sink: data landing, usually used for data storage or forwarding, such as DbSink, KafkaSink

Component: component, the smallest unit used to execute logic, source,channel,sink is a Component

Pipeline: pipeline or pipeline. A Pipeline consists of the components above, and different businesses can be assembled into different Pipeline.

Code implementation: digital data source-> accumulate-> convert to string-> landing

/ * component * / public interface Component {/ * * component name * @ return * / String getName (); / * * get downstream component * @ return * / Collection getDownStrems (); / * * component execution * / void execute (to) } public abstract class AbstractComponent implements Component {@ Override public void execute (to) {/ / the current component executes R r = doExecute (o); System.out.println (getName () + "receive" + o + "return" + r); / / gets the downstream component and executes Collection downStreams = getDownStrems () If (! CollectionUtils.isEmpty (downStreams)) {downStreams.forEach (c-> c.execute (r));}} protected abstract R doExecute (To) } / * data source * / public abstract class Source extends AbstractComponent {} / * pipeline / channel * @ param * / public abstract class Channel extends AbstractComponent {} / * data landing * @ param * / public abstract class Sink extends AbstractComponent {} public class IntegerSource extends Source {@ Override protected Integer doExecute (Integer o) {return o } @ Override public String getName () {return "Integer-Source";} @ Override public Collection getDownStrems () {return Collections.singletonList (new IncrChannel ());}} public class IncrChannel extends Channel {@ Override protected Integer doExecute (Integer o) {return o + 1;} @ Override public String getName () {return "Incr-Channel" } @ Override public Collection getDownStrems () {return Collections.singletonList (new StringChannel ());}} public class StringChannel extends Channel {@ Override protected String doExecute (Integer o) {return "str" + o;} @ Override public String getName () {return "String-Channel" } @ Override public Collection getDownStrems () {return Collections.singletonList (new StringSink ());}} public class StringSink extends Sink {@ Override protected Void doExecute (String o) {return null;} @ Override public String getName () {return "String-Sink";} @ Override public Collection getDownStrems () {return null }} / * pipeline * / public class Pipeline {/ * data source * / private Source source; public Pipeline (Source source) {this.source = source;} / * launch * / public void start () {source.execute (1);}}

Test:

Public class PipelineTest {@ Test public void test () {Pipeline pipeline = new Pipeline (new IntegerSource ()); pipeline.start ();}}

Execution result:

Integer-Source receive 1 return 1 Incr-Channel receive 1 return 2 String-Channel receive 2 return str2 String-Sink receive str2 return null

Event-driven architecture

Event-driven mode

Event-driven is based on a specific event as the trigger condition, thus throughout the process. Usually, event-driven belongs to publish-subscribe pattern or observer pattern, which is used for asynchronous processing and decoupling business logic. The specific implementation has in-process and distributed ways, such as: EventBus, MQ and so on.

Code example:

Public class OrderEventListener implements Listener {@ Override public void onEvent (OrderEvent event) {System.out.println ("receive event:" + event);}} public class EventBus {private final static List listeners = new ArrayList (); / * * registered listener * @ param listener * / public static void registerListener (Listener listener) {listeners.add (listener) } / * publish event * @ param event * / public void publishEvent (Event event) {/ / receive and process event listeners.forEach (l-> {l.onEvent (event);});}}

Test:

Public class EventBusTest {@ Test public void publish () {OrderEvent event = new OrderEvent ("order_2", OrderState.PENDING_PAYMENT); EventBus.registerListener (new OrderEventListener ()); EventBus eventBus = new EventBus (); eventBus.publishEvent (event);}}

Event publishing and listening are also available in Spring (Spring/SpringBoot event listening mechanism is discussed in detail):

@ Component public class OrderEventListener {@ Async @ EventListener (OrderEvent.class) public void onEvent (OrderEvent event) {System.out.println ("receive event:" + event);}} public class EventTest {@ Autowired private ApplicationContext context; @ Test public void publishEvent () {OrderEvent event = new OrderEvent ("order_1", OrderState.PENDING_PAYMENT); context.publishEvent (event) }} at this point, the study of "what are the common architectural patterns" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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