java_JAVA常用设计模式.pdf
《java_JAVA常用设计模式.pdf》由会员分享,可在线阅读,更多相关《java_JAVA常用设计模式.pdf(13页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、 Java?Lesson 1?Factory-?.?,?.?.?,?.?Factory-?MM?,?MM?,?,?MM?,?.?Factory?Factory-?public class Factory public static Sample creator(int which)/getClass?Sample?.if(which=1)return new SampleA();else if(which=2)return new SampleB();?FACTORY METHOD-?,?,?,?,?.?FACTORY METHOD-?MM?,?MM?,?,?Factory Method?,?
2、MM?,?,?,?MM?.?FACTORY METHOD-?public abstract class Factory public abstract Sample creator();public abstract Sample2 creator(String name);public class SimpleFactory extends Factory public Sample creator()return new SampleA public Sample2 creator(String name)return new Sample2A public class BombFacto
3、ry extends Factory public Sample creator()return new SampleB public Sample2 creator(String name)return new Sample2B?Builder?Builder-?,?.?,?.?.?Builder?Builder-?MM?,?MM,?,?,?,?MM?,?,?MM?,?builder.(?)?Builder?Builder-?public interface Builder /?A?void buildPartA();/?B?void buildPartB();/?C?void buildP
4、artC();/?(?)/?,?Director?./?Product getResult();public class Director private Builder builder;public Director(Builder builder)this.builder=builder;/?partA partB partC?/?public void construct()builder.buildPartA();builder.buildPartB();builder.buildPartC();?PROTOTYPE?PROTOTYPE-?,?.?,?,?.?.?PROTOTYPE?P
5、ROTOTYPE-?MM?QQ?,?,?,?copy?QQ?,?prototype?.(100?,?)?PROTOTYPE?PROTOTYPE-?public abstract class AbstractSpoon implements Cloneable String spoonName;public void setSpoonName(String spoonName)this.spoonName=spoonName;public String getSpoonName()return this.spoonName;public Object clone()Object object=n
6、ull;try object=super.clone();catch(CloneNotSupportedException exception)System.err.println(AbstractSpoon is not Cloneable);return object;public class SoupSpoon extends AbstractSpoon public SoupSpoon()setSpoonName(Soup Spoon);public class SaladSpoon extends AbstractSpoon public SaladSpoon()setSpoonNa
7、me(Salad Spoon);?Singleton-?,?.?.?Singleton-?6?,?,?Sigleton,?,?,?(?,?)?Singleton-?public class Singleton private static Singleton instance=null;public static synchronized Singleton getInstance()/?,?,?/?,?!if(instance=null)instance=new Singleton();return instance;?Adapter?Adapter-?,?.?.?Adapter?Adapt
8、er-?Sarah,?,?,?,?kent?,?Sarah?Adapter,?Sarah?(?)?Adapter?Adapter-?public interface IRoundPeg public void insertIntoHole(String msg);public interface ISquarePeg public void insert(String str);public class PegAdapter implements IRoundPeg,ISquarePeg private RoundPeg roundPeg;private SquarePeg squarePeg
9、;/?public PegAdapter(RoundPeg peg)this.roundPeg=peg;/?public PegAdapter(SquarePeg peg)(this.squarePeg=peg;)public void insert(String str)roundPeg.insertIntoHole(str);public void insertIntoHole(String str)SquarePeg.insert(str);?Bridge?Bridge-?,?,?,?/?,?.?Bridge?Bridge-?MM,?,?MM,?;?MM?,?,?MM?,?.?MM?,?
10、BRIDGE?Bridge?Bridge-?public abstract class Coffee CoffeeImp coffeeImp;public void setCoffeeImp()this.CoffeeImp=CoffeeImpSingleton.getTheCoffeImp();public SodaImp getCoffeeImp()return this.CoffeeImp;public abstract void pourCoffee();public abstract class CoffeeImp public abstract void pourCoffeeImp(
11、);/bridge public class CoffeeImpSingleton private static CoffeeImp coffeeImp;public CoffeeImpSingleton(CoffeeImp coffeeImpIn)this.coffeeImp=coffeeImpIn;public static CoffeeImp getTheCoffeeImp()return coffeeImp;?Composite?Composite-?,?.?.?.?.?Composite?Composite-?Mary?.?,?.?,?,?,?.?T?,?,?,?,?,?.?,?,?
12、.?,T?,?,?,?.,MM?Composite?,?Composite?Composite-?public abstract class Equipment private String name;/?public abstract double netPrice();/?public abstract double discountPrice();/?public boolean add(Equipment equipment)return false;/?public boolean remove(Equipment equipment)return false;/?,?.public
13、 Iterator iter()return null;public Equipment(final String name)this.name=name;public class Disk extends Equipment abstract class CompositeEquipment extends Equipment?DECORATOR?DECORATOR-?,?,?.?,?.?.?DECORATOR?DECORATOR-?Mary?Sarly?,?,?,?,?,?Fita,?(?MM?),?Mike?,?Decorator,?,?,?DECORATOR?DECORATOR-?pu
14、blic interface Work public void insert();public class SquarePeg implements Work public void insert()System.out.println(?);public class Decorator implements Work private Work work;/?List?private ArrayList others=new ArrayList();/?new?,?Work?;public Decorator(Work work)this.work=work;others.add(?);oth
15、ers.add(?);public void insert()newMethod();/?,?insert?,?public void newMethod()otherMethod();work.insert();public void otherMethod()ListIterator listIterator=others.listIterator();while(listIterator.hasNext()System.out.println(String)(listIterator.next()+?);?Facade?Facade-?.?,?.?,?,?.?.?Facade?Facad
16、e-?Nikon?,?,?,?,?MM?,?.?Facade?,?,?,?,?MM?.?Facade?Facade-?public class DBCompare String sql=SELECT*FROM WHERE =?;try Mysql msql=new mysql(sql);prep.setString(1,);rset=prep.executeQuery();if(rset.next()System.out.println(rset.getString(column name);catch(SException e)e.printStackTrace();finally mysq
17、l.close();mysql=null;?FLYWEIGHT?FLYWEIGHT-?FLYWEIGHT?.?.?.?,?.?.?,?.?,?.?,?.?.?FLYWEIGHT?FLYWEIGHT-?MM?,?,?,?,?,?,?MM?,?.?Flyweight,MM?,?.?FLYWEIGHT?FLYWEIGHT-?public class CD private String title;private int year;private Artist artist;public String getTitle()public int getYear()public Artist getArt
18、ist()return title;return year;return artist;public void setTitle(String t)title=t;public void setYear(int y)year=y;public void setArtist(Artist a)artist=a;public class Artist /?private String name;/note that Artist is immutable.String getName()return name;name=n;public class ArtistFactory Artist(Str
19、ing n)Hashtable pool=new Hashtable();Artist getArtist(String key)Artist result;result=(Artist)pool.get(key);/?Artist if(result=null)result=new Artist(key);pool.put(key,result);return result;?PROXY?PROXY-?,?.?.?,?,?.?.?,?,?,?.?PROXY?PROXY-?MM?,?hi,?,?,?,?Proxy?,?,?,?,?.?PROXY?PROXY-?public class Foru
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java_JAVA 常用 设计 模式
限制150内