我看了工厂模式的三个不同的模式,跟着敲的时候,我想到了springmvc,它有没有用到工厂模式,然后百度了一下,原来没有工厂模式,springmvc中含有的是策略模式、观察者模式和合成模式,等在具体看看吧。策略模式我好早看过,都忘记了。要温故而知新。还有很多看过了就以为自己懂了,其实只是有个印象,还是要实现,动手,读书百变其义自见。未来的路还是很长,加油吧!
参考:https://blog.csdn.net/weixin_35985385/article/details/81430545
上面博客里面的有图很详细。我只是把我自己的代码记录一下方便自己查看。窝窝头,嘿嘿!
简单工厂模式:
/**
* @program: IdeaJava
* @Date: 2020/1/9 14:50
* @Author: lhh
* @Description: 简单工厂模式
*/
public class SimpleFacotryApp {
public static void main(String[] args) {
Car myAutoCar = SimpleFactory.createCar("Auto");
myAutoCar.run();
Car myAudiCar = SimpleFactory.createCar("Audi");
myAudiCar.run();
}
}
interface Car
{
void run();
}
class Auto implements Car
{
@Override
public void run() {
System.out.println("Auto runing ~~~");
}
}
class Audi implements Car
{
@Override
public void run() {
System.out.println("Audi runing ~~~");
}
}
class SimpleFactory
{
public static Car createCar(String s)
{
if (s.equals("Auto")) return new Auto();
if (s.equals("Audi")) return new Audi();
return null;
}
}
工厂模式:
/** * @program: IdeaJava * @Date: 2020/1/9 15:22 * @Author: lhh * @Description: 工厂模式 */ public class FactoryApp { public static void main(String[] args) { TomatoFoodFactory tomatoFactory = new TomatoFoodFactory(); Food mytomato = tomatoFactory.createFood(); mytomato.color(); Food myBanana = new BananaFoodFactory().createFood(); myBanana.color(); } } interface Food { void color(); } class Tomato implements Food { @Override public void color() { System.out.println("Tomato is red!"); } } class Banana implements Food { @Override public void color() { System.out.println("Banana is yellow!"); } } interface FoodFactory { Food createFood(); } class BananaFoodFactory implements FoodFactory { @Override public Food createFood() { return new Banana(); } } class TomatoFoodFactory implements FoodFactory { @Override public Food createFood() { return new Tomato(); } }
抽象工厂模式:
/** * @program: IdeaJava * @Date: 2020/1/9 16:22 * @Author: lhh * @Description: 抽象工厂模式 */ public class AbstractFactoryApp { public static void main(String[] args) { IceCreamFactory appleIceCreamFactory= new AppleIceCreamFactory(); BigIceCream bigAppleIceCream = appleIceCreamFactory.createBigIceCream(); SmallIceCream smallAppleIceCream = appleIceCreamFactory.createSmallIceCream(); bigAppleIceCream.taste(); smallAppleIceCream.taste(); IceCreamFactory bananaIceCreamFactory = new BananaIceCreamFactory(); BigIceCream bigBananaIceCream = bananaIceCreamFactory.createBigIceCream(); SmallIceCream smallBananaIceCream = bananaIceCreamFactory.createSmallIceCream(); bigBananaIceCream.taste(); smallBananaIceCream.taste(); } } interface BigIceCream { void taste(); } interface SmallIceCream { void taste(); } class BigAppleIceCream implements BigIceCream { @Override public void taste() { System.out.println("这是苹果味冰淇淋(大份)"); } } class SmallAppleIceCream implements SmallIceCream { @Override public void taste() { System.out.println("这是苹果味冰淇淋(小份)"); } } class BigBananaIceCream implements BigIceCream { @Override public void taste() { System.out.println("这是香蕉味冰淇淋(大份)"); } } class SmallBananaIceCream implements SmallIceCream { @Override public void taste() { System.out.println("这是香蕉味冰淇淋(小份)"); } } interface IceCreamFactory { BigIceCream createBigIceCream(); SmallIceCream createSmallIceCream(); } class AppleIceCreamFactory implements IceCreamFactory { @Override public BigIceCream createBigIceCream() { return new BigAppleIceCream(); } @Override public SmallIceCream createSmallIceCream() { return new SmallAppleIceCream(); } } class BananaIceCreamFactory implements IceCreamFactory { @Override public BigIceCream createBigIceCream() { return new BigBananaIceCream(); } @Override public SmallIceCream createSmallIceCream() { return new SmallBananaIceCream(); } }