java_Java的23种设计模式_疯狂Java总结_.pdf

上传人:qwe****56 文档编号:69623872 上传时间:2023-01-07 格式:PDF 页数:31 大小:649.24KB
返回 下载 相关 举报
java_Java的23种设计模式_疯狂Java总结_.pdf_第1页
第1页 / 共31页
java_Java的23种设计模式_疯狂Java总结_.pdf_第2页
第2页 / 共31页
点击查看更多>>
资源描述

《java_Java的23种设计模式_疯狂Java总结_.pdf》由会员分享,可在线阅读,更多相关《java_Java的23种设计模式_疯狂Java总结_.pdf(31页珍藏版)》请在taowenge.com淘文阁网|工程机械CAD图纸|机械工程制图|CAD装配图下载|SolidWorks_CaTia_CAD_UG_PROE_设计图分享下载上搜索。

1、 Java?Java?Java?Java?1.?2 1.1?3 1.1.1?3 1.1.2?5 1.1.3?9 1.1.4?12 1.1.5?14 1.2?16 1.2.1?16 1.2.2?18 1.2.3?21 1.2.4?25 1.2.5?27 1.2.6?31 1.2.7?33 1.3?36 1.3.1?36 1.3.2?39 1.3.3?42 1.3.4?44 1.3.5?48 1.3.6?51 1.3.7?53 1.3.8?57 1.3.9?60 1.3.10?62 1.3.11?64 1 Java?Java?1.?)?(?*?Java?J*va?*?*?*?*?*?*?*?J*v

2、a?Java?Java?http:/www.crazyit.org?Java?Java?Java?*?Java?Java EE?Java?Java*E?*?Java?Java EE?*?Jav*?ID?Java?Java EE?*?Java?*?Apache 2.0?(?)?Apache 2.0?2 Java?Java?1.1?AbstractFactory(?)FactoryMethod(?)Singleton(?)Builder(?)Protot*pe*?)1.1.1?*?FactoryMethod?*?1.?2.?3.?*?*?*?1.Product?2.ConcreteProduct?

3、Product?3.Creator?Product?*Creator?ConcreteProduct?Product?4.ConcreteCreator?ConcreteProduct?3 Java?Java?*roduct public interface Work void doWork();ConcreteProduct public class StudentWork implements Work public void doWork()System.out.println(?*?!);public class TeacherWork implements Work public v

4、oid doWork()System.out.println(?!);Creator public interface IWorkFactory Work get*ork();Concre*eCreator pu*lic class StudentWorkFactory implements IWorkFactory 4 Java?Java?public Work getWork()*eturn new StudentWork();public class TeacherWorkFactory implements IWorkFactory public Work getWork()retur

5、n new TeacherWork();Test public class Test public static void m*in(Strin*args)IWorkFactory studentWorkFactory=new StudentWorkFactory();studentWorkFactory.getWork().d*Work();IWorkFactory teacherWorkFactory*new TeacherWorkFactory();teacherWorkFactory.g*tWork().*oWork();result?!?!1.1.2?1.?*?2.?3.?*4*?*

6、?5 Java?Java?1.Ab*tractFactory?2.ConcreteFactory?*.AbstractProduct?4.ConcreteProdu*t?*?*bstractProduct?5.Client?AbstractFactory?AbstractProduc*?*bstractFactory public interface IAn*malFactory ICat createCat();IDog cre*teDog();ConcreteFactory 6 Java?Java?p*blic class BlackAnimalFactory implem*nts IAn

7、imalFactory public ICat createCat()retur*new BlackCat();public IDog createDog()return new BlackDog();public class WhiteAnimalFac*ory imp*ements IAnimalFactory public ICat createCat()return new WhiteCat();public IDog cre*teDog()return new WhiteDog();Abstrac*Product public interface ICat void eat();pu

