结构类设计模式

来源:互联网 发布:易什么电器软件 编辑:程序博客网 时间:2024/05/11 05:58

享元模式

Using sharing to support large numbers of fine-grained objects efficiently.

flyweight pattern--------叫轻量级模式更适合吧,享元模式好奇怪的名字。

通过对象共享来减少对象的生成,对象的生成放在工厂之中,本质是对于对象的生成采取什么样的方式,感觉应该放在创建类设计模式中更加适合?

import java.util.HashMap;import java.util.Map;/* * 享元模式的所有对象必然有一个必须接受的外部状态 * */public abstract class FlyWeight{private String extrinsic;private String intrinsic;public String getIntrinsic() {return intrinsic;}public void setIntrinsic(String intrinsic) {this.intrinsic = intrinsic;}public FlyWeight(String extrinsic){this.extrinsic = extrinsic;}public abstract void doSomething();}public class ConcreteFlyWeight extends FlyWeight{public void doSomething(){System.out.println("do something...");}public ConcreteFlyWeight(String extrinsic){super(extrinsic);}}//简单工厂,通过extrinsic判断是否返回同一个对象public class FlyWeightFactory{private Map map = new HashMap();public FlyWeight getInstance(String extrinsic){FlyWeight fw = null;if(map.containsKey(extrinsic)){fw = (FlyWeight)map.get(extrinsic);}else{fw = new ConcreteFlyWeight(extrinsic);map.put(extrinsic, fw);}return fw;}}




组合模式

compose objects into tree structures to represent part-whole hierarchies.Composite lets clients treat individual objects and compositions of objects uniformly.

组合模式和学语法时组合是两回事,就是通过一个共同接口还构造节点,然后节点和节点之间连接形成一棵树。组合模式就是树的组织方式。

public abstract class Component{//所有节点共同逻辑    public abstract void doSomething();}// 树枝public class Composite extends Component{    private List<Component> list = new ArrayList<Component>();public void add(Component c){   list.add(c);}public void remove(Component c){   list.remove(c);}public void doSomething(){   System.out.println("Composite does something...");}}//叶子public class Leaf extends Component{    public void doSomething(){   System.out.println("leaf does something...");}}

适配器模式

Convert the interface of a class into another interface clients expect.Adapter lets classes work together that couldn't otherwise because of imcompatible interfaces.

感觉设计模式就是三种关系:1 套类;2 继承;3 组合。适配器模式就是将逻辑通过组合继承套类实现转换接口的目的,就是对原始的类通过组合继承套类变成能被高层模块调用的类。适配器模式和装饰模式不同的地方是装饰模式与原始对象实现同一接口,而适配器模式两个类之间不需要这种约束。

public class Adaptee{   public void doSomething(){      System.out.println("do something...");   }}// 组合实现转换public class Adapter{   Adaptee a = new Adaptee();   public void doSomething(){      a.doSomething();   }}// 继承实现转换public class Adapter extends Adaptee{  public void doSomething(){     super.doSomething();  }}


 

代理模式

Provide a surrogate or placeholder for another object to control access to it.

代理模式和适配器模式不同的是适配器模式一般用于DTO,适配器不要共同接口不用和被适配器对象处于一种业务逻辑中,而代理模式一般代理对象和原始对象处于同一个业务逻辑中,实现同一接口。

代理模式通过组合实现,如果原始业务逻辑处理的逻辑不够完善,也可以通过代理模式补充逻辑,由于实现的是同一接口,因此可以无缝对接。

public interface Subject{    public void doSomething();}public class ConcreteSubject implements Subject{    public void doSomething(){   System.out.println("do something...");}//通过必须传入一个Subject强制控制只能使用Proxypublic ConcreteSubject(Subject s){}}// 普通代理模式,高层直接调用Proxypublic class Proxy implements Subject{    Subject s;public Proxy(){Subject s = new ConcreteSubject(this);}    public void doSomething(){   before();   s.doSomething();   after();}private void before(){   System.out.println("before...");}private void after(){   System.out.println("after...");}}


 强制代理or动态代理

桥梁模式

Bridge Pattern --- Decouple an abstraction from its implementation so that the two can vary independently.

桥梁模式将一个对象分成两部分,一部分固定不变的一部分可变化的,然后通过组合将变化的部分塞入不变的那部分,如果变化的那部分出现其他可能则通过其他类实现变化的那部分的接口。实现拥抱变化

桥梁模式说到底还是组合模式,在代理模式中,如果将代理类看成整个业务逻辑,那么被代理类就对应着桥梁模式的可变化部分。

适配器也好、桥梁也好、代理也好,说到底,就是一个组合。实际上,结构类设计模式,就是组合的不同应用。而在行为类设计模式中,观察者模式,就是通过集合类组合更多的对象;策略模式中,在Context类中,用于封装类中,就是封装了策略接口;等等。。。

public abstract class Implementor{   public abstract void dosomething();}public ConcreteImplementor extends Implementor{   public abstract void dosomething(){      System.out.println("concreteImplementor...");   }}public abstract class Abstraction{   public abstract void dosomething();}public ConcreteAbstraction extends Abstraction{   Implementor impl = new Implementor();   public abstract void dosomething(){       System.out.println("ConcreteAbstraction...");   impl.doSomething();   }}

 

装饰模式

Decorator Pattern --- Attach additional responsibilities to an object dynamically keeping the same interface. Decorators provide a flexible alternative to subclassing for extending functionality.

装饰模式可以看成代理模式的升级版,代理类就是原始装饰类,代理类的下层再派生出多个对象,每个对象带有不同的功能。方便原始对象组合这些功能。而在代理模式中,将额外的功能完全组合到了代理类中,没有进行拆分。

实际上不要这个原始装饰类也是可以的,原始类直接派生子类,派生类的构造函数需要再传入共同的接口引用方便装饰。但这样装饰类和原始类混杂在一起了,而且已经从基类派生出来的,再传入基类引用才能构造,感觉不是那么合理。

 

public abstract class Component{   public abstract void doSomething();}public class ConcreteComponent extends Component{   public void doSomething(){      System.out.println("component do something...");   }}// 继承同一接口,同时组合ConcreteComponentpublic class Decorator extends Component{    private Component c;public Decorator(Component c){   this.c = c;}private void doSomething(){   System.out.println("basic Decorator do something..");   c.doSomething();}}//装饰类继承基本的装饰类public class BeforeDecorator extends Decorator{    private void before(){   System.out.println("before....");}public BeforeDecorator(Component c){   super(c);}public void doSomething(){   before();   super.doSomething();}}public class AfterDecorator extends Decorator{   private void after(){      System.out.println("after...");   }   public void doSomething(){      super.doSomething();      after();   }   public AfterDecorator(Component c){     super(c);   }}//然后高层模块就可以用装饰类组装原始对象


门面模式

facade pattern  ---  Provide a unified interface to a set of interfaces in a subsystem. Facade defines a higher-level interface that makes the subsystem easier to use. 

门面模式是组合的最高升级版,内部多个类组合,构成唯一的接口给外部调用,基本就是迪米特法则的解释,朋友之间通信也该最少。

public class A{   public void doSomething(){      System.out.println("A do something...");   }}public class B{   public void doSomething(){      System.out.println("B do something...");   }}public class C{   public void doSomething(){      System.out.println("C do something...");   }}public class Facade{  private A a = new A();  private B b = new B();  private C c = new C();  public void doSomething(){    a.doSomething();b.doSomething();c.doSomething();  }}


 


 

原创粉丝点击