基本的業務邏輯的編寫並非難事,做爲一個合格的程序員可以編寫出易擴展,可維護,高內聚,低耦合的代碼纔是真正的能力。因此從今天開始會陸陸續續的研究一下設計模式。設計模式並非針對編程語言,而是考察的編程的思想。java
工廠模式又細分爲三種:簡單工廠模式+工廠方法模式+抽象工廠模式程序員
簡單工廠模式:
簡單工廠模式是咱們上面的情景再現的狀況,簡單工廠就是說全部的細節都是依靠一個工廠統一完成,這種模式下工廠的壓力特別大。spring
工廠方法模式:
工廠方法模式在上面情景的基礎上將工廠進行了更改優化,以前工廠是一個工廠生產一年四季的衣服,如今抽象一個工廠類,在有四個工廠是該抽象工廠的具體實現類,這四個工廠具體分別負責生產春夏秋冬衣服。這樣仍然是工廠模式,只不過相對簡單工廠模式已經減輕了工廠的壓力了。編程
抽象工廠模式:
抽象工廠模式在抽象方法模式的基礎上有進行了強化,抽象方法中是一個具體工廠的實現類負責生產一種類型的衣服,而在抽象工廠模式下一個具體的工廠的實現類能夠生產多個類別的衣服。這樣更加的減輕服裝廠的壓力了。設計模式
這裏咱們就實現簡單工廠模式的代碼,其餘兩種的模式知識在簡單工廠模式的基礎上進行工廠類的強化markdown
服裝抽象類(Clothes)編程語言
package zhangxinhua.JavaDesignDetails.工廠模式.features;
public abstract class Clothes
{
//袖口的數量
private int wristband=2;
//領口的數量
private int neckline=1;
//穿衣方式
public void hold()
{
System.out.println("個人穿衣方式是從領口");
}
public int getWristband()
{
return wristband;
}
public void setWristband(int wristband)
{
this.wristband = wristband;
}
public int getNeckline()
{
return neckline;
}
public void setNeckline(int neckline)
{
this.neckline = neckline;
}
}
具體服裝類一(AutumnCloth)ide
package zhangxinhua.JavaDesignDetails.工廠模式.special;
import zhangxinhua.JavaDesignDetails.工廠模式.features.Clothes;
public class AutumnCloth extends Clothes {
@Override
public void hold()
{
System.out.println("我是秋裝,我要倒着穿");
}
}
具體服裝類二(SpringCloth)學習
package zhangxinhua.JavaDesignDetails.工廠模式.special;
import zhangxinhua.JavaDesignDetails.工廠模式.features.Clothes;
public class SpringCloth extends Clothes {
@Override
public void hold()
{
System.out.println("我是春裝,我要從領口穿衣");
}
}
具體服裝類三(SummerCloth)優化
package zhangxinhua.JavaDesignDetails.工廠模式.special;
import zhangxinhua.JavaDesignDetails.工廠模式.features.Clothes;
public class SummerCloth extends Clothes {
@Override
public void hold()
{
System.out.println("我是夏裝,我要從懷口穿");
}
}
工廠類(Clothfactory)
package zhangxinhua.JavaDesignDetails.工廠模式.簡單工廠模式.factory;
import zhangxinhua.JavaDesignDetails.工廠模式.features.Clothes;
import zhangxinhua.JavaDesignDetails.工廠模式.special.AutumnCloth;
import zhangxinhua.JavaDesignDetails.工廠模式.special.SpringCloth;
import zhangxinhua.JavaDesignDetails.工廠模式.special.SummerCloth;
public class ClothFactory {
public static Clothes createClothes(String type) throws Exception
{
Clothes clothes=null;
switch (type)
{
case "spring":
clothes=new SpringCloth();
break;
case "summer":
clothes=new SummerCloth();
break;
case "autumn":
clothes=new AutumnCloth();
break;
default:
throw new Exception("目前不支持你指定的類型");
}
return clothes;
}
}
客戶端調用(client)
package zhangxinhua.JavaDesignDetails.工廠模式.簡單工廠模式.client;
import zhangxinhua.JavaDesignDetails.工廠模式.features.Clothes;
import zhangxinhua.JavaDesignDetails.工廠模式.簡單工廠模式.factory.ClothFactory;
public class client
{
public static void main(String[] args) throws Exception
{
System.out.println("工廠你給我一套春裝");
Clothes spring = ClothFactory.createClothes("spring");
spring.hold();
System.out.println("********************");
System.out.println("工廠你給我一套秋裝");
Clothes autum = ClothFactory.createClothes("autumn");
autum.hold();
System.out.println("********************");
System.out.println("工廠你給我一套夏裝");
Clothes summer = ClothFactory.createClothes("summer");
summer.hold();
System.out.println("********************");
}
}
效果展現
有須要源碼的,或者有不理解,歡迎掃描左側二維碼,進羣交流討論。