Java common design patterns (Singleton, factory, abstract factory)

Source: Internet
Author: User

Design Patterns are often divided into the following three types:

Creation type

When creating an object, we do not directly instantiate the object. Instead, the program determines how to create the object based on the specific scenario. For example, factory method, abstract factory mode, and Singleton mode.

ConstructorIt is used to organize multiple objects into a larger structure. Such as the adapter mode, bridging mode, and bundle mode.

Behavior TypeIt helps communication between objects between systems and controls processes in complex systems. For example, the command mode, interpreter mode, and iterator mode.

I. Singleton Mode

public class Singleton { public static Singleton instance; private Singleton() { } public static Singleton getInstance() {  if (instance == null) {   instance = new Singleton();  }  return instance; } public static void main(String[] args) {  Singleton instance1 = Singleton.getInstance();  Singleton instance2 = Singleton.getInstance();  System.out.println(instance1 == instance2); }}

(In spring, you only need to specify scope = "Singleton" in the configuration object bean to implement singleton)

Ii. Simple Factory

When instance A needs to call the method of instance B, it is not implemented through the new B instance, but through a factory bfactory that can create instance B.

B = new B (); --- à B = bfactory (). getb ();

It is changed:

When instance A needs to call a method, this method can be implemented by instance B or instance B1. Then we define an interface IB to allow B and B1 to implement this interface. And write an IB factory ibfactory to provide a getib () method.

public class A{private IB ib;public A(IB ib){this.ib=ib;}public void showMessage(){ib.print();}}public interface IB{public abstract void print();} public class B implements IB{public void print(){}} public clsss B1 implements IB{public void print(){}} public IBFactory{public IB getIB(){return new B();}} --------------------------IB ib=IBFactory.getIB();A a=new A(ib);a.showMessage();----------------------------

 

Abstract factory pattern diagram:

3. factory methods and abstract factories

In the simple factory mode, the system uses the productfactory factory class to produce all product instances, and the factory class is used to determine the instance that produces the class.

3.1 factory MethodIn the factory method, if we do not want to make logical judgments in the factory class, the program can provide different factories for different product classes and different factory classes to produce different products.

After the factory method is used, the client code is successfully separated from the implementation class of the called object.

3.2 Abstract Factory Model: To solve the coupling problem between the client code and different factories, add a factory class that produces outputfactory instances (different factory instances are produced based on input parameters)

public class OutputFactoryFactory {public static OutputFactory getOutputFactory(String type) {if ("better".equalsIgnoreCase(type)) {return new BetterOutputFactory();} else {return new OutputFactory();}}}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.