赞
踩
乾坤(qiankun)是一个微前端框架,其核心原理是基于浏览器的浏览器路由劫持和微前端应用的沙箱隔离技术,实现了多个独立前端应用在同一个页面中并存、并能进行通信和交互的能力。
具体来说,乾坤的实现原理包括以下几个关键点:
总的来说,乾坤通过路由劫持和应用沙箱技术实现了多个独立前端应用在同一个页面中并存的能力,并通过通信机制实现了全局状态的共享和数据交互,从而实现了微前端的解决方案。
订阅-发布模式(也称为观察者模式)是一种软件设计模式,用于定义对象间的一对多依赖关系,当一个对象的状态发生改变时,所有依赖于它的对象都会得到通知并自动更新。在订阅-发布模式中,存在两个主要角色:
订阅-发布模式的主要优点包括:
观察者模式是订阅-发布模式的一种特例,它将发布者和订阅者合二为一,即发布者和订阅者是同一个对象。观察者模式通常用于需要监测对象状态变化的场景,例如 GUI 应用程序中的事件监听器。
单例模式(Singleton Pattern)
- const Singleton = (function() {
- let instance;
-
- function createInstance() {
- // 创建实例的逻辑
- return {
- method: function() {
- console.log('Singleton method called');
- }
- };
- }
-
- return {
- getInstance: function() {
- if (!instance) {
- instance = createInstance();
- }
- return instance;
- }
- };
- })();
-
- // 使用单例对象
- const instance1 = Singleton.getInstance();
- const instance2 = Singleton.getInstance();
- console.log(instance1 === instance2); // true,只有一个实例存在
2. 观察者模式(Observer Pattern)
- class Subject {
- constructor() {
- this.observers = [];
- }
-
- subscribe(observer) {
- this.observers.push(observer);
- }
-
- unsubscribe(observer) {
- this.observers = this.observers.filter(subscriber => subscriber !== observer);
- }
-
- notify(data) {
- this.observers.forEach(observer => observer.update(data));
- }
- }
-
- class Observer {
- update(data) {
- console.log(`Received data: ${data}`);
- }
- }
-
- // 使用观察者模式
- const subject = new Subject();
- const observer1 = new Observer();
- const observer2 = new Observer();
-
- subject.subscribe(observer1);
- subject.subscribe(observer2);
-
- subject.notify('New data'); // Output: Received data: New data
3. 工厂模式(Factory Pattern)
- class Product {
- constructor(name) {
- this.name = name;
- }
- }
-
- class ConcreteProductA extends Product {
- constructor() {
- super('Product A');
- }
- }
-
- class ConcreteProductB extends Product {
- constructor() {
- super('Product B');
- }
- }
-
- class Factory {
- createProduct(type) {
- switch (type) {
- case 'A':
- return new ConcreteProductA();
- case 'B':
- return new ConcreteProductB();
- default:
- throw new Error('Invalid product type.');
- }
- }
- }
-
- // 使用工厂模式
- const factory = new Factory();
- const productA = factory.createProduct('A');
- const productB = factory.createProduct('B');
- console.log(productA); // Output: ConcreteProductA { name: 'Product A' }
- console.log(productB); // Output: ConcreteProductB { name: 'Product B' }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。