What design patterns do you know? Let me show you some of the common ones.

Keywords: Java

  • What design patterns do you know?

There are 23 design patterns in Java. We don't need all of them, but we should master some of them. All design patterns are listed below. The design patterns I need to master are listed separately. Of course, the more I can master, the better.
Overall, design patterns fall into three categories:

  1. There are five types of creative mode: factory method mode, abstract factory mode, singleton mode, builder mode and prototype mode.
  2. There are seven structural modes: adapter mode, decorator mode, agent mode, appearance mode, bridge mode, combination mode and hedonic mode.
  3. There are eleven behavioral modes: strategy mode, template method mode, observer mode, iteration mode, responsibility chain mode, command mode, memorandum mode.
    Patterns, state patterns, visitor patterns, mediator patterns, interpreter patterns.

Single row mode
The best understanding of a design model, divided into the lazy and hungry Han style.

Hungry Chinese:
public class Singleton { 
// Direct creation of objects
 public static Singleton instance = new Singleton();
// Privatization constructor 
private Singleton() { }
// Return object instance 
public static Singleton getInstance() {
 return instance; 
 }
}
Slacker:
public class Singleton { 
// Declare variables
  private static volatile Singleton singleton = null;
// private constructors 
   private Singleton() { }
// Providing external methods
   public static Singleton getInstance() { 
     if (singleton == null) {
           if (singleton == null) {
          }
        }
 return singleton;
    }

Factory Design Model
Factory mode is divided into factory method mode and abstract factory mode.
Factory Method Model:
The factory method pattern is divided into three kinds: the common factory pattern, which is to build a factory class, and to create instances of some classes that implement the same interface.
The multiple factory method pattern is an improvement on the common factory method pattern. In the common factory method pattern, if the string passed is wrong, the object can not be created correctly. The multiple factory method pattern provides multiple factory methods to create objects separately.
Static factory method mode, the method in the above multiple factory method mode is set to static, without creating an instance, can be called directly.
Ordinary factory model:

public interface Sender {
 public void Send(); 
 } 
 public class MailSender implements Sender {
          System.out.println("this is mail sender!");
   }
}
 public class SmsSender implements Sender {
@Override
  public void Send() { 
    }
} 
public class SendFactory { 
   public Sender produce(String type) { 
     if ("mail".equals(type)) {
          return new MailSender();
      return new SmsSender();
    } else { 
      return null; 
   } 
 }

Multiple Factory Method Patterns
This pattern is an improvement on the common factory method pattern. In the common factory method pattern, if the string passed is wrong, the object can not be created correctly, while the multiple factory method pattern provides multiple factory methods to create objects separately.

public class SendFactory { 
public Sender produceMail(){
 return new MailSender();
    }
  return new SmsSender(); 
  }
 }
public class FactoryTest { 
    public static void main(String[] args) { 
     SendFactory factory = new SendFactory();
      Sender sender = factory.produceMail(); 
        }
 }

Static factory method mode, the method in the above multiple factory method mode is set to static, without creating an instance, can be called directly.

  public class SendFactory {
    public static Sender produceMail(){
 }
  public static Sender produceSms(){ 
     return new SmsSender();
    }
}
  public class FactoryTest { 
    public static void main(String[] args) { 
     Sender sender = SendFactory.produceMail(); 
     sender.send(); 
    }
 }

Abstract factory pattern
The factory method pattern has a problem that class creation depends on factory class, that is to say, if you want to expand the program, you must modify the factory class, which violates the closure principle. So, from the design point of view, there are some problems, how to solve them? Use the abstract factory pattern to create multiple jobs
Factory class, so that once you need to add new functions, you can directly add new factory class, without modifying the previous code.

Builder Model
The factory class pattern provides the pattern of creating a single class, while the builder pattern manages all kinds of products together to create composite objects. The so-called composite objects refer to a class with different attributes. In fact, the builder pattern is the combination of the former abstract factory pattern and the last Test.

adapter design pattern

The adapter pattern converts the interface of a class into another interface representation expected by the client, aiming at eliminating the class compatibility problem caused by interface mismatch. There are three main types: class adapter mode, object adapter mode and interface adapter mode.

Class adapter pattern

public class Source { 
public void method1() { 
System.out.println("this is original method!");
 } 


----------
public interface Targetable { 
/* Same method as in the original class */ 
public void method1(); 
/* Methods of new classes */ 
public void method2(); 
} 
public class Adapter extends Source implements Targetable { @Override 
public void method2() {
 System.out.println("this is the targetable method!"); 
} 
public class AdapterTest { 
public static void main(String[] args) { 
Targetable target = new Adapter();
 target.method1();
} 
}

Object's adapter pattern
The basic idea is the same as the Adapter mode of the class. It only modifies the Adapter class. This time, instead of inheriting the Source class, it holds an instance of the Source class to solve the compatibility problem.

public class Wrapper implements Targetable {
 private Source source;
public Wrapper(Source source) {
 super(); 
 }
@Override
 public void method2() { 
}
@Override 
public void method1() {
 source.method1(); 
 }


----------


public class AdapterTest {
public static void main(String[] args) { 
Source source = new Source(); 
Targetable target = new Wrapper(source); 
target.method1(); 
target.method2();
 }
}

Interface adapter mode

The adapter of an interface is like this: Sometimes there are many abstract methods in an interface we write. When we write the implementation class of the interface, we must implement all the methods of the interface. This is obviously wasteful sometimes, because not all the methods are needed, sometimes only some ones are needed. In order to solve this problem, we introduce the adapter model of the interface. Formula, with the aid of an abstract class, the abstract class implements the interface and implements all the methods. Instead of dealing with the original interface, we only get in touch with the abstract class, so we write a class, inherit the abstract class, and rewrite the methods we need.

Observer
The observer model is well understood, similar to email subscriptions and RSS subscriptions. When we browse some blogs or wiki s, we often see RSS icons. This means that when you subscribe to this article, you will be notified in time if there are subsequent updates. In fact, simply put it in one sentence: when an object changes, other objects that depend on it will be notified, and as it changes! Objects are one-to-many relationships.

public interface Observer { 
public void update(); 
}
public class Observer1 implements Observer { 
@Override 
public void update() { 
System.out.println("observer1 has received!"); 
} 
}
public class Observer2 implements Observer { 
@Override
 public void update() { 
 }
  }
 /*Increase the number of observers*/ 
 public void add(Observer observer);
public void del(Observer observer);
/*Notify all observers*/ 
public void notifyObservers();
/*Self-operation*/
 public void operation();
}
public abstract class AbstractSubject implements Subject {
private Vector<Observer> vector = new Vector<Observer>();
@Override
 public void add(Observer observer) { 
}
@Override 
public void del(Observer observer) { 
vector.remove(observer); 
}
@Override 
public void notifyObservers() { 
Enumeration<Observer> enumo = vector.elements(); 
while (enumo.hasMoreElements()) { 
enumo.nextElement().update(); 
}
 }
}
public class MySubject extends AbstractSubject {
@Override
 public void operation() { 
notifyObservers(); 
}
}
public class ObserverTest { 
public static void main(String[] args) { 
Subject sub = new MySubject(); 
sub.add(new Observer1());
sub.add(new Observer2());
 sub.operation();
}
}

Posted by gooney0 on Fri, 12 Apr 2019 00:36:32 -0700