1. 程式人生 > >java常見的幾種設計模式

java常見的幾種設計模式

設計模式 單例 餓漢式 懶漢式

設計模式

1、概述

1)設計模式(Design pattern):是一套被反復使用、多數人知曉的、經過分類編目的、代碼設計經驗的總結;

2)分類:

創建型模式(創建對象的): 單例模式、抽象工廠模式、建造者模式、工廠模式、原型模式。

行為型模式(對象的功能): 適配器模式、橋接模式、裝飾模式、組合模式、外觀模式、享元模式、代理模式。

結構型模式(對象的組成): 模版方法模式、命令模式、叠代器模式、觀察者模式、中介者模式、備忘錄模式、解釋器模式、狀態模式、策略模式、職責鏈模式、訪問者模式。

2、簡單工廠模式

1)概述:又叫靜態工廠方法模式,它定義一個具體的工廠類負責創建一些類的實例

2)實現:

                        public abstract class Animal {
				public abstract void eat();
			}
			
			public class Cat extends Animal{
				@Override
				public void eat() {
					System.out.println("貓吃魚");
				}
			}
			
			public class Dog extends Animal{
				@Override
				public void eat() {
					System.out.println("狗吃肉");
				}
			}
			
			public class AnimalFactory {
				public static Animal get(String name){
					if(name.equals("Dog")){
						return new Dog();
					}else if(name.equals("Cat")){
						return new Cat();
					}else{
						return null;
					}
				}
			}
			
			public class Test {
				public static void main(String[] args) {
					AnimalFactory.get("Cat").eat();
					AnimalFactory.get("Dog").eat();
				}
			}


3、工廠方法模式

1)概述:工廠方法模式中抽象工廠類負責定義創建對象的接口,具體對象的創建工作由繼承抽象工廠的具體類實現

2)實現:

public interface AnimalFactory {

public abstract Animal get() ;

}

--------------------------

public class DogFactory implements AnimalFactory {

@Override

public Animal get() {

return new Dog();

}

}

-------------------------------------

AnimalFactory af = new DogFactory() ;

Animal animal = af.get() ;

animal.eat() ;

4、單例模式

概述:保證類在內存中只有一個對象

1)餓漢式

                        public class Student {
				private final static Student s = new Student();
				private Student() {
					super();
				}
				public static Student get(){
					return s;
				}
			}


2)懶漢式

                        public class Teacher {
				private static Teacher t = null;
				public Teacher() {
					super();
				}
				public static Teacher get(){
					if(t == null){
						t = new Teacher();
					}
					return t;
				}
			}


3)Runtime類(單例模式的實現)

概述:每個Java應用程序都有一個Runtime類實例,使應用程序能夠與其運行的環境相連接。可以通過getRuntime方法獲取當前運行時對象。

public Process exec(String command)執行Dos命令

5、模版設計模式

1)概述:定義一個算法的骨架,而將具體的算法延遲到子類中來實現

2)實現:

                        public abstract class GetTime {
				public long getTime(){
					long startTime = System.currentTimeMillis();
					work();	//一些耗時操作
					long endTime = System.currentTimeMillis();
					return endTime - startTime;
				}
				public abstract void work();
			}
			
			public class ExtendsGet extends GetTime{
				@Override
				public void work() {
					for(int i = 0;i < 1000;i ++){
						System.out.print(i);
					}
					System.out.println();
				}
			}
			
			public class Test {
				public static void main(String[] args) {
					ExtendsGet eg = new ExtendsGet();
					long time = eg.getTime();
					System.out.println(time);
				}
			}


6、裝飾模式

1)概述:使用被裝飾類的一個子類的實例,在客戶端將這個子類的實例交給裝飾類。是繼承的替代方案

2)實現:(IO流中將File包裝成一個高效字符流)

                        public interface Phone {
				void call();
			}
			
			public class CallPhone implements Phone{
				@Override
				public void call() {
					System.out.println("打電話");
				}
			}
			
			public class PhoneDecorate extends CallPhone{
				private Phone phone;
				public PhoneDecorate(Phone phone) {
					super();
					this.phone = phone;
				}
				@Override
				public void call() {
					phone.call();
				}
			}
			
			public class MusicPhone extends PhoneDecorate{
				public MusicPhone(Phone phone) {
					super(phone);
				}
				@Override
				public void call() {
					super.call();
					System.out.println("聽音樂");
				}
			}
			
			public class VideoPhone extends PhoneDecorate{
				public VideoPhone(Phone phone) {
					super(phone);
				}
				@Override
				public void call() {
					super.call();
					System.out.println("看視頻");
				}
			}
			
			public class Test {
				public static void main(String[] args) {
					new VideoPhone(new MusicPhone(new CallPhone())).call();
				}
			}


7、觀察者模式

1)概述:一種一對多的依賴關系,讓多個觀察者對象同時監聽一個主題對象;

2)實現:

                        public class WorkMan {
    				String Manname;
				public WorkMan(String manname) {
					super();
					Manname = manname;
				}
			}
			
			public class Job {
				String jobname;
				int jobmoney;
				public Job(String jobname, int jobmoney) {
					super();
					this.jobname = jobname;
					this.jobmoney = jobmoney;
				}
			}
			
			public class Headhunter {
				private ArrayList<Job> jobal = new ArrayList<>();
				private ArrayList<WorkMan> manal = new ArrayList<>();
				
				public void addWorkMan(WorkMan wm){
					manal.add(wm);
				}
				public void delWorkMan(WorkMan wm){
					manal.remove(wm);
				}
				public void addJob(Job job){
					jobal.add(job);
					sent(job);
				}
				public void delJob(Job job){
					jobal.remove(job);
				}
				private void sent(Job job) {
					for(WorkMan wm : manal){
						System.out.println(wm.Manname + ",你好!有份" + job.jobname 
								+ "的工作,薪資" + job.jobmoney + "元,有意者請及時面試");
					}
				}
			}
			
			public class Test {
				public static void main(String[] args) {
					Headhunter headhunter = new Headhunter();
					WorkMan wm1 = new WorkMan("張三");
					WorkMan wm2 = new WorkMan("李四");
					WorkMan wm3 = new WorkMan("王五");
					headhunter.addWorkMan(wm1);
					headhunter.addWorkMan(wm2);
					headhunter.addWorkMan(wm3);
					Job job1 = new Job("司機", 8000);
					Job job2 = new Job("保姆", 9000);
					Job job3 = new Job("清潔員", 6000);
					headhunter.addJob(job1);
					headhunter.addJob(job2);
					headhunter.addJob(job3);
					headhunter.delWorkMan(wm3);
					headhunter.delJob(job3);
					headhunter.addWorkMan(new WorkMan("趙六"));
					headhunter.addJob(new Job("保安", 7000));
				}
			}


java常見的幾種設計模式