设计模式学习之策略模式

说明 策略模式是设计模式中使用频率很高的模式,主要的就是实现对行为的包装,达到结果的方式有多种,使用者可以选择任何一个方式来得到想要结果,在增加新的方式时更加的方便与灵活。它是为了适应算法的灵活性而产生的。 策略模式实现 以常用的购物为例,一般情况下购物分为浏览商品,下单,支付。在支付时消费者可以选择多种不同的支付方式,如支付宝、微信、京东支付、银联支付等。在支付中流程就可以使用到策略模式,网站为用户提供了这些支付方式可供选择,用户只需要选择自己喜欢的支付方式来进行充值就可以得到商品了。在一般情况下都会定义一个抽象类来定义支付的一些行为,如金额,商品简介等信息。实现的抽象类如以下定义: package cn.imcompany.pay; /** * Created by tomyli on 2018/6/20. * Github: https://github.com/peng051410 */ public interface Payment { boolean pay(String param); } 这样就定义了支付方式的行为,剩下的就由不同的支付方式来进行实现,比如支付宝支付、微信支付,示例代码如下: package cn.imcompany.pay; /** * Created by tomyli on 2018/6/20. * Github: https://github.com/peng051410 */ public class AliPay implements Payment { @Override public boolean pay(String param) { System.out.println("AliPay"); return true; } } public class WechatPay implements Payment { @Override public boolean pay(String param) { System.out.println("WechatPay"); return true; } } 要是再增加一种支付方式则可以实现Payment接口进行实现就可以了,一般情况下会有一个维护支付方式的常量类,由它来维护可用的支付方式。这个常量类的简化代码如下: ...

2018-06-20 · 1 min · 159 words · tomyli

设计模式学习之工厂模式

说明 工厂设计模式是23种设计模式中使用频率非常高的,属于创建型模式。主要特点是实现了实体创建与使用的分离,达到了解耦的目的。工厂设计模式一般分为简单工厂、工厂方法、抽象工厂。 前提准备 4年一界世界杯即将到来,啤酒厂商又要大嫌一笔。我们先准备好要生产的啤酒实体。 package cn.imcompany; /** * Created by tomyli on 2018/5/27. * Github: https://github.com/peng051410 */ public interface Beer { String getName(); } public class JinShiBai implements Beer { @Override public String getName() { return "金士百"; } } public class QingDao implements Beer { @Override public String getName() { return "青岛"; } } public class BaiWei implements Beer { @Override public String getName() { return "百威"; } } 在上面定义三种啤酒,它们都由抽象类Beer继承而来。下面使用简单工厂来给消费者提供啤酒。 ...

2018-06-12 · 2 min · 413 words · tomyli

设计模式学习之原型模式

特点 原型设计模式就是系统中产生的每一个对象都不相同,通过原型来创建新的对象,原型模式属于创建型模式。 实现方式 一般情况可以通过对象克隆的方式来根据一个对象创建出来多个对象,每个对象在内存占用的内存地址都不一样。在JAVA中克隆又分为浅克隆与深克隆。 浅克隆 JAVA中是按值进行传递。实现克隆的方式就是实现Cloneable接口,这样就可以重写Object对象的clone方法来进行对象的克隆。代码如下: /** * Created by tomyli on 2018/5/30. * Github: https://github.com/peng051410 */ public class Apple implements Cloneable { public String name; public double weight; @Override protected Object clone() throws CloneNotSupportedException { return super.clone(); } } 测试代码如下: public static void main(String[] args) throws Exception { Apple apple = new Apple(); apple.name = "apple"; apple.weight = 2.23; try { Apple clone = (Apple)apple.clone(); System.out.println(clone == apple); } catch (Exception e) { e.printStackTrace(); } } 测试代码中clone与apple是两个完全不同的对象,这样就通过apple这个原型创建出来一个全新的对象。但是这里存在一个问题,现在Apple对象中只包含了值类型的成员变量,如果包含了其它对象会克隆也会成功吗?我们在Apple对象中增加一个Stone对象的集合,代码: ...

2018-06-09 · 1 min · 195 words · tomyli

设计模式学习之单例模式

