当前位置:网站首页>Factory pattern pattern pattern (simple factory, factory method, abstract factory pattern)

Factory pattern pattern pattern (simple factory, factory method, abstract factory pattern)

2020-11-08 23:45:56 treasure

Three design patterns related to the factory  2020-11-08 15:03:29

Simple factory model : To put it bluntly , Indirectly out of new Create objects , It's about getting objects directly from the factory .

Simple factory : Not with new Create objects , Instead, the process of creating objects is centralized in the factory , This makes it easy to maintain the change creation process ; It also realizes the decoupling between the call creation object and the object .

Don't talk much , Let's start with the following UML Class diagram :

                

 

 Automobile 
package com.yuan.factory.simple;

public abstract class Car {

public abstract void getname();

}

Volkswagen class
package com.yuan.factory.simple;

public class Dazong extends Car {

@Override
public void getname() {
System.out.println(" The public ");
}
}

Tesla cars
package com.yuan.factory.simple;

public class Tsla extends Car {
@Override
public void getname() {
System.out.println(" tesla ");
}
}

Automobile factories
package com.yuan.factory.simple;

public class CarFactory {

public static Car getTsla(){
return new Tsla();
}


public static Car getDazong(){
return new Dazong();
}

}

Customer class
package com.yuan.factory.simple;

public class Customer {
public static void main(String[] args) {
CarFactory carFactory = new CarFactory();
carFactory.getTsla().getname();
carFactory.getDazong().getname();
}
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Factory method model

   Because of the simple factory model , dissatisfaction “ Open to expansion , Turn off for changes ” principle , So , There's the factory approach model . Then the factory approach pattern is on the simple factory model , Create a factory for each subclass , So it's scalable , Follow these principles .

 

 Bus interface 
package com.yuan.factory.method;


public interface Car {

void getname();
}

Volkswagen class
package com.yuan.factory.method;

public class Dazong implements Car {
@Override
public void getname() {
System.out.println(" The public ");
}
}
Tesla cars
package com.yuan.factory.method;

public class Tsla implements Car {
@Override
public void getname() {
System.out.println(" tesla ");
}
}

Car factory interface
package com.yuan.factory.method;

public interface CarFactory {

Car getCar();
}
Volkswagen factory
package com.yuan.factory.method;

public class DazongFactory implements CarFactory {


@Override
public Car getCar() {
return new Dazong();
}
}

Tesla automobile factory
package com.yuan.factory.method;

public class TslaFactory implements CarFactory{

@Override
public Car getCar() {
return new Tsla();
}
}

Customer class
package com.yuan.factory.method;

public class Customer {

public static void main(String[] args) {
new DazongFactory().getCar().getname();
new TslaFactory().getCar().getname();
}
}


 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

Abstract factory pattern

Product family : Different types of products produced by the same manufacturer .

Same product type : A little ... A little ... A little

     Abstract factory pattern (Abstract Factory Pattern) It's about creating other factories around one super factory . This super factory is also called the factory of other factories . This type of design pattern is a creation pattern , It provides the best way to create objects .

     In abstract factory mode , Interface is the factory responsible for creating a related object , There is no need to explicitly specify their classes . Each generated factory can provide objects according to the factory pattern .

 

版权声明
本文为[treasure]所创,转载请带上原文链接,感谢