来源:http://www.bjsxt.com/
一、S03E229_01【GOF23设计模式】_简单工厂模式详解、面向对象设计原则、开闭原则、依赖反转原则、迪米特法则
没有工厂模式的情况
package com.test.factory.simple;
public interface Car {
public void run();
}
package com.test.factory.simple;
public class Audi implements Car{
@Override
public void run() {
System.out.println("奥迪在跑");
}
}
package com.test.factory.simple;
public class Byd implements Car{
@Override
public void run() {
System.out.println("比亚迪在跑");
}
}
package com.test.factory.simple;
/**
* 测试没有工厂模式的情况下
*/
public class Client01 { //调用者 依赖Audi、Byd
public static void main(String[] args) {
Car c1 = new Audi();
Car c2 = new Byd();
c1.run();
c2.run();
}
}
简单工厂模式的情况
package com.test.factory.simple;
public class CarFactory {
public static Car createCar(String type){
if("奥迪".equals(type)){
return new Audi();
}else if("比亚迪".equals(type)){
return new Byd();
}else{
return null;
}
}
}
package com.test.factory.simple;
/**
* 简单工厂情况下
*/
public class Client02 { //调用者 不依赖Audi、Byd
public static void main(String[] args) {
Car c1 = CarFactory.createCar("奥迪");
Car c2 = CarFactory.createCar("比亚迪");
c1.run();
c2.run();
}
}
或者
package com.test.factory.simple;
public class CarFactory2 {
public static Car createAudi(){
return new Audi();
}
public static Car createByd(){
return new Byd();
}
}
package com.test.factory.simple;
/**
* 简单工厂情况下
*/
public class Client03 { //调用者 不依赖Audi、Byd
public static void main(String[] args) {
Car c1 = CarFactory2.createAudi();
Car c2 = CarFactory2.createByd();
c1.run();
c2.run();
}
}
二、S03E230_01【GOF23设计模式】_工厂方法模式详解
package com.test.factory.factorymethod;
public interface Car {
public void run();
}
package com.test.factory.factorymethod;
public class Audi implements Car{
@Override
public void run() {
System.out.println("奥迪在跑");
}
}
package com.test.factory.factorymethod;
public class Byd implements Car{
@Override
public void run() {
System.out.println("比亚迪在跑");
}
}
package com.test.factory.factorymethod;
public interface CarFactory {
Car createCar();
}
package com.test.factory.factorymethod;
public class AudiFactory implements CarFactory{
@Override
public Car createCar() {
return new Audi();
}
}
package com.test.factory.factorymethod;
public class BydFactory implements CarFactory{
@Override
public Car createCar() {
return new Byd();
}
}
package com.test.factory.factorymethod;
public class Client {
public static void main(String[] args) {
Car c1 = new AudiFactory().createCar();
Car c2 = new BydFactory().createCar();
c1.run();
c2.run();
}
}
三、S03E231_01【GOF23设计模式】_抽象工厂模式详解
package com.test.factory.abstractfactory;
public interface Engine {
void run();
void start();
}
class LuxuryEngine implements Engine{
@Override
public void run() {
System.out.println("转得快");
}
@Override
public void start() {
System.out.println("启动快!可以自动启停");
}
}
class LowEngine implements Engine{
@Override
public void run() {
System.out.println("转得慢");
}
@Override
public void start() {
System.out.println("启动慢!");
}
}
package com.test.factory.abstractfactory;
public interface Seat {
void massage();
}
class LuxurySeat implements Seat{
@Override
public void massage() {
System.out.println("可以自动按摩");
}
}
class LowSeat implements Seat{
@Override
public void massage() {
System.out.println("不能按摩");
}
}
package com.test.factory.abstractfactory;
public interface Tyre {
void revolve();
}
class LuxuryTyre implements Tyre{
@Override
public void revolve() {
System.out.println("旋转不磨损");
}
}
class LowTyre implements Tyre{
@Override
public void revolve() {
System.out.println("旋转磨损快");
}
}
package com.test.factory.abstractfactory;
public interface CarFactory {
Engine createEngine();
Seat createSeat();
Tyre createTyre();
}
package com.test.factory.abstractfactory;
public class LuxuryFactory implements CarFactory{
@Override
public Engine createEngine() {
return new LuxuryEngine();
}
@Override
public Seat createSeat() {
return new LuxurySeat();
}
@Override
public Tyre createTyre() {
return new LuxuryTyre();
}
}
package com.test.factory.abstractfactory;
public class LowFactory implements CarFactory{
@Override
public Engine createEngine() {
return new LowEngine();
}
@Override
public Seat createSeat() {
return new LowSeat();
}
@Override
public Tyre createTyre() {
return new LowTyre();
}
}
package com.test.factory.abstractfactory;
public class Client {
public static void main(String[] args) {
CarFactory factory = new LuxuryFactory();
Engine e = factory.createEngine();
e.run();
e.start();
}
}