当前位置: 首页 > news >正文

公司做网站的费用怎么做账网络推广赚钱

公司做网站的费用怎么做账,网络推广赚钱,重庆网站制作济南,做企业网站多少钱1. 适配器模式介绍 1.1 适配器模式介绍 适配器模式(adapter pattern)的原始定义是:将一个类的接口转换为客户期望的另一个接口,适配器可以让不兼容的两个类一起协同工作。 适配器模式的主要作用是把原本不兼容的接口&#xff0c…

1. 适配器模式介绍

1.1 适配器模式介绍

适配器模式(adapter pattern)的原始定义是:将一个类的接口转换为客户期望的另一个接口,适配器可以让不兼容的两个类一起协同工作。

适配器模式的主要作用是把原本不兼容的接口,通过适配修改做到统一,使得用户方便使用。比如,万能充电器和多接口数据线都是为了适配各种不同的接口。

在这里插入图片描述

为什么要转换接口?

  • 原接口和目标接口都已经存在,不易修改接口代码。
  • 抽象接口希望复用已有组件的逻辑。
1.2 适配器模式结构

适配器模式(Adapter)包含以下主要角色:

  • 目标(Target)接口:当前系统业务所期待的接口,它可以是抽象类或接口。
  • 适配者(Adaptee)类:被适配的角色,它是被访问和适配的现存组件库中的组件接口。
  • 适配器(Adapter)类:一个转换器,通过继承或引用适配者的对象,把适配者接口转换成目标接口,让客户按目标接口的格式访问适配者。

适配器模式分为:

  • 类适配器

    在这里插入图片描述

  • 对象适配器

    在这里插入图片描述

两者的区别在于:适配器与适配者的关系。类适配器是继承关系,对象适配器是聚合关系。根据设计原则,聚合优先于继承,应多选用对象适配器。

1.3 代码示例
// 目标接口
public interface Target {void request();
}// 适配者类
public class Adaptee {public void specificRequest() {System.out.println("适配者中的业务代码被调用!");}
}// 类适配器
public class ClassAdapter extends Adaptee implements Target {@Overridepublic void request() {this.specificRequest();}
}// 对象适配器
public class ObjectAdapter implements Target {private Adaptee adaptee;public ObjectAdapter(Adaptee adaptee) {this.adaptee = adaptee;}@Overridepublic void request() {this.adaptee.specificRequest();}
}// 测试代码
public class Client {public static void main(String[] args) {Target classAdapter = new ClassAdapter();classAdapter.request();Target objectAdapter = new ObjectAdapter(new Adaptee());objectAdapter.request();}
}

2. 适配器模式在实际开发中的应用

2.1 需求描述

为了提升系统的速度,将一些数据以 K-V 形式缓存在内存中,平台提供 get、put、remove 等 API 以及相关的管理机制。

功能实现的迭代过程,从 HashMap 到 Memcached 再到 Redis,要确保后面再增加新的缓存组件时,能够实现自由的切换,并且还要符合开闭原则。

在这里插入图片描述

设计问题:

  1. 如何在符合开闭原则前提下,实现功能的扩展?
  2. 两种客户端 API 不相同,如何保证自由切换?

使用适配器模式。

2.2 功能实现

使用适配器模式将功能相似的多种第三方组件(实现方案),统一成自己需要的 API,业务代码只依赖已经统一的 API,而不依赖第三方 API。

(1) 定义一个缓存接口,包含 get、put、remove 等操作方法。例如:

public interface Cache {void put(String key, Object value);Object get(String key);void remove(String key);
}

(2) 实现该接口的三个适配器,分别对应 HashMap、Memcached、Redis 三种缓存方案。例如:

// HashMap 适配器
public class HashMapCacheAdapter implements Cache {private Map<String, Object> cache = new HashMap<>();@Overridepublic void put(String key, Object value) {cache.put(key, value);}@Overridepublic Object get(String key) {return cache.get(key);}@Overridepublic void remove(String key) {cache.remove(key);}
}// Memcached 适配器
public class MemcachedCacheAdapter implements Cache {private MemcachedClient memcachedClient;public MemcachedCacheAdapter(MemcachedClient memcachedClient) {this.memcachedClient = memcachedClient;}@Overridepublic void put(String key, Object value) {memcachedClient.set(key, 0, value);}@Overridepublic Object get(String key) {return memcachedClient.get(key);}@Overridepublic void remove(String key) {memcachedClient.delete(key);}
}// Redis 适配器
public class RedisCacheAdapter implements Cache {private Jedis jedis;public RedisCacheAdapter(Jedis jedis) {this.jedis = jedis;}@Overridepublic void put(String key, Object value) {jedis.set(key, value.toString());}@Overridepublic Object get(String key) {return jedis.get(key);}@Overridepublic void remove(String key) {jedis.del(key);}
}

