简单工厂
简单工厂是一个工厂可以生产各种产品,用户只需要得到这个工厂就可获得需要的产品
优点:对用户友好
缺点:工厂分工不明确导致逻辑复杂
└── simple
├── SimpleFactory.java
└── SimpleFactoryTest.java
package com.xh.pattern.factory.simple;
/**
* Created by root on 3/15/18.
*/
public class SimpleFactory {
public void produceComputer(String computerBrands) {
switch (computerBrands) {
case "apple":
System.out.println("MAC COMPUTER");
break;
case "thinkpad":
System.out.println("thinkpad COMPUTER");
break;
case "asus":
System.out.println("asus COMPUTER");
break;
default:
System.out.println("生产不了");
}
}
}
package com.xh.pattern.factory.simple;
/**
* Created by root on 3/15/18.
*/
public class SimpleFactoryTest {
public static void main(String[] args) {
SimpleFactory simpleFactory = new SimpleFactory();
simpleFactory.produceComputer("asus");
}
}
工厂方法
工厂方法是为了弥补简单工厂的分工不明改进的
优点:工厂分工明确,逻辑简单
缺点:用户需要找到对应的工厂才能获得产品,对用户不友好
├── function
│ ├── AppleFactory.java
│ ├── AsusFactory.java
│ ├── Factory.java
│ ├── FuncFactoryTest.java
│ └── ThinkPadFactory.java
package com.xh.pattern.factory.function;
/**
* Created by root on 3/15/18.
*/
public interface Factory {
void produceComputer();
}
package com.xh.pattern.factory.function;
/**
* Created by root on 3/15/18.
*/
public class AppleFactory implements Factory {
@Override
public void produceComputer() {
System.out.println("MAC COMPUTER");
}
}
package com.xh.pattern.factory.function;
/**
* Created by root on 3/15/18.
*/
public class AsusFactory implements Factory {
@Override
public void produceComputer() {
System.out.println("asus COMPUTER");
}
}
package com.xh.pattern.factory.function;
/**
* Created by root on 3/15/18.
*/
public class ThinkPadFactory implements Factory {
@Override
public void produceComputer() {
System.out.println("thinkpad COMPUTER");
}
}
package com.xh.pattern.factory.function;
/**
* Created by root on 3/15/18.
*/
public class FuncFactoryTest {
public static void main(String[] args) {
Factory factory=new AppleFactory();
factory.produceComputer();
}
}
抽象工厂
抽象工厂是为了弥补工厂方法的对用户不友好改进的
优点:工厂分工明确,逻辑简单,对用户友好
缺点:???
├── abstr
│ ├── AppleFactory.java
│ ├── AsusFactory.java
│ ├── DefaultFactory.java
│ ├── Factory.java
│ ├── FuncFactoryTest.java
│ └── ThinkPadFactory.java
package com.xh.pattern.factory.abstr;
/**
* Created by root on 3/15/18.
*/
public abstract class Factory {
abstract void produceComputer();
public void produceComputer(String computerBrands) {
switch (computerBrands) {
case "apple":
new AppleFactory().produceComputer();
break;
case "thinkpad":
new ThinkPadFactory().produceComputer();
break;
case "asus":
new AsusFactory().produceComputer();
break;
default:
System.out.println("生产不了");
}
}
}
package com.xh.pattern.factory.abstr;
/**
* Created by root on 3/15/18.
*/
public class AppleFactory extends Factory {
@Override
public void produceComputer() {
System.out.println("MAC COMPUTER");
}
}
...
package com.xh.pattern.factory.abstr;
/**
* Created by root on 3/15/18.
*/
public class DefaultFactory extends Factory {
@Override
void produceComputer() {
new AppleFactory().produceComputer();
}
}
package com.xh.pattern.factory.abstr;
/**
* Created by root on 3/15/18.
*/
public class FuncFactoryTest {
public static void main(String[] args) {
DefaultFactory factory = new DefaultFactory();
factory.produceComputer();
factory.produceComputer("asus");
}
}