简单工厂模式
public class FactoryPatternTest {
public static void main(String[] args) {
Shape rectangle = ShapeFactory.getShape("Rectangle");
rectangle.draw();
Shape square = ShapeFactory.getShape("Square");
square.draw();
Shape circle = ShapeFactory.getShape("Circle");
circle.draw();
}
}
interface Shape {
void draw();
}
class Rectangle implements Shape {
@Override
public void draw() {
System.out.println("Rectangle.draw");
}
}
class Square implements Shape {
@Override
public void draw() {
System.out.println("Square.draw");
}
}
class Circle implements Shape {
@Override
public void draw() {
System.out.println("Circle.draw");
}
}
class ShapeFactory {
public static Shape getShape(String type) {
if (null == type) {
return null;
}
switch (type) {
case "Rectangle":
return new Rectangle();
case "Square":
return new Square();
case "Circle":
return new Circle();
default:
return null;
}
}
}
工厂模式
public class FactoryMethodTest {
public static void main(String[] args) {
PhoneFactory miPhoneFactory = new MiPhoneFactory();
miPhoneFactory.makePhone().callPhone();
PhoneFactory iPhoneFactory = new IPhoneFactory();
iPhoneFactory.makePhone().callPhone();
}
}
interface Phone {
void callPhone();
}
class MiPhone implements Phone{
@Override
public void callPhone() {
System.out.println("MiPhone.callPhone");
}
}
class IPhone implements Phone{
@Override
public void callPhone() {
System.out.println("IPhone.callPhone");
}
}
interface PhoneFactory {
Phone makePhone();
}
class MiPhoneFactory implements PhoneFactory {
@Override
public Phone makePhone() {
return new MiPhone();
}
}
class IPhoneFactory implements PhoneFactory {
@Override
public Phone makePhone() {
return new IPhone();
}
}
抽象工厂
public class AbstractFactoryPatternTest {
public static void main(String[] args) {
IHouseholdElectricFactory factory = new MeideHouseholdElectricFactory();
factory.createFridge().closeStorage();
factory.createWasher().wash();
}
}
interface IFridge {
void closeStorage();
}
interface IWasher {
void wash();
}
class MeideFridge implements IFridge {
@Override
public void closeStorage() {
System.out.println("美的冰箱");
}
}
class MeideWasher implements IWasher {
@Override
public void wash() {
System.out.println("美的洗衣机");
}
}
class GeliFridge implements IFridge {
@Override
public void closeStorage() {
System.out.println("格力冰箱");
}
}
class GeliWasher implements IWasher {
@Override
public void wash() {
System.out.println("格力洗衣机");
}
}
interface IHouseholdElectricFactory {
IFridge createFridge();
IWasher createWasher();
}
class MeideHouseholdElectricFactory implements IHouseholdElectricFactory {
@Override
public IFridge createFridge() {
return new MeideFridge();
}
@Override
public IWasher createWasher() {
return new MeideWasher();
}
}
class GeliHouseholdElectricFactory implements IHouseholdElectricFactory {
@Override
public IFridge createFridge() {
return new GeliFridge();
}
@Override
public IWasher createWasher() {
return new GeliWasher();
}
}
- 简单工厂:由一个工厂根据传入的参数返回不同的对象
- 工厂方法:将工厂抽象出来,一个产品对应一个具体的工厂类
- 抽象工厂:一个工厂生产一个产品族的一系列产品