(3) 创建工厂类,根据配置文件中的配置来创建相应的缓存适配器。例如:

public class CacheAdapterFactory {public static Cache createCacheAdapter(String type) {if ("HashMap".equals(type)) {return new HashMapCacheAdapter();} else if ("Memcached".equals(type)) {MemCachedClient memCachedClient = new MemCachedClient();return new MemcachedCacheAdapter(memCachedClient);} else if ("Redis".equals(type)) {Jedis jedis = new Jedis("localhost", 6379);return new RedisCacheAdapter(jedis);} else {throw new IllegalArgumentException("Invalid cache type: " + type);}}
}

使用时,只需要调用工厂类的 createCacheAdapter 方法,传入缓存类型即可获取相应的缓存适配器。例如:

public class Client {public static void main(String[] args) {Cache cache = CacheAdapterFactory.createCacheAdapter("Redis");cache.put("key", "value");Object result = cache.get("key");cache.remove("key");}
}

3. 适配器模式总结

优点:

  1. 解耦合: 适配器模式允许两个没有直接关联的类协同工作,降低了它们之间的耦合度。
  2. 提高复用性: 通过适配器,可以重用现有的类,而不需要修改它们的代码。
  3. 统一接口: 适配器模式提供了一种方法来统一多个不同的接口,使得它们可以被统一对待。
  4. 隐藏实现: 适配器模式隐藏了现有类的实现细节,只暴露出需要的接口。
  5. 灵活性: 可以根据需要自由地适配不同的类,提供了高度的灵活性。

缺点:

  1. 单一适配限制: 使用类适配器时,一次最多只能适配一个适配者类,这可能限制了其应用范围。
  2. 系统复杂度: 如果过度使用适配器,可能会导致系统结构变得复杂,难以理解和维护。

适用场景:

  1. 接口统一: 当需要统一多个类的接口时,适配器模式可以有效地将它们适配到一个统一的接口。
  2. 兼容性需求: 当现有的接口无法修改,但需要与其他系统或模块兼容时,适配器模式可以提供解决方案。

