Aspectj implementation design mode (4) - Abstract factory model

zhaozj2021-02-16  48

This article describes the use of AspectJ to implement the design mode, the article describes the reference implementation of the abstract plant mode aspectJ version in the form of IT product production.

Abstract factory model is an object's creation mode, which is a further promotion of factory methods. It provides customers with an interface that makes customers do not have to create product objects in multiple product families with the type of product type. The simple UML legend of the pattern is as follows

Now, the factory's creation method is handled by a specific abstract factory, so that the specific factory class does not need to know which abstract factory needs to implement, nor does it take to understand the product creation logic. The UML map of the example system of this article is shown below

Abstract factory use Inter-Type declares that the factory class declares an abstract factory interface that needs to be implemented and invokes according to the captured creative method, implementing specific creation logic.

AbstractFactoryASpect.java

Public aspect abstractFactoryAspect {

Public CPU ComputerProducer.createCPU () {Return Null;} public ram computerproducer.createram () {Return NULL;}

Declare PARENTS: MacProducer Implements ComputerProducer; Declare Parents: PcProducer Implements ComputerProducer

// capture the creation of the CPU product family

CPU Around (COMPUCER): Target && Call (CPU ComputerProducer .createcpu ()) {Return ChoosecpuProducer (Product);

// Capture the creation of the RAM product family

Ram Around (ComputerProducer Product): Target (Product) && Call (Ram ComputerProducer .createram ()) {Return ChooseraMproducer (Product);

// Select different factory classes

private Cpu chooseCpuProducer (ComputerProducer producer) {if (producer instanceof MacProducer) {return new MacCpu ();} else if (producer instanceof PcProducer) {return new PcCpu ();} else {throw new RuntimeException ( "No such kind of producer" }}

private Ram chooseRamProducer (ComputerProducer producer) {if (producer instanceof MacProducer) {return new MacRam ();} else if (producer instanceof PcProducer) {return new PcRam ();} else {throw new RuntimeException ( "No such kind of producer" Abstract factories and specific factories are very simple

Public interface computerproducer {}

Public class pcproducer {}

Public class macProducer {}

This is not easy to understand, and there is no method.

The same product class code is only an explanation without any functions. Public interface CPU {}

Public interface ram {}

Public class pccpu {}

Public Class Maccpu {}

Public class pcRam {}

Public class macram {}

Test code Demo.java

public class Demo {public static void main (String [] args) {ComputerProducer producer = new PcProducer (); System.out.println (. (producer.createCpu ()) toString ()); System.out.println ((producer .CreateRam ()). Tostring ()); product = new macproducer (); system.out.println ((producer.createcpu ()). Tostring ()); system.createram ()) .tostring ());

}

}

The result of the output is as follows

AOPABSTRACTFACTORY.PCCPU

AOPABSTRACTFACTORY.PCRAM

AOPABSTRACTFACTORY.MACCPU

AOPABSTRACTFACTORY.MACRAM

At this point, I have already used aspectj to simply implement an example of IT product creation using abstract factory model. This series will introduce how to use AspectJ to implement iterative sub-mode.

Disclaimer This article is reserved by Starchu1981, if you need to pay, please write the author and source.

转载请注明原文地址:https://www.9cbs.com/read-26880.html

New Post(0)