8、blic interface IDog void eat();Concrete*roduct public class Black*at implements ICat public void eat()System.out.println(The bl*ck cat is eating!);7 Java?Java?public class WhiteCat implements*Cat public void eat()Sy*tem.out.prin*ln(The w*ite cat is eating!*);public class BlackDog implements IDog pub

9、lic void eat()System.out.println(The black dog is eating);public class WhiteDog implements IDog public void eat()System.out.println(The white dog is eat*ng!);Client public static void main(String args)IAnimalFactory blackAnimalFa*tory=new BlackAnimalFactory();ICat blackCat=blackAnimalFactory.createC

10、at();blackCat.eat();IDog blackD*g=blackAnimalFactory.createDog();blackDog.eat();IAnimalFactory whiteAnimalF*ctory=new WhiteAnimalFactory();ICat whiteCat=whiteAnimalFactory.createCat();whiteCat.eat();IDog*hiteDog=whiteAnimalFactory.createDog();whiteDog.eat();res*lt The bla*k cat is eating!Th*black do

11、g is eatin*!The white cat is eating!8 Java?Java?The white dog is*ating!1.1.3?*?*?1.?*.?*?*?1.Builder?Product?2.ConcreteBuilder?Buil*er?*?3.Director?Builder?4.Product?ConcreteBuilder?9 Java?Java?Buil*er public interface PersonBuilder void buildHead();v*id buildBody();void buildFoot()*Person buildPers

12、on();ConcreteBuilder public class ManBuilder implements PersonB*ilder Person person;public ManBuilder()person=ne*Man();publ*c void build*ody()perso*.setBody(?);10 Java?Java?public void buildFoot()person.setFo*t(?);public void buildHead()pers*n.setHead(?*?);*ublic Person buildPerson()retur*person;Dir

13、*ctor public class PersonDirec*or public Person constructPerson(PersonBuilder pb)pb.buildHead();pb.buildBody();pb.buildFoot();return pb.buildPerson();Product public class Person private String head;private String body;private String foot;public String getH*ad()return head;public void setHead(String

14、hea*)this.head=head;public String getBody()11 Java?Java?return body;public void setBody(String body)this.b*dy=body;public String getFoot()return foot;public void setFoot(String foot)t*is.foot=foot;public class Man extends Person Test publ*c class Test public static void main(String ar*s)PersonDirect

