简单工厂模式
定义一些类及抽象类
package com. learn. simpleFactory. model ;
public class Apple implements Fruit {
@Override
public void eatFruit ( ) {
System . out. println ( "吃了一个苹果" ) ;
}
@Override
public void buyFruit ( ) {
System . out. println ( "买了一个苹果" ) ;
}
package com. learn. simpleFactory. model ;
public class Banana implements Fruit {
@Override
public void eatFruit ( ) {
System . out. println ( "吃了一个香蕉" ) ;
}
@Override
public void buyFruit ( ) {
System . out. println ( "买了一个香蕉" ) ;
}
}
}
package com. learn. simpleFactory. model ;
public interface Fruit {
void eatFruit ( ) ;
void buyFruit ( ) ;
}
创建一个工厂类用来生产这两个对象的实例
package com. learn. simpleFactory. factory ;
import com. learn. simpleFactory. model. Apple ;
import com. learn. simpleFactory. model. Banana ;
import com. learn. simpleFactory. model. Fruit ;
public class FruitFactory {
public static Fruit getInstance ( String param) {
if ( "apple" . equals ( param) ) {
return new Apple ( ) ;
} else {
return new Banana ( ) ;
}
}
}
demo
package com. learn. simpleFactory. run ;
import com. learn. simpleFactory. factory. FruitFactory ;
import com. learn. simpleFactory. model. Fruit ;
public class App {
public static void main ( String [ ] args) {
Fruit apple = FruitFactory . getInstance ( "apple" ) ;
apple. eatFruit ( ) ;
apple. buyFruit ( ) ;
Fruit banana = FruitFactory . getInstance ( "banana" ) ;
banana. eatFruit ( ) ;
banana. buyFruit ( ) ;
}
}
简单工厂关系图
抽象工厂模式
简单工厂适用于解决单种类型的多个对象,对于多类型的对象应该使用抽象工厂模式,例:苹果和香蕉都有品牌,并且每个品牌都可以实现多种水果
定义水果类及抽象类
public interface Fruit {
void eatFruit ( ) ;
void buyFruit ( ) ;
}
package com. learn. absFactory. model ;
public class FSKApple implements Fruit {
@Override
public void eatFruit ( ) {
System . out. println ( "吃富士康苹果" ) ;
}
@Override
public void buyFruit ( ) {
System . out. println ( "买富士康苹果" ) ;
}
}
package com. learn. absFactory. model ;
public class FSKBanana implements Fruit {
@Override
public void eatFruit ( ) {
System . out. println ( "吃富士康香蕉" ) ;
}
@Override
public void buyFruit ( ) {
System . out. println ( "买富士康香蕉" ) ;
}
}
package com. learn. absFactory. model ;
public class HPGApple implements Fruit {
@Override
public void eatFruit ( ) {
System . out. println ( "吃红苹果苹果" ) ;
}
@Override
public void buyFruit ( ) {
System . out. println ( "买红苹果苹果" ) ;
}
}
package com. learn. absFactory. model ;
public class HPGBanana implements Fruit {
@Override
public void eatFruit ( ) {
System . out. println ( "吃红苹果香蕉" ) ;
}
@Override
public void buyFruit ( ) {
System . out. println ( "买红苹果香蕉" ) ;
}
}
创建一个抽象工厂类,作为超级工厂管理多个类型的工厂
package com. learn. absFactory. factory ;
import com. learn. absFactory. model. Fruit ;
public interface FruitFactory {
Fruit getAppleInstance ( ) ;
Fruit getBananaInstance ( ) ;
}
package com. learn. absFactory. factory ;
import com. learn. absFactory. model. FSKApple ;
import com. learn. absFactory. model. FSKBanana ;
import com. learn. absFactory. model. Fruit ;
public class FSKFactory implements FruitFactory {
@Override
public Fruit getAppleInstance ( ) {
return new FSKApple ( ) ;
}
@Override
public Fruit getBananaInstance ( ) {
return new FSKBanana ( ) ;
}
}
package com. learn. absFactory. factory ;
import com. learn. absFactory. model. Fruit ;
import com. learn. absFactory. model. HPGApple ;
import com. learn. absFactory. model. HPGBanana ;
public class HPGFactory implements FruitFactory {
@Override
public Fruit getAppleInstance ( ) {
return new HPGApple ( ) ;
}
@Override
public Fruit getBananaInstance ( ) {
return new HPGBanana ( ) ;
}
}
demo
package com. learn. absFactory. run ;
import com. learn. absFactory. factory. FSKFactory ;
import com. learn. absFactory. factory. FruitFactory ;
import com. learn. absFactory. factory. HPGFactory ;
import com. learn. absFactory. model. Fruit ;
public class App {
public static void main ( String [ ] args) {
FruitFactory fskFactory = new FSKFactory ( ) ;
fskFactory. getAppleInstance ( ) . buyFruit ( ) ;
fskFactory. getAppleInstance ( ) . eatFruit ( ) ;
fskFactory. getBananaInstance ( ) . buyFruit ( ) ;
fskFactory. getBananaInstance ( ) . eatFruit ( ) ;
FruitFactory hpgFactory = new HPGFactory ( ) ;
Fruit hpgApple = hpgFactory. getAppleInstance ( ) ;
hpgApple. buyFruit ( ) ;
hpgApple. eatFruit ( ) ;
Fruit hpgBanana = hpgFactory. getBananaInstance ( ) ;
hpgBanana. buyFruit ( ) ;
hpgBanana. eatFruit ( ) ;
}
}
抽象工厂关系图