public class Wuling implements Car {
@Override
public void name ( ) {
System. out. println ( "五菱宏光!" ) ;
}
}
public class Tesla implements Car {
@Override
public void name ( ) {
System. out. println ( "特斯拉!" ) ;
}
}
public class Dazhong implements Car {
@Override
public void name ( ) {
System. out. println ( "大众" ) ;
}
}
public class CarFactory {
public static Car getcar ( String car) {
if ( car. equals ( "五菱" ) ) {
return new Wuling ( ) ;
} else if ( car. equals ( "特斯拉" ) ) {
return new Tesla ( ) ;
} else {
return null;
}
}
public static Car getWuling ( ) {
return new Wuling ( ) ;
}
public static Car getTesla ( ) {
return new Tesla ( ) ;
}
}
public interface Car {
void name ( ) ;
}
public class Consumer {
public static void main ( String[ ] args) {
Car c1 = CarFactory. getcar ( "五菱" ) ;
Car c2 = CarFactory. getcar ( "特斯拉" ) ;
c1. name ( ) ;
c2. name ( ) ;
}
}
package Month4. day4_20. method;
public class MoBai implements Car {
@Override
public void name ( ) {
System. out. println ( "摩拜单车" ) ;
}
}
package Month4. day4_20. method;
public class MoBaiFactory implements CarFactory {
@Override
public Car getCar ( ) {
return new MoBai ( ) ;
}
}
package Month4. day4_20. method;
public class Tesla implements Car {
@Override
public void name ( ) {
System. out. println ( "特斯拉!" ) ;
}
}
package Month4. day4_20. method;
public class TeslaFactory implements CarFactory {
@Override
public Car getCar ( ) {
return new Tesla ( ) ;
}
}
package Month4. day4_20. method;
public class Wuling implements Car {
@Override
public void name ( ) {
System. out. println ( "五菱宏光!" ) ;
}
}
package Month4. day4_20. method;
public class WulingFactory implements CarFactory {
@Override
public Car getCar ( ) {
return new Wuling ( ) ;
}
}
package Month4. day4_20. method;
public interface Car {
void name ( ) ;
}
package Month4. day4_20. method;
public interface CarFactory {
Car getCar ( ) ;
}
package Month4. day4_24. TestAbstractFactory;
public interface Kind {
Dog dog ( ) ;
Cat cat ( ) ;
}
package Month4. day4_24. TestAbstractFactory;
public interface Dog {
void lookhome ( ) ;
void smile ( ) ;
}
package Month4. day4_24. TestAbstractFactory;
public interface Cat {
void climb ( ) ;
void eatfish ( ) ;
}
package Month4. day4_24. TestAbstractFactory;
public class Pet implements Kind {
@Override
public Dog dog ( ) {
return new CaiDog ( ) ;
}
@Override
public Cat cat ( ) {
return new BlueCat ( ) ;
}
}
package Month4. day4_24. TestAbstractFactory;
public class CaiDog implements Dog {
@Override
public void lookhome ( ) {
System. out. println ( "柴犬不会看家!" ) ;
}
@Override
public void smile ( ) {
System. out. println ( "柴犬一边喝水,一边微笑!" ) ;
}
}
package Month4. day4_24. TestAbstractFactory;
public class BlueCat implements Cat {
@Override
public void climb ( ) {
System. out. println ( "英短猫咪爬树!" ) ;
}
@Override
public void eatfish ( ) {
System. out. println ( "英短吃水里的鱼" ) ;
}
}
package Month4. day4_24. TestAbstractFactory;
public class Test {
public static void main ( String[ ] args) {
System. out. println ( "=========英短猫咪========" ) ;
Pet pet = new Pet ( ) ;
Cat cat = pet. cat ( ) ;
cat. climb ( ) ;
cat. eatfish ( ) ;
System. out. println ( "=========柴犬========" ) ;
Pet pet1 = new Pet ( ) ;
Dog dog = pet1. dog ( ) ;
dog. lookhome ( ) ;
dog. smile ( ) ;
}
}
定义: 抽象工厂模式提供了一个创建一系列相关或者相互依赖对象的接口,无需指定他们的具体的类! 实用场景: 1.客户端应用层不依赖于产品实例如何被创建,实现等细节2.强调一些列相关的产品对象,一起使用创建对象需要大量的重复代码3.提供一个产品类的库,所有产品以相同的接口出现,从而使得客户端不依赖于具体的实现! 优点: 具体产品在应用层的代码隔离,无需关心创建的细节,将一个系列的产品统一到一起创建 缺点: 规定了所有可能被创建的产品集合,产品族中扩展新的产品困难,增加了系统的抽象性和理解难度