Design pattern factory design pattern, agent design pattern, adapter design pattern

Posted by j4v1 on Tue, 04 Jan 2022 20:30:49 +0100

Factory design pattern, agent design pattern, adapter design pattern

Factory design mode

There are multiple subclasses under the same parent class (generally an interface). Each time you want to operate a subclass, you must create a new subclass object, which is easy to cause subclass coupling. If you write a transition class between subclasses and new subclasses, it is the factory design pattern
Parent class (Interface)

//animal parent
public interface Animal {
	
	public void run();
}

Subclass dog

//The dog subclass implements the animal parent class
public class Dog implements Animal{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("dog like run");
	}
}

Subclass cat

//cat subclasses implement the animal parent class
public class Cat implements Animal{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("cat like run");
	}
}

Test class

public class Test {
	public static void main(String[] args) {
		Animal a=new Dog();
		a.run();
		Animal b=new Cat();
		b.run();
	}
}

Got it

However, if you want to change the subclass of the operation, you must change the new object in the Test class. Such an operation is extremely discouraged in development, so a transition class is added between them

Transition class

Get name to know which object to create

public class Factory {
	public static Animal getAnimal(String name) {
		if(name.equals("dog")) {
			return new Dog();
		}else if(name.equals("cat")) {
			return new Cat();
		}
		return null;
	}
}

Add a transitional Test class, and call the run () method by new Dog () with the name dog

public class Test {
	public static void main(String[] args) {
		Animal a=Factory.getAnimal("dog");
		a.run();
	}
}

Suppose you add a subclass Bird

public class Bird implements Animal{

	@Override
	public void run() {
		// TODO Auto-generated method stub
		System.out.println("bird not like run");
	}
}

Change transition class

if(name.equals("dog")) {
			return new Dog();
		}else if(name.equals("cat")) {
			return new Cat();
		}else if(name.equals("bird")){
			return new Bird();
		}

The test class calls the Bird method

Animal a=Factory.getAnimal("bird");
a.run();

Agent design pattern

What is the agent design pattern? For example, a player wants to win the first game, but he doesn't want to play by himself, so he recruits people on a platform to win the first game on his behalf. The platform is a theme and an interface. The player and the agent are two objects that implement the interface. In official words, it is to provide an agent for other objects to control access to this object.

interface subject{//Define the subject of an operation
	public void FirstWin();
}

class player implements subject{//Player implementation theme

	@Override
	public void FirstWin() {
		// TODO Auto-generated method stub
		System.out.println("I want to win the first");
	}
		
}

class GamePlayer implements subject{//Generation practice implementation theme
	private subject sub=null;
	public GamePlayer(subject subject) {
		// TODO Auto-generated constructor stub
		this.sub=subject;
	}
	
	public void brfore() {
		System.out.println("Pick up the order, warm up and find the feel");
	}
	
	@Override
	public void FirstWin() {
		// TODO Auto-generated method stub
		brfore();
		this.sub.FirstWin();//Represents the player's ideas
		after();
	}
	
	public void after() {
		System.out.println("Collect the money. Welcome to come next time");
	}
	
}

Test class

public class Demo {
	public static void main(String[] args) {
		subject s=new player();
		subject subject=new GamePlayer(s);
		subject.FirstWin();
	}
}

result

adapter design pattern

Let two interfaces that cannot work together work together
For example, a cross screwdriver is an interface, which can only open the cross nails, and another word screwdriver is an interface, which can open the word nails. The function of the adapter is to enable the cross screwdriver to open the word nails, and vice versa.

interface A{//Cross screwdriver
	public void solve();
}

interface B{//A screwdriver
	public void help();
}

class AImpl implements A {//I'm a cross screwdriver

	@Override
	public void solve() {
		// TODO Auto-generated method stub
		System.out.println("I can solve the nail of the cross");
	}
	
}

class BImpl implements B {//A screwdriver

	@Override
	public void help() {
		// TODO Auto-generated method stub
		System.out.println("I can help deal with a word of nails");
	}
	
}

class Adapter implements A,B{//Adapter

	private A a;
	private B b;
	
	public Adapter(A a) {
		// TODO Auto-generated constructor stub
		this.a=a;
	}
	
	public Adapter(B b) {
		// TODO Auto-generated constructor stub
		this.b=b;
	}
	
	@Override
	public void help() {
		// TODO Auto-generated method stub
		a.solve();
	}

	@Override
	public void solve() {
		// TODO Auto-generated method stub
		b.help();
	}
	
}

Test class

public class Demo05 {
	public static void main(String[] args) {
	//Use the cross screwdriver as a word
		A a=new AImpl();//Define cross screwdriver
		Adapter adapterA=new Adapter(a);//Instantiate adapter B, use the cross screwdriver as a screwdriver. When the help() method is invoked in the adapter, A will be executed.
		help(adapterA);//In fact, it calls help() of the Adapter
		//solve(adapterA);
	}
	public static void solve(A a) {//Static method
		a.solve();
	}
	public static void help(B b) {//Static method
		b.help();
	}
}

result

Topics: Java