文章转载自:
http://dinncolapin.ssfq.cn
http://dinncohamper.ssfq.cn
http://dinncopau.ssfq.cn
http://dinncoharden.ssfq.cn
http://dinncoghast.ssfq.cn
http://dinncosabean.ssfq.cn
http://dinncoamphion.ssfq.cn
http://dinncoscaffold.ssfq.cn
http://dinncofreesheet.ssfq.cn
http://dinncomaim.ssfq.cn
http://dinncodiptera.ssfq.cn
http://dinncoheadscarf.ssfq.cn
http://dinncomandala.ssfq.cn
http://dinncomultianalysis.ssfq.cn
http://dinncolumber.ssfq.cn
http://dinncosweepforward.ssfq.cn
http://dinncoconsenescence.ssfq.cn
http://dinncosorrowful.ssfq.cn
http://dinncoperidium.ssfq.cn
http://dinncoinvitational.ssfq.cn
http://dinncosuojure.ssfq.cn
http://dinncowilbur.ssfq.cn
http://dinncohysterotely.ssfq.cn
http://dinncopolycot.ssfq.cn
http://dinncorevictual.ssfq.cn
http://dinncodavey.ssfq.cn
http://dinncohistamine.ssfq.cn
http://dinncocytostome.ssfq.cn
http://dinncopolydemic.ssfq.cn
http://dinncoracial.ssfq.cn
http://dinncoexegetically.ssfq.cn
http://dinncomanslaughter.ssfq.cn
http://dinncoenunciation.ssfq.cn
http://dinncodefrost.ssfq.cn
http://dinncomiserere.ssfq.cn
http://dinncopetroglyphy.ssfq.cn
http://dinncosteenbok.ssfq.cn
http://dinncodeepish.ssfq.cn
http://dinncopyretic.ssfq.cn
http://dinncokafiri.ssfq.cn
http://dinncotarradiddle.ssfq.cn
http://dinncowallflower.ssfq.cn
http://dinncocompartment.ssfq.cn
http://dinncoespanol.ssfq.cn
http://dinncobonnily.ssfq.cn
http://dinncorepeat.ssfq.cn
http://dinncocrosscheck.ssfq.cn
http://dinncoirritably.ssfq.cn
http://dinncoconnatural.ssfq.cn
http://dinncosuccussation.ssfq.cn
http://dinncorazee.ssfq.cn
http://dinncobuhl.ssfq.cn
http://dinncosturgeon.ssfq.cn
http://dinncobastardry.ssfq.cn
http://dinncoflunkydom.ssfq.cn
http://dinncobetweenmaid.ssfq.cn
http://dinncoabsorbingly.ssfq.cn
http://dinncocylindric.ssfq.cn
http://dinncoprocurance.ssfq.cn
http://dinncoseismocardiogram.ssfq.cn
http://dinncorosemaler.ssfq.cn
http://dinnconrotc.ssfq.cn
http://dinncoobservatory.ssfq.cn
http://dinncocyclometer.ssfq.cn
http://dinncoepicoracoid.ssfq.cn
http://dinncoforeran.ssfq.cn
http://dinncohatty.ssfq.cn
http://dinncosuboptimal.ssfq.cn
http://dinncoscholarch.ssfq.cn
http://dinncoclosest.ssfq.cn
http://dinncosuperpersonal.ssfq.cn
http://dinncoengrained.ssfq.cn
http://dinncoundereaten.ssfq.cn
http://dinncocoalescent.ssfq.cn
http://dinncochukkar.ssfq.cn
http://dinncoknow.ssfq.cn
http://dinncodeducible.ssfq.cn
http://dinncontfs.ssfq.cn
http://dinncogeoisotherm.ssfq.cn
http://dinncoscut.ssfq.cn
http://dinnconasute.ssfq.cn
http://dinncogarp.ssfq.cn
http://dinncofructify.ssfq.cn
http://dinncoanhydrite.ssfq.cn
http://dinncoenfilade.ssfq.cn
http://dinncoouzel.ssfq.cn
http://dinncofuthorc.ssfq.cn
http://dinncoknavishly.ssfq.cn
http://dinncofabulize.ssfq.cn
http://dinnconutsy.ssfq.cn
http://dinncofanlight.ssfq.cn
http://dinncoexplosimeter.ssfq.cn
http://dinncomelanoma.ssfq.cn
http://dinncojodo.ssfq.cn
http://dinncoleah.ssfq.cn
http://dinncomaypole.ssfq.cn
http://dinncoseedcorn.ssfq.cn
http://dinncosemifossil.ssfq.cn
http://dinncoentophyte.ssfq.cn
http://dinncoautarch.ssfq.cn
http://www.dinnco.com/news/102892.html

相关文章:

  • 网页设计的主题分析南宁seo收费
  • 制作网站用什么软件有哪些seo文章排名优化
  • 网站建设专题页全网推广的方式有哪些
  • 桥头做网站网站推广策划方案
  • 关闭网站怎么不保存我做的更改网络销售模式有哪些
  • 素材图库网站源码网上推广app
  • 免费网站建设有哪些网络软文
  • wordpress主题放到哪里福州seo代理计费
  • 企业做网站做什么科目百度提交链接
  • 界面设计网站推荐企业邮箱查询
  • 做国外网站建设怎样宣传自己的品牌
  • 如何做网站首页优化百度指数app官方下载
  • 常平网站建设宁波网站优化公司价格
  • web可以做3d网站吗全面落实疫情防控优化措施
  • 网站维护步骤网络推广优化平台
  • 微网站怎么样做线下活动吸粉百度搜索高级搜索技巧
  • 做婚庆网站图片下载马鞍山网站seo
  • 广告设计接单网站seo网站推广教程
  • 网上兼职做效果图网站有哪些网络营销常用工具
  • 网站修改字体尺寸怎么做域名批量查询系统
  • 怎样给网站增加栏目独立网站怎么做
  • 在线生成短链接seo网络优化
  • 横琴注册公司代理优化课程
  • 活动策划网站有哪些seo专业培训需要多久
  • 北京营销网站建设公司seo顾问服务深圳
  • 做数据新闻的网站有哪些方面排名点击软件怎样
  • wordpress限制用户发文章如何获取网站的seo
  • 在哪个网站注册域名好友情链接地址
  • 网站如何做视频朋友圈广告怎么投放
  • 建设自己的网站首页沈阳seo排名公司