Skip to content

Latest commit

History

History
236 lines (182 loc) · 5.7 KB

File metadata and controls

236 lines (182 loc) · 5.7 KB

设计模式 Java语言实现

装饰器模式

下面我们实现了通过装饰器来位圣诞树实例增加新的描述的装饰器设计

  • 圣诞树接口
publicinterfaceChristmasTree {
/** * @return 圣诞树的描述 */Stringdecorate();
}
  • 具体的圣诞树
publicclassRealChristmasTreeimplementsChristmasTree{
@OverridepublicStringdecorate() {
return"Christmas Tree";
}
}
  • 装饰类
publicclassTreeDecoratorimplementsChristmasTree {
privateChristmasTreechristmasTree;
publicTreeDecorator(ChristmasTreetree) {
this.christmasTree = tree;
}
@OverridepublicStringdecorate() {
returnchristmasTree.decorate();
}
}
  • 具体的装饰类 灯
publicclassBubbleLightextendsTreeDecorator {
publicBubbleLight(ChristmasTreetree) {
super(tree);
}
@OverridepublicStringdecorate() {
returnsuper.decorate() + decorateWithBubbleLights();
}
privateStringdecorateWithBubbleLights() {
return" with Bubble Lights";
}
}
  • 另一个具体装饰类
publicclassGarLandextendsTreeDecorator {
publicGarLand(ChristmasTreetree) {
super(tree);
}
@OverridepublicStringdecorate() {
returnsuper.decorate() + decorateWithGarLand();
}
privateStringdecorateWithGarLand() {
return" with GarLand";
}
}
//JUnit 5classDecoratorTest {
@Testvoiddecorate() {
//简单的一个装饰器ChristmasTreetree = newBubbleLight(newRealChristmasTree());
assertEquals(tree.decorate(), "Christmas Tree with Bubble Lights");
//两个相同的装饰器ChristmasTreecomplex_tree = newBubbleLight(newBubbleLight(newRealChristmasTree()));
assertEquals(complex_tree.decorate(), "Christmas Tree with Bubble Lights with Bubble Lights");
//两个不同的装饰器ChristmasTreecombine_tree = newBubbleLight(newGarLand(newRealChristmasTree()));
assertEquals(combine_tree.decorate(), "Christmas Tree with GarLand with Bubble Lights");
}
}

适配器模式

LinkedList -> Stack

LinkedList

  • getLast
  • removeLast
  • add
  • size

Stack

  • push
  • pop
  • peek
  • size

PS:LinkedList中已经提供了这些接口,同样是LinkedList原有方法的封装

PPS: Java的Queue是一个接口,链表也实现了其所需的所有功能

publicinterfaceQueue<E> extendsCollection<E>
importjava.util.LinkedList;
importjava.util.Queue;
Queue<Integer> queue = newLinkedList<>();
Throws exceptionReturns special value
Insertadd(e)offer(e)
Removeremove()poll()
Examineelement()peek()

代码实现链接

PPPS:Java中基础的的StackQueue的使用

LinkedList<String> stack = newLinkedList<>();
stack.push("1");
System.out.println(stack.peek());
System.out.println(stack.poll());
System.out.println(stack.isEmpty());
System.out.println(stack.size());
Queue<String> queue = newLinkedList<>();
queue.add("str");
System.out.println(queue.element());
queue.remove(queue.remove());
System.out.println(queue.isEmpty());
System.out.println(queue.size());

单例模式

惰性实现的单例模式

同步getInstance()方法 同步整个方法的开销比较大,多个进程

/** * 利用同步来实现安全的多线程单例模式 * 延迟加载 */publicclassSingleton {
privatestaticSingletonuniqueInstance;
privateSingleton() {
}
publicstaticsynchronizedSingletongetInstance() {
if (uniqueInstance == null) {
uniqueInstance = newSingleton();
}
returnuniqueInstance;
}
}

急切的创建实例

//当创建单例的代价不太大时publicclassSingleton {
privatestaticSingletonuniqueInstance = newSingleton();
privateSingleton() {
}
publicstaticSingletongetInstance() {
returnuniqueInstance;
}
}

双重检查加锁

/** * 利用双重检查加锁来实现安全的多线程单例模式 */publicclassSingleton {
privatevolatilestaticSingletonuniqueInstance;
privateSingleton() {
}
publicstaticSingletongetInstance() {
//如果类还没有被创建if (uniqueInstance == null) {
//在这里同步多个线程 同一时刻只有一个线程能够进入下面的代码synchronized (Singleton.class) {
//进程申请进入这段代码时实例未创建 但是在其进入这段代码前可能其他进程已经进入//这段代码并创建了实例 所以这里进行第二次检查if (uniqueInstance == null) {
uniqueInstance = newSingleton();
}
}
}
returnuniqueInstance;
}
}