Write in front
Copy an assignment on the Internet, but there's no money! I have a fart money. I'm so angry that I wrote it directly. Share it here for reference (you can copy it, but you have to copy it when you know it). You can reprint it without signing my name, but you need to state that it is not original.
This is a series of homework, so I'm angry. I don't want to be angry later.
This assignment has a lot of reference for the book graphic design mode. Thank you and recommend it here.
Creative design pattern experiment
1. Simple factory
Topic: use the simple factory mode to design a drawing tool class that can create different geometries (such as circle, rectangle, triangle, etc.). Each geometry has a drawing method daw() and an erase method erase() , it is required to throw an unsupported shapeexception exception when drawing unsupported geometry. Draw class diagram and program simulation implementation.
Class diagram
Programming implementation
package TemplateMethod; public abstract class AbstractDraw { public void draw(){ throw new UnsupportedShapedException("Drawn graphics that cannot be drawn"); } public abstract void erase(); } package TemplateMethod; public class Circle extends AbstractDraw{ @Override public void erase() { System.out.println("The circle has been cleared"); } } package TemplateMethod; public class Rectangle extends AbstractDraw{ @Override public void draw() { System.out.println("The triangle has been drawn"); } @Override public void erase() { System.out.println("The rectangle has been cleared"); } } package TemplateMethod; public class Triangle extends AbstractDraw{ @Override public void draw() { System.out.println("The rectangle has been drawn"); } @Override public void erase() { System.out.println("The triangle has been cleared"); } } package TemplateMethod; public class UnsupportedShapedException extends RuntimeException{ public UnsupportedShapedException(){ } public UnsupportedShapedException(String strings){ super(strings); } } package TemplateMethod; public class Main{ public static void main(String[] args){ AbstractDraw c = new Circle(); AbstractDraw r = new Rectangle(); AbstractDraw t = new Triangle(); r.draw(); t.draw(); t.erase(); c.draw(); } }
Operation results
2. Factory method
Title: in a network management software, different connection classes need to be provided for different network protocols, such as POP3Connection for POP3 protocol, IMAPConnection for IMAP protocol, HTTPConnection for HTTP protocol, etc. because the creation process of network connection objects is complex, it is necessary to encapsulate the creation process into special classes It will also support more types of network protocols. Now it adopts factory method mode for design, drawing class diagram and programming simulation.
Class diagram
Programming implementation
package FactoryMethod.Connection; import FactoryMethod.FrameWork.Product; public class HTTPConnection extends Product { private String owner; HTTPConnection(String owner){ this.owner = owner; System.out.println("For users" + owner + "Created HTTPConnection Connection class"); } @Override public void use() { System.out.println("user" + owner + "in use HTTPConnection Connection class"); } public String getOwner() { return owner; } } package FactoryMethod.Connection; import FactoryMethod.FrameWork.Factory; import FactoryMethod.FrameWork.Product; import java.util.ArrayList; import java.util.List; public class HTTPFactory extends Factory { private List<String> owners = new ArrayList(); @Override protected Product createProduct(String owner) { return new HTTPConnection(owner); } @Override protected void registerProduct(Product product) { owners.add(((HTTPConnection)product).getOwner()); } public List getOwners(){ return owners; } } package FactoryMethod.Connection; import FactoryMethod.FrameWork.Product; public class IMAPConnection extends Product { private String owner; IMAPConnection(String owner){ this.owner = owner; System.out.println("For users" + owner + "Created IMAPConnection Connection class"); } @Override public void use() { System.out.println("user" + owner + "in use IMAPConnection Connection class"); } public String getOwner() { return owner; } } package FactoryMethod.Connection; import FactoryMethod.FrameWork.Factory; import FactoryMethod.FrameWork.Product; import java.util.ArrayList; import java.util.List; public class IMAPFactory extends Factory { private List<String> owners = new ArrayList(); @Override protected Product createProduct(String owner) { return new IMAPConnection(owner); } @Override protected void registerProduct(Product product) { owners.add(((IMAPConnection)product).getOwner()); } public List getOwners(){ return owners; } } package FactoryMethod.Connection; import FactoryMethod.FrameWork.Product; public class POP3Connection extends Product { private String owner; POP3Connection(String owner){ this.owner = owner; System.out.println("For users" + owner + "Created POP3Connection Connection class"); } @Override public void use() { System.out.println("user" + owner + "in use POP3Connection Connection class"); } public String getOwner() { return owner; } } package FactoryMethod.Connection; import FactoryMethod.FrameWork.Factory; import FactoryMethod.FrameWork.Product; import java.util.ArrayList; import java.util.List; public class POP3Factory extends Factory { private List<String> owners = new ArrayList(); @Override protected Product createProduct(String owner) { return new POP3Connection(owner); } @Override protected void registerProduct(Product product) { owners.add(((POP3Connection)product).getOwner()); } public List getOwners(){ return owners; } } package FactoryMethod.FrameWork; public abstract class Factory { public final Product create(String owner){ Product n = createProduct(owner); registerProduct(n); return n; } protected abstract Product createProduct(String owner); protected abstract void registerProduct(Product product); } package FactoryMethod.FrameWork; public abstract class Product { public abstract void use(); } package FactoryMethod; import FactoryMethod.Connection.HTTPFactory; import FactoryMethod.Connection.IMAPFactory; import FactoryMethod.Connection.POP3Factory; import FactoryMethod.FrameWork.Factory; import FactoryMethod.FrameWork.Product; public class Main { public static void main(String[] args){ Factory pop3Factory = new POP3Factory(); Factory ImapFactory = new IMAPFactory(); Factory HttpFactory = new HTTPFactory(); Product pop3 = pop3Factory.create("Paratrooper one"); Product imap = ImapFactory.create("variable A"); pop3.use(); } }
Operation results
3. Abstract factory
Title: in order to improve the performance of database operation, users can customize the database Connection object Connection object and Statement object Statement, and provide different Connection objects and Statement objects for different types of databases, such as Oracle or MySQL special Connection classes and Statement classes, and users can dynamically change the number of systems according to actual needs through configuration files The system is designed with abstract factory pattern, the corresponding class diagram is drawn and programmed to simulate the implementation.
Class diagram
Programming implementation
package AbstractFactory; public class Connection { public void connection(){} } package AbstractFactory; public abstract class databaseFactory { public abstract Connection createConnection(); public abstract Statement createStatement(); } package AbstractFactory; public class Main { public static void main(String[] args) { String databaseType = "Mysql"; Class clazz; Object object = null; try { clazz = Class.forName("AbstractFactory." + databaseType+"Factory"); object = clazz.newInstance(); } catch (Exception e) { e.printStackTrace(); } databaseFactory daFactory = (databaseFactory) object; Connection connection ; Statement statement; connection = daFactory.createConnection(); statement = daFactory.createStatement(); connection.connection(); statement.statement(); } } package AbstractFactory; public class mysqlConnection extends Connection { public void connection() { System.out.println("provide MySql Connection object"); } } package AbstractFactory; public class MysqlFactory extends databaseFactory { @Override public Connection createConnection() { return new mysqlConnection(); } @Override public Statement createStatement() { return new mysqlStatement(); } } package AbstractFactory; public class mysqlStatement extends Statement{ public void statement() { System.out.println("provide mysql Statement object"); } } package AbstractFactory; public class oracleConnection extends Connection { public void connection() { System.out.println("provide oracle Connection object"); } } package AbstractFactory; public class OracleFactory extends databaseFactory{ @Override public Connection createConnection() { return new oracleConnection(); } @Override public Statement createStatement() { return new oracleStatement(); } } package AbstractFactory; public class oracleStatement extends Statement { public void statement() { System.out.println("provide oracle Statement object"); } } package AbstractFactory; public class Statement { public void statement(){} }
Operation results
When it is Mysql
When Oracle
4. Builder mode
Title: in a racing game, racing cars include formula racing, field racing, sports car, truck and other types. The body, engine, tire, gearbox and other components of different types of racing cars are different. Players can choose their own racing car type, and the system will create a complete racing car according to the player's choice. Now the builder mode is used to build and draw the racing car The corresponding class diagram is programmed and simulated
Class diagram
Programming implementation
package Builder; public abstract class Builder { public abstract void carBody(String string); public abstract void engine(String string); public abstract void tire(String string); public abstract void gearbox(String string); public abstract void over(String string); } package Builder; public class Director { private Builder builder; public Director(Builder builder){ this.builder = builder; } public void construct(){ builder.carBody("Your body is being built, please wait"); builder.engine("Your engine is being built, please wait"); builder.gearbox("Your gearbox is being built, please wait"); builder.tire("Your tire is being built, please wait"); builder.over("Congratulations"); } } package Builder; public class FormulaCar extends Builder{ @Override public void carBody(String string) { System.out.println(string + "\n The system creates the body of formula racing for you!"); } @Override public void engine(String string) { System.out.println(string + "\n The system creates the engine of formula racing for you!"); } @Override public void tire(String string) { System.out.println(string + "\n The system creates formula car tires for you!"); } @Override public void gearbox(String string) { System.out.println(string + "\n The system creates the gearbox of formula racing for you!"); } @Override public void over(String string) { System.out.println(string + "\n Your formula one car has been built!"); } } package Builder; public class Main { public static void main(String[] args){ Builder truck = new Truck(); Builder formulaCar = new FormulaCar(); Builder sportCarBuilder = new SportCarBuilder(); Director truck1 = new Director(truck); Director formulaCar1 = new Director(formulaCar); Director sportCarBuilder1 = new Director(sportCarBuilder); truck1.construct(); formulaCar1.construct(); sportCarBuilder1.construct(); } } package Builder; public class SportCarBuilder extends Builder{ @Override public void carBody(String string) { System.out.println(string + "\n The system creates the body of sports car for you!"); } @Override public void engine(String string) { System.out.println(string + "\n The system creates the engine of sports car for you!"); } @Override public void tire(String string) { System.out.println(string + "\n The system creates the tires of sports cars for you!"); } @Override public void gearbox(String string) { System.out.println(string + "\n The system creates a gearbox for your sports car!"); } @Override public void over(String string) { System.out.println(string + "\n Your sports car has been built!"); } } package Builder; public class Truck extends Builder{ @Override public void carBody(String string) { System.out.println(string + "\n The system creates the truck body for you!"); } @Override public void engine(String string) { System.out.println(string + "\n The system creates the truck engine for you!"); } @Override public void tire(String string) { System.out.println(string + "\n The system creates truck tires for you!"); } @Override public void gearbox(String string) { System.out.println(string + "\n The system creates the gearbox of the truck for you!"); } @Override public void over(String string) { System.out.println(string + "\n Your truck has been built!"); } }
Operation results
5. Prototype mode
Title: in an online recruitment website, users can create a resume template. For different jobs, you can copy the resume template and make appropriate modifications to generate a new resume. When copying a resume, users can choose whether to copy the photos in the resume: if you choose "yes" , the photos will be copied together. Modifying the photos in the new resume will not affect the photos in the resume template, and modifying the template will not affect the new resume. If you select no , the photos in the resume template are directly referenced. Modifying the photos in the resume template will lead to the photos in the new resume being modified together, and vice versa. Now the resume replication function is designed in prototype mode, and two implementation schemes of shallow cloning and deep cloning are provided. The corresponding class diagram is drawn and programmed for simulation.
Class diagram
Programming implementation
package Prototype.FrameWork; import java.util.HashMap; public class Manager { // private HashMap showcase = new HashMap(); // public void register(String name, Product proto) { // showcase.put(name,proto); // } public Product create(Boolean bool, Product product) { //Product p = (Product) showcase.get(protoname); return product.createClone(bool); } } package Prototype.FrameWork; public interface Product extends Cloneable{ void use(String s,String picture); Product createClone(Boolean bool); } package Prototype; import Prototype.FrameWork.Manager; import Prototype.FrameWork.Product; public class Main { public static void main(String[] args){ Manager manager = new Manager(); Picture picture1 = new Picture("A cat"); Picture picture2 = new Picture("A dog"); Resume abc = new Resume("personal information",picture1); abc.use("",""); Product a = manager.create(true,abc); //true is a deep clone and false is a shallow clone a.use("",""); abc.picture.newPicture("A dog"); //abc.reset("",picture2); abc.use("",""); a.use("",""); // manager.register("haha",abc); // Product a = manager.create("haha"); // a.use("123",""); // abc.reset("template resume updated", picture2); // a.use("456",""); // Product c = manager.create("haha"); // c.use("789",""); // manager.register("xixi",abc); // Product b = manager.create("xixi"); // b.use("",""); } } package Prototype; import Prototype.FrameWork.Product; public class Picture implements Product { private String pic = "Original picture"; public Picture(){ } public Picture(String pic){ this.pic = pic; } public void newPicture(String pic){ this.pic = pic; } public String getPic(){ return this.pic; } @Override public Product createClone(Boolean bool) { if(bool){ Picture p = null; try { p = (Picture) clone(); }catch (CloneNotSupportedException e) { e.printStackTrace(); } return p; } else return this; } @Override public void use(String s, String picture) { System.out.println("first" + s + "the second" + picture); } } package Prototype; import Prototype.FrameWork.Product; public class Resume implements Product { private String s = "This is the main part, where you can insert the resume:"; public Picture picture; public Resume(){ } public Resume(String s, Picture picture){ this.s = s; this.picture = picture; } public Resume getPicture(Resume resume){ return resume; } public void reset(String s, Picture picture){ this.s = s; this.picture = picture; } @Override public void use(String s, String picture) { if(this.picture != null){ System.out.println(this.s + " " + s + "\n" + this.picture.getPic() + " " + picture); } else System.out.println(this.s + " " + s + "\n" + "Guru Guru Nagetto" + " " + picture); } @Override public Product createClone(Boolean bool) { Resume p = null; try { p = (Resume) clone(); p.picture = (Picture) p.picture.createClone(bool); }catch (CloneNotSupportedException e) { e.printStackTrace(); } return p; } }
Operation results
Deep cloning
Shallow cloning
6. Singleton mode
Title: a Web performance test software contains a virtual user generator In order to avoid the inconsistency in the number of virtual users generated, the test software only allows to start the only virtual user generator when working. The virtual user generator is designed in single instance mode, class diagram is drawn, and three programming simulation methods are used respectively: hungry Han single instance, double detection lock and IoDH.
Tips
Since the main class is the same as the run result, only the first of thehungary is recorded.
TheHungry
Class diagram
Programming implementation
package Singleton.TheHungry; public class Main { public static void main(String[] args){ System.out.println("The performance test begins"); Singleton user1 = Singleton.getInstance(); Singleton user2 = Singleton.getInstance(); if(user1 == user2){ System.out.println("user1 And user2 Is the same virtual user"); }else { System.out.println("These are two different examples"); } System.out.println("End of performance test!"); } } package Singleton.TheHungry; public class Singleton { private static Singleton singleton = new Singleton(); private Singleton() { System.out.println("A virtual user was generated"); } public static Singleton getInstance(){ return singleton; } }
Operation results
DoubleChecked
Class diagram
Programming implementation
package Singleton.DoubleChecked; public class Singleton { private volatile Singleton singleton; private Singleton() { System.out.println("A virtual user was generated"); } public static Singleton getInstance(){ if(singleton == null){ synchronized (Singleton.class){ if(singleton == null){ singleton = new Singleton(); } } } return singleton; } }
Operation results
IoDH
Class diagram
Programming implementation
package Singleton.IoDH; public class Singleton { private Singleton() { System.out.println("A virtual user was generated"); } private static class HolderClass { private final static Singleton singleton = new Singleton(); } public static Singleton getInstance(){ return HolderClass.singleton; } }
.DoubleChecked;
public class Singleton {
private volatile Singleton singleton;
private Singleton() {
System.out.println("a virtual user is generated");
}
public static Singleton getInstance(){
if(singleton == null){
synchronized (Singleton.class){
if(singleton == null){
singleton = new Singleton();
}
}
}
return singleton;
}
}
##### Operation results #### IoDH ##### Class diagram [External chain picture transfer...(img-nsTv2j4D-1635506203761)] ##### Programming implementation ```java package Singleton.IoDH; public class Singleton { private Singleton() { System.out.println("A virtual user was generated"); } private static class HolderClass { private final static Singleton singleton = new Singleton(); } public static Singleton getInstance(){ return HolderClass.singleton; } }