<> Factory mode
<>1, Simple factory mode
<>1.1, definition
Simple factory mode (Simple Factory Pattern) It means that a factory object decides which product class instance to create , Not belong to 23 Design patterns .
A simple factory is a factory of products
<>1.2, Application scenario
The simple factory pattern is suitable for scenes where factory classes create fewer objects , And the client only needs to pass in factory parameters , You don't need to care about the logic of how to create objects .
<>1.3, shortcoming
The responsibilities of factories are relatively heavy , When adding new products, you need to modify the judgment logic of factory class , It violates the opening and closing principle .
It is not easy to expand the overly complex product structure .
<>1.4, example
Payment interface specification :
public interface IPay { void pay(); }
Concrete implementation :
public class AliPay implements IPay { @Override public void pay() { System.out.
println(" Alipay payment "); } } public class WxPay implements IPay { @Override public void
pay() { System.out.println(" Wechat payment "); } } public class BankPay implements IPay {
@Override public void pay() { System.out.println(" UnionPay payment "); } }
factory :
public class PayFactory { // Inherit interface specification using reflection mechanism , So as to realize product creation . public IPay create(Class<?
extends IPay> clazz) { try { if (null != clazz) { return clazz.newInstance(); }
}catch (Exception e) { e.printStackTrace(); } return null; } }
test :
public class Test { public static void main(String[] args) { PayFactory
payFactory= new PayFactory(); IPay pay = payFactory.create(AliPay.class); pay.
pay(); } }
<>2, Factory method model
<>2.1, definition
Factory method model (Factory Method
Pattern) It refers to defining an interface to create an object that loves you , But let's look at the class of this interface to decide which class to instantiate , Factory methods delay instantiation of classes to subclasses .
It belongs to creative design pattern .
Solve product expansion problems . Upgraded version of simple factory mode .
The factory method is the factory of the factory
<>2.2, Application scenario
Creating objects requires a lot of repetitive code .
client ( application layer ) It does not depend on how product class instances are created , Implementation and other details .
A class determines which object to create through its subclasses .
<>2.3, advantage
Users only need to care about the factory corresponding to the required products , No need to care about creation details .
Adding new products conforms to the opening and closing principle , It improves the scalability of the system .
<>2.4, shortcoming
The number of classes is easy to be too many , It increases the complexity of code structure .
It increases the abstraction and understanding difficulty of the system .
<>2.5, example
Payment interface specification :
public interface IPay { void pay(); }
Concrete implementation :
public class AliPay implements IPay { @Override public void pay() { System.out.
println(" Alipay payment "); } } public class WxPay implements IPay { @Override public void
pay() { System.out.println(" Wechat payment "); } } public class BankPay implements IPay {
@Override public void pay() { System.out.println(" UnionPay payment "); } }
Factory method :
public interface IPayFactory { IPay create(); }
Specific example factory :
public class AliPayFactory implements IPayFactory { @Override public IPay
create() { return new AliPay(); } } public class WxPayFactory implements
IPayFactory { @Override public IPay create() { return new WxPay(); } } public
class BankPayFactory implements IPayFactory { @Override public IPay create() {
return new BankPay(); } }
test :
public class Test { public static void main(String[] args) { IPayFactory
payFactory= new AliPayFactory(); IPay pay = payFactory.create(); pay.pay(); } }
<>3, Abstract factory pattern
<>3.1, definition
Abstract factory pattern (Abastract Factory Pattern) It refers to providing an interface to create a series of related or interdependent objects , There is no need to specify their specific classes .
It belongs to creative design pattern .
Product hierarchy and product family
** Product family :** A range of related products , Put them together to make them relevant .
** Product grade :** Same product standard ( That is, the same inheritance system ), Different characteristics .
<>3.2, Application scenarios related to ethylene shall be
client ( application layer ) Does not depend on how product class instances are created , Implementation and other details .
Emphasize a range of relevant product objects ( Belong to the same product family ) Create objects with . A lot of duplicate code is required .
Provide a library of product classes , All products appear with the same interface , So that the client does not depend on the specific implementation .
<>3.3, advantage
Code isolation of specific products in the application layer , No need to care about creation details
Unify a series of product families and create them together
<>3.4, shortcoming
Specifies all product sets that may be created , It is difficult to expand new products in the product family , The of the abstract factory needs to be modified Interface .
It increases the abstraction and understanding difficulty of the system .
Data connection pool is a classic factory model
<>3.5, example
Payment and collection interface specification :
public interface IPay { void pay(); } public interface ICollect { void collect(
); }
Concrete implementation :
public class AliPay implements IPay { @Override public void pay() { System.out.
println(" Alipay payment "); } } public class WxPay implements IPay { @Override public void
pay() { System.out.println(" Wechat payment "); } } public class AliCollect implements
ICollect { @Override public void collect() { System.out.println(" Alipay arrival "); } }
public class WxCollect implements ICollect { @Override public void collect() {
System.out.println(" Wechat arrival "); } }
Abstract factory :
public abstract class Factory { public void init(){ System.out.println(" Open payment link "
); } public abstract IPAay createPay(); public abstract ICollect createCollect()
; }
Factory implementation :
public class AliFactory extends Factory { @Override public IPAay createPay() {
return new AliPay(); } @Override public ICollect createCollect() { return new
AliCollect(); } } public class WxFactory extends Factory { @Override public
IPAay createPay() { return new WxPay(); } @Override public ICollect
createCollect() { return new WxCollect(); } }
test :
public class Test { public static void main(String[] args) { Factory factory =
new AliFactory(); factory.init(); IPAay aliPay = factory.createPay(); aliPay.pay
(); ICollect aliCollect = factory.createCollect(); aliCollect.collect(); Factory
wxFactory= new WxFactory(); wxFactory.init(); IPAay wxPay = wxFactory.createPay
(); wxPay.pay(); ICollect wxCollect = factory.createCollect(); wxCollect.collect
(); } }
<>4, summary
Simple factory : Factory of products
Factory method : Factory factory
Abstract factory : Complex Product Factory , More concerned about making the product family
Spring Application in :
*
AbastractFactory
*
AnnotationApplicationContext wait
Technology
Daily Recommendation