特点 单例模式就是确保在系统中只在一个实例提供功能。单例有好几种写法,主要有饿汉式、懒汉式、静态方法内部类、注册式单例。 饿汉式 饿汉式单例就是在类定义时就已经将实例进行了初始化,在系统调用时可以直接返回不需要再实例化。示例代码如下: public class Hungry { private Hungry() { } private static final Hungry INSTANCE = new Hungry(); public static Hungry getInstance() { return INSTANCE; } } 饿汉式的优点是提前进行初始化,线程安全。缺点是在系统未调用的情况下占用了内存空间,是以空间换取时间的样例 懒汉式 懒汉式就是在使用才对对象实例进行初始化,达到了延迟加载的目的。示例代码如下: public class LazyOne { private LazyOne() { } private static LazyOne instance = null; public static LazyOne getInstance() { if (instance == null) { instance = new LazyOne(); } return instance; } } 懒式式优化点使用时实例化,延迟加载。缺点是存在线程安全问题 ...

2018-06-05 · 3 min · 512 words · tomyli

Idea中vim键的映射

Idea中IdeaVim插件可以让我们在Idea中使用vi的按键来进行操作,但是原生的配置只带有一些基本的移动操作。需要在~/.ideavimrc文件中进行Idea的动作映射来发挥它们的最大力量。 Key Description kj gt VimBack gT VimForward gd GotoDeclaration gf GotoTypeDeclaration gh QuickJavaDoc gs GotoSymbol gi GotoImplementation gu FindUsages gj IntroduceVariable leader w d ActivateDatabaseToolWindow leader ' ActivateTerminalToolWindow leader w r ActivateRedisServersToolWindow leader w m ActivateMavenProjectsToolWindow leader w a HideActiveWindow leader w w HideAllWindows leader w p ActivateProjectToolWindow leader w s ActivateStructureToolWindow leader w v ActivateVersionControlToolWindow leader w h h Go to left window leader w l l Go to right window leader w k k Go to window up leader w j j Go to window down leader w V v Split Window Vertically leader w - s Split Winodw Horizontally leader w c c Close current window leader w o o Close all window except current leader GotoAction leader c c GotoClass leader c r CopyReference leader c p CopyPaths leader f f GotoFile leader f d DashLauncherAction leader b b RecentFiles leader b p FileStructurePopup leader b u ReopenClosedTab leader d d Debug leader d D DebugClass leader d j JRebel leader l l EvaluateExpression leader t t ToggleLineBreakpoint leader r r Run leader r R RunClass leader i m ImplementMethods leader e e ShowErrorDescription leader e n GotoNextError leader R :source ~/.ideavimrc leader g s Vcs.QuickListPopupAction leader r p ReplaceInPath zO ExpandAllRegions zo ExpandRegion zc CollapseRegion zC CollapseAllRegions fj emacsIDEAs.AceJump fw emacsIDEAs.AceJumpWord ff CommentByLineComment fb CommentByBlockComment gd GotoDeclaration gf GotoTypeDeclaration gh QuickJavaDoc gs GotoSymbol gi GotoImplementation gu FindUsages gj IntroduceVariable ,, ReformatCode ,r RenameElementi Back Forward leader R reload ~/.ideavimrc leader a l :actionlist leader g s Vcs.QuickListPopupAction leader s p FindInPath leader r p ReplaceInPath , c r Revert change , c t Close other tab

2018-05-14 · 2 min · 272 words · tomyli

title = “2020-ARTS-打卡第十五天” lastmod = 2026-01-26T15:43:06+08:00 tags = [“arts”, “java”, “leetcode”] categories = [“arts”] draft = false author = “tomyli” +++ Algorithm 题目 题目描述 求一个数的N次幂 题目解答 public class MyPow { public double doPow(double x, int n) { if(x == 0) { return 0; } if(x == 1 || n == 0) { return 1; } if(n == 1) { return x; } long b = n; if(b < 0) { x = 1 / x; b = -b; } double res = 1.0; while(b > 0) { if((b & 1) == 1) { res *= x; } x *= x; b >>= 1; } return res; } } Review Tip Share

1 min · 111 words · Me