Personal Technology Blog (IBLi)
CSDN making the nuggets
Simple Factory model
The simple factory pattern is where a factory object decides which instance of a product class to create; It is a creative pattern, but it is not one of GOF’s 23 design patterns.
UML class diagrams
elements
1. An abstract product class 2. A concrete product class 3
All kinds of happy water loved by fat lovers (product interface)
public interface Kls {
String name(a);
}
Copy the code
Fat House Happy Water – Cola (specific product)
public class Coke implements Kls {
@Override
public String name(a) {
return "Fat House Happy water - Coke."; }}Copy the code
Happy Water – Sprite (Specific product)
public class Sprite implements Kls {
@Override
public String name(a) {
return "Happy Water - Sprite"; }}Copy the code
Happy Water Production Factory (Factory)
public class KlsFactory {
public static Kls getFzs(String type) throws Exception {
Kls fzs = null;
if ("coke".equalsIgnoreCase(type)) {
fzs = new Coke();
} else if ("sprite".equalsIgnoreCase(type)) {
fzs = new Sprite();
}
if (Objects.isNull(fzs)) {
throw new RuntimeException("No happy water.");
}
returnfzs; }}Copy the code
Client use
public class Fz {
@Test
public void drink(a) throws Exception {
// Make coke
Kls coke = KlsFactory.getFzs("coke");
System.out.println("Fat house begins to drink:" + coke.name());
// Make Sprite
Kls sprite = KlsFactory.getFzs("sprite");
System.out.println("Fat house begins to drink:"+ sprite.name()); }}Copy the code
Advantages and disadvantages of simple factories
advantages
- A concrete implementation hidden from the client, where the client only needs to know what object to create, not how it was created
- Decoupled, the client does not need to create objects through new. If the production class needs to be modified later, it only needs to modify the factory class, rather than the whole project to find where the new is
disadvantages
- A dedicated factory is responsible for production, and if the business becomes complex, this class becomes bloated
- What products are produced by the factory class are written dead in the factory, if new products are added, but also modify the production logic of the factory class
Factory method pattern
The Factory Method pattern is a step further from the simple Factory. Instead of providing a single Factory class to create all objects, we provide different factories for different objects. That is, each object has a factory corresponding to it.
elements
1. One abstract product class 2. Multiple specific product classes 3. One Abstract factory 4
Follow the happy water example above. The KlsFactory is abstracted from the common method, and then the concrete KlsFactory is realized respectively.
Happy water general factory
public interface Factory {
/** * make happy water **@return Kls
*/
Kls create(a);
}
Copy the code
The coke plant
public class CokeFactory implements Factory {
@Override
public Kls create(a) {
return newCoke(); }}Copy the code
Sprite factory
public class SpriteFactory implements Factory {
@Override
public Kls create(a) {
return newSprite(); }}Copy the code
Fat House (client)
public class Fz {
@Test
public void drink(a) throws Exception {
// Make coke
CokeFactory cokeFactory = new CokeFactory();
Kls coke = cokeFactory.create();
System.out.println("Fat house begins to drink:" + coke.name());
// Make Sprite
SpriteFactory spriteFactory = new SpriteFactory();
Kls sprite = spriteFactory.create();
System.out.println("Fat house begins to drink:"+ sprite.name()); }}Copy the code
Expand Fanta Happy Water
public class Fanta implements Kls {
@Override
public String name(a) {
return "Happy Water -- Fanta."; }}Copy the code
Copy code fender factory
public class FantaFactory implements Factory {
@Override
public Kls create(a) {
return newFanta(); }}Copy the code
Fat house use add
FantaFactory fantaFactory = new FantaFactory(); Kls fanta = fantaFactory.create(); System.out.println(" + fanta.name());Copy the code
Advantages and disadvantages of the factory method pattern
advantages
(1) Reduce the code coupling degree, object generation is handed over to the subclass (where the coupling degree is relative to the simple factory mode of the factory class)
disadvantages
1. Increase the amount of code, each specific product needs a specific factory (in a specific business may produce a large number of duplicate code). 2. When adding abstract products, that is, adding another product family requires modification of factory against OCP
Abstract Factory pattern
elements
Concrete Product Class 3. Abstract Factory Class – Declares (a group) methods that return abstract products 4. Concrete Factory Class – generates (a group) concrete products
One abstract product class and two concrete products (Cola)
public abstract class Coke {
public abstract void doCreate(a);
}
public class LocalCoke extends Coke{
@Override
public void doCreate(a) {
System.err.println("Produce local Coke."); }}public class ForeignCoke extends Coke{
@Override
public void doCreate(a) {
System.out.println("Making foreign Cola"); }}Copy the code
One abstract product class and two concrete products (Sprite)
public abstract class Sprite {
public abstract void doCreate(a);
}
public class LocalSprite extends Sprite{
@Override
public void doCreate(a) {
System.out.println("Make local Sprite."); }}public class ForeignSprite extends Sprite{
@Override
public void doCreate(a) {
System.err.println("Making foreign Sprite."); }}Copy the code
One abstract factory and two concrete factories
public interface IAbstractFactory {
/** * produces coke **@returnDeclare (a set of) methods that return abstract products */
Coke createCoke(a);
/** * make Sprite **@returnDeclare (a set of) methods that return abstract products */
Sprite createSprite(a);
}
public class LocalFactory implements IAbstractFactory{
/** * produces coke **@returnGenerate (a set of) concrete products */
@Override
public LocalCoke createCoke(a) {
return new LocalCoke();
}
/** * make Sprite **@returnGenerate (a set of) concrete products */
@Override
public LocalSprite createSprite(a) {
return newLocalSprite(); }}public class ForeignFactory implements IAbstractFactory{
/** * produces coke **@returnGenerate (a set of) concrete products */
@Override
public ForeignCoke createCoke(a) {
return new ForeignCoke();
}
/** * make Sprite **@returnGenerate (a set of) concrete products */
@Override
public ForeignSprite createSprite(a) {
return newForeignSprite(); }}Copy the code
Use of abstract factories
public class FactoryTest {
public static void main(String[] args) {
LocalFactory localFactory = new LocalFactory();
LocalCoke localCoke = localFactory.createCoke();
localCoke.doCreate();
ForeignFactory foreignFactory = newForeignFactory(); ForeignSprite foreignSprite = foreignFactory.createSprite(); foreignSprite.doCreate(); }}Copy the code
Advantages and disadvantages of abstract factories
advantages
In abstract factory mode, we can define and implement more than one interface, and a factory can generate more than one product class. It is very flexible and easy to expand for the production of complex objects (compared with the optimization of factory method mode).
disadvantages
1. All factories need to be modified to expand the product, which violates the OCP principle. 2
Refer to the article
- Design patterns – Simple Factory, Factory method pattern, Abstract factory pattern
- Design pattern — Factory pattern
How many more ten years to go on being a hot-blooded teenager