23种设计模式的代码版(Java) ---结构型

[color=darkred]//**********结构型模式**********[/color]

[color=darkred]//Adapter [/color]
//基本方法有两种,一种是使用引用一种使用继承
//将不符合标准的接口转成符合标准的接口,接口的修改主要是参数的增减,
//返回值类型,当然还有方法名
//感觉这就是封装的另一种表示形式,封装有用方法封装(在方法中调用功能方法),
//用类封装(先传入功能方法所在的类的对象,通过调用此对象的功能方法)

//使用引用的形式
class Adapteea {
public void kk() {}
}

interface Targeta {
String vv(int i, int k);
}

class Adaptera implements Targeta{
Adapteea ade;

public Adaptera(Adapteea ade) {
this.ade = ade;
}

public String vv(int i, int k) {
//具体的业务方法实现在Adaptee中,这个方法
//只起到了接口转换的作用
//调用此方法是通过引用
ade.kk();
return null;
}
}

//使用继承形式的
class Adapteeb {
public void kk() {}
}

interface Targetb {
String vv(int i, int k);
}

class Adapterb extends Adapteeb implements Targetb {
public String vv(int i, int k) {
//调用此方法是通过继承
kk();
return null;
}
}

[color=darkred]//Proxy [/color]
interface Subject {
void request();
}

class realSubject implements Subject {
public void request() {
//do the real business
}
}

class Proxy implements Subject {
Subject subject;

public Proxy(Subject subject) {
this.subject = subject;
}

public void request() {
System.out.println("do something");

subject.request();

System.out.println("do something");
}
}

[color=darkred]//Bridge [/color]
//感觉就是多态的实现

interface Imp {
void operation();
}

class Cimp1 implements Imp {
public void operation() {
System.out.println("1");
}
}

class Cimp2 implements Imp {
public void operation() {
System.out.println("2");
}
}

class Invoker {
Imp imp = new Cimp1();

public void invoke() {
imp.operation();
}
}

[color=darkred]//Composite [/color]
interface Component {
void operation();

void add(Component component);

void remove(Component component);
}

class Leaf implements Component {
public void operation() {
System.out.println("an operation");
}

public void add(Component component) {
throw new UnsupportedOperationException();
}

public void remove(Component component) {
throw new UnsupportedOperationException();
}
}

class Composite implements Component {
List components = new ArrayList();

public void operation() {
Component component = null;

Iterator it = components.iterator();
while (it.hasNext()) {
//不知道此component对象是leaf还是composite,
//如果是leaf则直接实现操作,如果是composite则继续递归调用
component = (Component) it.next();
component.operation();
}
}

public void add(Component component) {
components.add(component);
}

public void remove(Component component) {
components.remove(component);
}
}

[color=darkred]//Decorator [/color]
//对一个类的功能进行扩展时,我可以使用继承,但是不够灵活,所以选用了
//另外的一种形式,引用与继承都可活得对对象的一定的使用能力,而使用引用将更灵活
//我们要保证是对原功能的追加而不是修改,否则只能重写方法,或使用新的方法
//注意concrete的可以直接new出来,
//而decorator的则需要用一个另外的decorator对象才能生成对象
//使用对象封装,和公用接口
//Decorator链上可以有多个元素

interface Componenta {
void operation();
}

class ConcreteComponent implements Componenta {
public void operation() {
System.out.println("do something");
}
}

class Decorator implements Componenta {
private Componenta component;

public Decorator(Componenta component) {
this.component = component;
}

public void operation() {
//do something before

component.operation();

//do something after
}
}

[color=darkred]//Facade [/color]
//非常实用的一种设计模式,我可以为外部提供感兴趣的接口

class Obj1 {
public void ope1() {}
public void ope2() {}
}

class Obj2 {
public void ope1() {}
public void ope2() {}
}

class Facade {
//我得到了一个简洁清晰的接口
public void fdMethod() {
Obj1 obj1 = new Obj1();
Obj2 obj2 = new Obj2();

obj1.ope1();
obj2.ope2();
}
}

//Flyweight
//空
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值