15、or pd=new PersonDirector();Person person=pd.constructPerson(new ManBuilder();System*out.println(person.getBody();System.out.println(person.getFoot();System.out.println(person.getHead();result?*?*?1.1.4?*?*?1.?*?12 Java?Java?2.?Singleton?Instance?Instance?*?Singleton public class Singleton private st

16、atic Singleton sing;private Singleton()public st*tic Singleton get*nstance()if(sing=null)sing=new Singleto*();return sing;Test public class Test public static void*ain(*tring args)Singleton sing=Singleton.getInstance();Singleton si*g2=Singleton.getI*stance();System.out.println(sing);System.out.pr*nt

17、ln(sing2);result 13 Java?Java?singleton.Singleton1c78e57 singleton.Singleton1c78e57 1.1.5?1.?*?2.?3.?*?4.?1.Prototype?2.ConcretePrototype?3.Client?*?C:UsersfulinA ppDataLocalTemp404683imagesPrototy pe.jpg?Prototype 14 Java?Java?public class Prototype implements Cloneable private String name;public v

18、oid setName(String name)this.name=name;public String getName()return this.name;public Object clone()try return super.clone();catch(Exception e)e.printStackTrace();return null;ConcretePrototype publ*c class ConcretePrototype extend*Prototype public ConcretePrototype(String name)setName(name);Client p

19、ublic clas*Test public static void main(String args)Prototype pro=new ConcretePrototy*e(prototype);Prototype pro2=(Prototype)pro.clone();*ystem.out.println(pro.getName()*;System.out.println(pro2.getName();result prototype 15 Java?Java?prototype 1.2?Adapter*?*Bridge(?*)Composite(?)Decorator(?*?)Facad

20、e(?)Flyweight(?)Proxy(?)1.2.1?Adapter?*?*?1.?*?2.?*?*.?Adapter?1.Target?Client?2.Client?Target?3.Adapt*e?4.Adapter?Adaptee?Target?16 Java?Java?C:UsersfulinA ppDataLocalTemp404683imagesA dapter.jpg?Target public interface Target void adapteeMethod();void adapterMethod();Adaptee public class Adaptee p

21、ublic void adapteeMethod()Syste*.out.p*intln(Adaptee method!);Adapt*r public clas*Adapter implement*Target private Adap*ee adaptee;public Adapter(Adaptee adaptee)this.adapte*=adaptee;public void adapteeMethod()adaptee.adapteeMethod();public void adapterMethod()*ystem.out.println(Adapter method!);17

22、Java?Java?Client public cla*s Test public stati*void main(String args)Target target=new Adapter(new Adaptee();tar*et.adapteeMethod();target.adapterM*thod();result Adaptee method!Adapter method!1.2.2?*?1.?*?*?2.?*?Bridge?3.?4.?*?5.*?*?1.Abstraction?18 Java?Java?Implementor?2.RefinedAbstraction?Abstra

23、ction?3.Implementor?Ab*traction?*?Implementor?Abstraction?4.ConcreteImplementor*?Implementor?C:UsersfulinA ppDataLocalTemp404683imagesBridge.jpg?Abstr*ction public abstract class Person private Clothing clothing;pr*vate String type;public Clothing getClothing()return clothing;publi*void setClothing(

24、)this.clothing=*lothingFactory.getClothing();19 Java?Java?public void setType(String type)t*is.type=type;public String getType()return this.ty*e;public abstract void dress();RefinedAbstraction public class Man extends*erson public Man()setType(?);public void dress()Clothing clothing=get*lothing();cl

25、othing.personDressCloth(this);public class Lady extends Person public Lady()setTyp*(?);public void dress()Cloth*ng clothing=getClothing();c*othing.personDressCloth(this);Implemento*public abstract class Clothing public abstract void personDressC*oth(*erson person);ConcreteImplemento*public class*ack

26、*t extends Clothing 20 Java?Java?public void personDressCloth(Person person)System.out.println(person.getType()+?);public cl*ss Trouser extends Clothing public void personDressCloth(Person person)System.ou*.println(*erson.getType()+?);Test public class Te*t public s*atic void main(String args)Person

27、 man=new Man();Person lady=new Lady();Clothing jacket=new Ja*ket();Clot*ing trouser=new Trouser();jacket.personDressCloth(man);trouser.personDressCloth(man);j*cket.personDressCloth(lady);trouser.personDressCloth(lady);result?1.2.3?21 Java?Java?-?Composite?*?1.?-?*?2.?1.Component?Component?(?)?*?2.Le

28、af?3.Compos*te?*?Component?*?4.Client?Component?*?C:UsersfulinA ppDataLocalTemp404683imagesComposite.jpg?Component 22 Java?Java?p*blic abstract class Employer private String name;public void setName(String name)this.name=*ame;public String getName()return this.name;public abstract void add(Employer

29、employer*;public abstract void delete(Employer employer);public List employers;public void printInfo*)System.out.println(name);*ublic List getE*ployers()return this.employers;Leaf public class Programmer extends Employer public Programmer(String name)setNam*(name);employers=null;/?,?public v*id add(

30、Employer employer)public void delete(Employer employer)23 Java?Java?public class Pro*ectAssistant extends Employer public ProjectAss*stant(String name)setName(name);employers=*ull;/?,?public void add(Employer employer)public void delet*(Employer employer)Composite public class Project*anager extends

31、 E*ployer public ProjectManager(String name)setName(name);employers=new A*rayList();public void add(Employer employer)employers.add(employer);public void delete(Emplo*er employer)employers.remove(employer);Clie*t publ*c class Test public st*tic void main(String args)Employer pm=new ProjectManager(?)

32、;Emplo*er pa=new ProjectAssistant(?);Employer progra*mer1=new Programmer(?);Employer programmer2=new Programmer(?);pm.add(pa);/?pm.add(programmer2);/*?*?24 Java?Java?List ems=pm.getEm*loyers();for(Employer em:ems)System.out.println(em.getNam*();*result?1.2.4?Decorator?*?*?1.?*?2.?3.?1.Component?2.Co

33、ncreteComponent?3.Decorator?Component?Component?4.ConcreteDecorator?25 Java?Java?C:UsersfulinA ppDataLocalTemp404683imagesDecorator.jpg?Component public interface Person void eat();ConcreteComponent *ublic class M*n implements Person public void eat()System.out.println(?);*Decorator public abstrac*c

34、lass Decorator implements Perso*protected Person person*public void setPerson(Person person)this.person=person;public void eat()person.eat();ConcreteDec*rator publi*class ManDecoratorA extends Decorator 26 Java?Java?public void eat()super.eat();reEat();Sy*tem.out.println(ManDecoratorA?);public void

35、reEat()System.out.println(?);*public class ManDecoratorB extends Decorator*public void eat()super.eat();Syst*m.out.println(=);System.out.println(ManDecoratorB?);Test public class Test public st*tic void main(Strin*args)Man man=new Man();ManDecoratorA md1=new ManDecoratorA();ManDecoratorB md2=n*w Man

36、DecoratorB();md1.setPerson(man);md2.setPerson(md1);md2.eat();result?ManDecoratorA?=ManDecoratorB?1.2.5?27 Java?Java?Facade?*?1.?*?*?Fa*ade?*?facade?2.?facade?3.?facade?facade?1.Facade?2.Subsystemclasses?Facade?facade?*acade?28 Java?Java?C:UsersfulinA ppDataLocalTemp404683imagesFacade.jpg?Facade publ

37、i*class Facade ServiceA s*;ServiceB sb;ServiceC sc;public sa sb sc Facade()=new S*rviceAImpl();=new*erviceBImpl();=new ServiceCImpl();public void methodA()sa.methodA();sb.methodB();publi*void methodB()s*.methodB();sc.methodC();public void methodC()sc.methodC();sa.methodA();29 Java?Java?Subsystemclas

38、se*public*lass ServiceAImpl implements ServiceA public void methodA()System.out.println(?A);public class ServiceBImpl implements ServiceB public void methodB()System.out.println(?B);*public class ServiceCImpl implements ServiceC public void methodC()System.out.println(?*C);Test public class Test pub

39、lic static voi*main(String args)ServiceA sa=new ServiceAImpl();Ser*iceB sb=new ServiceBImpl();sa.metho*A();sb.methodB();System.out.println(=);/facade Facade facade=new Facade();facade.methodA();facade.methodB();resu*t?A 30 Java?Java?*?B=?A?B?B?C 1.2.6?Flyweight?1.?*?2.?3.?*?4.?*?5.?Flyweight?*?1.Fly

40、weight?flyweight?2.ConcreteFlyweight?Flyweight?Concrete*lyweight?ConcreteFlyweight?3.UnsharedConcreteFlyweigh*?Flywe*ght?Flyweight?*?Flyweight?UnsharedConcreteFlyweight?ConcreteFlyweight?4.Flyweigh*Factory 31 Java?Java?flywe*ght?flyweight?flywei*ht?Fl*weightFactory?*C:UsersfulinA ppDataLocalTemp4046

41、83imagesFly weight.jpg?Flyweight public int*rface Flyweight void a*tion(int arg);ConcreteFlyweight public class FlyweightImpl implements Flyweight public void action(int arg)/T*DO Auto-genera*ed method stub System.out.println(*?:+arg);FlyweightFactory public class Flyweigh*Factory private static Map

42、 flyweights=new HashMap();public FlyweightF*ctory(String arg)flyweights.put(arg,new FlyweightImpl();public static Flyweight getFly*eight(String key)if(flyweights.get(key)=null)flyweights.p*t(key,new FlyweightImpl();32 Java?Java?return flyweights.get(key);public static int g*tSize()retu*n flyweights.

43、size();Test public class Test public static v*id main(String args)/TODO Auto-generated method stub Flyweight fly1=Flyw*ightFact*ry.getFlyweight(*a);fly1.action(1);Flyweight fly*=FlyweightFactory.getF*yweight(a);System.out.println(fly1=fly2);Flyweight fl*3=FlyweightFactory.getFlywei*ht(b);fly3.action

44、(2);Flyweight fly4=Flyweigh*Factory.getF*yweight(c);fly4.action(3);Flyweigh*fly5=FlyweightFactory.getFlyweight(d);fly4.action(4);System.out.println(FlyweightFactory.getSize()*result?:1 true?:2*?:3?:4 4 1.2.7?33 Java?Java?1.?RemoteProxy?2.?*?VirtualProxy?*?3.?ProtectionProxy?4.?SmartReference?1.Proxy

45、?RealSubject?Subject?Proxy?Subject?*?Subject?*?*?2.RemoteProxy?*.VirtualProxy?4.ProtectionProxy?*?5.Subjec*?RealSubject?Proxy?RealSubject?*?Proxy?6.RealSubject*?Proxy?C:UsersfulinA ppDataLocalTemp404683imagesProxy.jpg?Proxy 34 Java?Java?public class ProxyObject implements Object Objec*obj;public Pro

46、xyObject()System.out.println(?);o*j=new ObjectImpl();public void ac*ion()System.out.p*intln(?);obj.action*);System.out.println(*?);Subject public interface Obje*t void action();RealSubject public class ObjectImpl implements Object pu*lic void action()System.out.println(=);System.out.println(=);Syste

47、m.out.pr*ntln(?);System.out.println(=);System.out.println(=);Test public class Test publi*static void main()Object ob*=new ProxyObject();obj.action();*result 35 Java?Java?=*=?=*=?1.3?Chain of Responsibility(?)Command(?)Interpreter(?)Iterator(?*?)Mediator(?)Memento(?)Observer(?)State(?*?)Strategy(?)T

48、emplateMethod(?)Vis*tor(?)1.3.1?*?*?.?1.?2.?*?3.?36 Java?Java?1.Handler?2.ConcreteHandler?*?*?*?3.Client?(ConcreteHandler)?C:UsersfulinA ppDataLocalTemp404683imagesCOR.jpg?Hand*er public interface RequestHandle void handleRequest(R*quest request);ConcreteHandler public class HRRequestHandle implemen

49、ts RequestHandle public void handleRequest(Request request)if(request instanceof DimissionRequest)System.out.println(?,?!);System.out.println(?*);37 Java?Java?public class PMRequestHandle implements RequestHandle Req*estHandle rh;public PMRequestHandle(RequestHandle*h)this.rh=rh;public void handle*equest(Request request)if(request instanceof AddMoneyRequest)System.out.println(?,?!*);else rh.handleRequest(request);public class TLRequestHandle implements RequestHandle RequestHandle rh;public TLRequestHandle(RequestHand*e rh)this.rh=rh;public void handleRequest(Request request)if(request instanc

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 应用文书 > 财经金融

本站为文档C TO C交易模式,本站只提供存储空间、用户上传的文档直接被用户下载,本站只是中间服务平台,本站所有文档下载所得的收益归上传人(含作者)所有。本站仅对用户上传内容的表现方式做保护处理,对上载内容本身不做任何修改或编辑。若文档所含内容侵犯了您的版权或隐私,请立即通知淘文阁网,我们立即给予删除!客服QQ:136780468 微信:18945177775 电话:18904686070

工信部备案号:黑ICP备15003705号© 2020-2023 www.taowenge.com 淘文阁