- package lq.test;
- import java.io.*;
- import java.util.*;
- //*********创建型模式***************
- //factory method 1
- //1具体的构造算法,和2构造出的具体产品由子类实现
- interface Product {
- }
- //或者我也提供一个工厂的接口,由这个抽象类来继承它
- abstract class Factory {
- abstract public Product fmd();;
- //我认为这个方方法的存在是,是对FactoryMethod方法的补充
- //例如可以为生成的对象赋值,计算为生成对象应付何值,前后的日值
- //且这些都是公用的,生成产品的最主要算法还是在FactoryMethod中,
- //这个方法只是起辅助作用,这也是一种思维方法,将具体的算法实现在一个方法中
- //而我不直接调用此方法,而使用另外的一个方法封装它,等到了更灵活的效果,而
- //子类需实现的内容是FactoryMethod
- //此方法是一个TemplateMethod
- public Product creat(); {
- Product pd = null;
- System.out.println("before operation");;
- pd = fmd();;
- System.out.println("end operation");;
- return pd;
- }
- }
- class Product1 implements Product {
- }
- class Factory1 extends Factory {
- public Product fmd(); {
- Product pd = new Product1();;
- return pd;
- }
- }
- //FactroyMethod 2
- //这种方式简单实用
- interface Producta {
- }
- interface Factorya {
- Producta create();;
- }
- class Producta1 implements Producta {}
- class Factorya1 implements Factorya {
- public Producta create(); {
- Producta pda = null;
- pda = new Producta1();;
- return pda;
- }
- }
- //AbstractFactory
- //AbstractFactory与FactoryMethod的不同在于AbstractFactory创建多个产品
- //感觉此模式没有什么大用
- //当然可以还有更多的接口
- interface Apda {}
- interface Apdb {}
- interface Afactory {
- Apda createA();;
- Apdb createB();;
- }
- class Apda1 implements Apda {}
- class Apdb1 implements Apdb {}
- //有几个接口就有几个对应的方法
- class Afactory1 implements Afactory {
- public Apda createA(); {
- Apda apda = null;
- apda = new Apda1();;
- return apda;
- }
- public Apdb createB(); {
- Apdb apdb = null;
- apdb = new Apdb1();;
- return apdb;
- }
- }
- //Builder
- //一个产品的生成分为生成部件和组装部件,不同的产品每个部件生成的方式不同
- //而组装的方式相同,部件的生成抽象成接口方法,而组装的方法使用一个TemplateMethod方法
- interface Cpda {}
- class Cpda1 implements Cpda {}
- interface BuilderI {
- void buildPart1();;
- void buildPart2();;
- void initPd();;
- Cpda getPd();;
- }
- abstract class BuilderA implements BuilderI {
- Cpda cpda;
- public Cpda getPd(); {
- initPd();;
- //对对象的内容进行设置
- buildPart1();;
- buildPart2();;
- return cpda;
- }
- }
- class Builder extends BuilderA {
- public void buildPart1(); {
- System.out.println(cpda);;
- }
- public void buildPart2(); {
- System.out.println(cpda);;
- }
- public void initPd(); {
- cpda = new Cpda1();;
- }
- }
- //一个简单的生成产品的实现
- //1
- abstract class Fy {
- public abstract void med1();;
- static class Fy1 extends Fy {
- public void med1(); {
- }
- }
- public static Fy getInstance(); {
- Fy fy = new Fy1();;
- return fy;
- // Fy fy = new Fy1(); {//这种匿名内部类是静态的!!
- // public void med1(); {
- // }
- // };
- // return fy;
- }
- }
- //2
- interface Pdd {}
- class Pdd1 implements Pdd {}
- abstract class Fya {
- public static Pdd getPd(); {
- Pdd pdd = new Pdd1();;
- return pdd;
- }
- }
- //Prototype 在java中就是clone,又包含深拷贝和浅拷贝
- class CloneObja {
- public CloneObja MyClone(); {
- return new CloneObja();;
- }
- }
- class CloneObjb {
- public CloneObjb MyClone(); throws Throwable {
- CloneObjb cobj = null;
- cobj = (CloneObjb); pcl(this);;
- return cobj;
- }
- //深度拷贝算法
- private Object pcl(Object obj); throws Throwable {
- ByteArrayOutputStream bao = new ByteArrayOutputStream(1000);;
- ObjectOutputStream objo = new ObjectOutputStream(bao);;
- objo.writeObject(obj);;
- ByteArrayInputStream bai = new ByteArrayInputStream(bao.toByteArray(););;
- ObjectInputStream obji = new ObjectInputStream(bai);;
- Object objr = obji.readObject();;
- return objr;
- }
- }
- //Singleton
- //一个类只有一个对象,例如一个线程池,一个cache
- class Singleton1 {
- public static Singleton1 instance = new Singleton1();;
- private Singleton1(); {
- }
- public static Singleton1 getInstance(); {
- return instance;
- }
- }
- class Singleton2 {
- public static Singleton2 instance;
- private Singleton2(); {
- }
- // public static Singleton2 getInstance(); {
- // if (instance == null); {
- // instance = new Singleton2();;
- // }
- //
- // return instance;
- // }
- public static Singleton2 getInstance(); {
- synchronized(Singleton2.class); {
- if (instance == null); {
- instance = new Singleton2();;
- }
- }
- return instance;
- }
- }
- //**********结构型模式**********
- //Adapter
- //基本方法有两种,一种是使用引用一种使用继承
- //将不符合标准的接口转成符合标准的接口,接口的修改主要是参数的增减,
- //返回值类型,当然还有方法名
- //感觉这就是封装的另一种表示形式,封装有用方法封装(在方法中调用功能方法);,
- //用类封装(先传入功能方法所在的类的对象,通过调用此对象的功能方法);
- //使用引用的形式
- 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;
- }
- }
- //Proxy
- 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");;
- }
- }
- //Bridge
- //感觉就是多态的实现
- 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();;
- }
- }
- //Composite
- 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);;
- }
- }
- //Decorator
- //对一个类的功能进行扩展时,我可以使用继承,但是不够灵活,所以选用了
- //另外的一种形式,引用与继承都可活得对对象的一定的使用能力,而使用引用将更灵活
- //我们要保证是对原功能的追加而不是修改,否则只能重写方法,或使用新的方法
- //注意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
- }
- }
- //Facade
- //非常实用的一种设计模式,我可以为外部提供感兴趣的接口
- 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
- //空
- //**********行为型模式*************
- //Chain of Responsibility
- //与Decorator的实现形式相类似, &nb
设计模式
最新推荐文章于 2024-09-15 22:03:14 发布