From e4b033cd9edc06908f04193df1879d41bde8722c Mon Sep 17 00:00:00 2001 From: "OWNEROR-41FNVD0\\lenovo" Date: Wed, 28 May 2025 17:22:35 +0800 Subject: [PATCH] =?UTF-8?q?feat=20(=E6=A0=87=E8=AF=86):=20=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20AppAspectProgrammingDesign=20=E4=BB=93=E5=BA=93?= =?UTF-8?q?=E6=A0=87=E8=AF=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../entry/src/main/ets/components/child2.ts | 6 ++- .../main/ets/entryability/EntryAbility.ets | 51 ++++--------------- .../entry/src/main/ets/pages/Index.ets | 6 +-- .../entry/src/main/ets/pages/Index1.ets | 3 +- .../entry/src/main/ets/pages/Index2.ets | 6 +-- .../entry/src/main/ets/pages/Index7.ets | 5 +- .../entry/src/main/ets/pages/index10.md | 34 +++++++++++++ .../entry/src/main/ets/pages/index8.ets | 12 +++++ .../entry/src/main/ets/pages/index9.ets | 13 +++++ 9 files changed, 85 insertions(+), 51 deletions(-) create mode 100644 AppAspectProgrammingDesign/entry/src/main/ets/pages/index10.md create mode 100644 AppAspectProgrammingDesign/entry/src/main/ets/pages/index8.ets create mode 100644 AppAspectProgrammingDesign/entry/src/main/ets/pages/index9.ets diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/components/child2.ts b/AppAspectProgrammingDesign/entry/src/main/ets/components/child2.ts index ee7580be..b17a3ff3 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/components/child2.ts +++ b/AppAspectProgrammingDesign/entry/src/main/ets/components/child2.ts @@ -17,7 +17,9 @@ * 最佳实践: 应用切面编程设计 */ // [Start child2] -// child2 +// child.ets import {Base} from './base'; -export class Child2 extends Base {} +export class Child extends Base { + // Inherit the getCurrentLocation method of the parent class +} // [End child2] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/entryability/EntryAbility.ets b/AppAspectProgrammingDesign/entry/src/main/ets/entryability/EntryAbility.ets index feaa7b9e..54de9871 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/entryability/EntryAbility.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/entryability/EntryAbility.ets @@ -15,53 +15,24 @@ /* * 最佳实践: 应用切面编程设计 -*/import { AbilityConstant, ConfigurationConstant, UIAbility, Want } from '@kit.AbilityKit'; +*/ + +// [Start entry_ability] +// EntryAbility.ets +import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; -import { window } from '@kit.ArkUI'; import { util } from '@kit.ArkTS'; - -const DOMAIN = 0x0000; - +// Obtain the target package name export default class EntryAbility extends UIAbility { onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void { - this.context.getApplicationContext().setColorMode(ConfigurationConstant.ColorMode.COLOR_MODE_NOT_SET); - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onCreate'); + hilog.info(0x0000, 'testTag', '%{public}s', ' onCreate'); util.Aspect.addBefore(this.context.constructor, 'startAbility', false, (instance: Object, wantParam: Want) => { console.info('UIAbilityContext startAbility: want.bundleName is ' + wantParam.bundleName); }); this.context.startAbility(want, () => {}) } - - onDestroy(): void { - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onDestroy'); - } - - onWindowStageCreate(windowStage: window.WindowStage): void { - // Main window is created, set main page for this ability - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageCreate'); - - windowStage.loadContent('pages/Index', (err) => { - if (err.code) { - hilog.error(DOMAIN, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err)); - return; - } - hilog.info(DOMAIN, 'testTag', 'Succeeded in loading the content.'); - }); - } - - onWindowStageDestroy(): void { - // Main window is destroyed, release UI related resources - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onWindowStageDestroy'); - } - - onForeground(): void { - // Ability has brought to foreground - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onForeground'); - } - - onBackground(): void { - // Ability has back to background - hilog.info(DOMAIN, 'testTag', '%{public}s', 'Ability onBackground'); - } -} \ No newline at end of file + // Other related configurations + // ... +} +// [End entry_ability] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index.ets index 76116df9..3c8c0dc7 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index.ets @@ -25,12 +25,12 @@ import { A } from '../components/baseAbility'; @Component struct Index { build() { - // UI代码 + // UI code } } util.Aspect.addBefore(A, 'getElementByIndex', false, - // 参数校验 + // Check the parameters (instance: A, arr: Object, idx: number) => { if (!(arr instanceof Array)) { throw Error('arg arr is expected to be an array'); @@ -42,7 +42,7 @@ util.Aspect.addBefore(A, 'getElementByIndex', false, throw Error('arg idx is expected to be smaller than arr.length'); } }); -// 原方法执行 +// The original method is executed let buffer: Array = [1, 2, 3, 5]; let that = new A(); that.getElementByIndex(buffer, -1); diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index1.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index1.ets index 4759e21c..0e8e53b9 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index1.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index1.ets @@ -19,7 +19,8 @@ */ // [Start index1] -import {util} from '@kit.ArkTS'; +// index.ets +import { util } from '@kit.ArkTS'; import { Test } from '../components/somePackage'; @Entry diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index2.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index2.ets index 6d99446d..6ea5fea9 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index2.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index2.ets @@ -29,7 +29,7 @@ struct Index { // UI code } } -// 插入执行前后打印时间, 将插入动作封装成一个接口 +// Print the time before and after the insertion, and encapsulate the insertion action into an interface function addTimePrinter(targetClass: Object, methodName: string, isStatic: boolean) { let t1 = 0; let t2 = 0; @@ -41,10 +41,10 @@ function addTimePrinter(targetClass: Object, methodName: string, isStatic: boole console.log("t2---t1 = " + (t2 - t1).toString()); }); } -// 给Test的doSomething实例方法添加打印执行时间的逻辑 +// Add the logic for printing the execution time to the doSomething instance method of Test addTimePrinter(Test1, 'doSomething', false); new Test1().doSomething() -// 给Test的test静态方法添加打印执行时间的逻辑 +// Add the logic for printing the execution time to the test static method of the test addTimePrinter(Test1, 'test', true); Test1.test() // [End index2] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index7.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index7.ets index 02e2ed77..b68484ef 100644 --- a/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index7.ets +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/Index7.ets @@ -18,8 +18,9 @@ */ // [Start index7] -import {util} from '@kit.ArkTS'; -import {geoLocationManager} from "@kit.LocationKit"; +// index.ets +import { util } from '@kit.ArkTS'; +import { geoLocationManager } from "@kit.LocationKit"; import { Child } from '../components/child'; @Entry diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/index10.md b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index10.md new file mode 100644 index 00000000..881a7255 --- /dev/null +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index10.md @@ -0,0 +1,34 @@ +// [Start index10] +@Component +struct Index { + foo(){} + build(){}; +} + +util.Aspect.replace(Index, 'foo', false, ...); +util.Aspect.replace(Index, 'build', false, ...); +// [End index10] + +// [Start index11] +// Example of unrecommended usage: +// 'somePackage'; +class Test { + foo(): string { + return 'hello'; + } +} +util.Aspect.addAfter(Test, 'foo', false, () => { + console.log('execute foo'); +}); + +// The correct usage example is as follows: +class Test { + foo(): string { + return 'hello'; + } +} +util.Aspect.addAfter(Test, 'foo', false, (instance: Test, ret: string) => { + console.log('execute foo'); + return ret; // Return the return value of the original method +} +// [End index11] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/index8.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index8.ets new file mode 100644 index 00000000..fb2b9916 --- /dev/null +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index8.ets @@ -0,0 +1,12 @@ +import { util } from '@kit.ArkTS'; + +// [Start index8] +class Test { + foo() {} +} +util.Aspect.addBefore(Test, 'foo', false, (instance: Test) => { + instance.foo(); +}); +// Infinite recursion +new Test().foo(); +// [End index8] \ No newline at end of file diff --git a/AppAspectProgrammingDesign/entry/src/main/ets/pages/index9.ets b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index9.ets new file mode 100644 index 00000000..56aa0782 --- /dev/null +++ b/AppAspectProgrammingDesign/entry/src/main/ets/pages/index9.ets @@ -0,0 +1,13 @@ +import { util } from '@kit.ArkTS'; + +// [Start index9] +class Test { + foo() {} +} +// Save the original method implementation first +let originalFoo = new Test().foo; +util.Aspect.addBefore(Test, 'foo', false, (instance: Test) => { + // If the original method does not use this, you can directly call the originalFoo () method. + // If this is used in the original method, bind should be used to bind the instance, but there will be a compilation warning originalFoo.bind (instance); +}); +// [End index9] \ No newline at end of file -- Gitee