当前位置: 首页> 文旅> 文化 > 惠州百度seo排名_软件网站开发公司_seo网站培训班_网页设计模板

惠州百度seo排名_软件网站开发公司_seo网站培训班_网页设计模板

时间:2025/7/13 6:50:36来源:https://blog.csdn.net/qq_59463944/article/details/147581136 浏览次数:0次
惠州百度seo排名_软件网站开发公司_seo网站培训班_网页设计模板

Java 工厂模式示例代码

下面我将展示三种常见的工厂模式实现:简单工厂模式、工厂方法模式和抽象工厂模式。

1. 简单工厂模式

java

// 产品接口
interface Shape {void draw();
}// 具体产品类
class Circle implements Shape {@Overridepublic void draw() {System.out.println("画一个圆形");}
}class Rectangle implements Shape {@Overridepublic void draw() {System.out.println("画一个矩形");}
}class Square implements Shape {@Overridepublic void draw() {System.out.println("画一个正方形");}
}// 简单工厂类
class ShapeFactory {// 使用getShape方法获取形状类型的对象public Shape getShape(String shapeType) {if (shapeType == null) {return null;}if (shapeType.equalsIgnoreCase("CIRCLE")) {return new Circle();} else if (shapeType.equalsIgnoreCase("RECTANGLE")) {return new Rectangle();} else if (shapeType.equalsIgnoreCase("SQUARE")) {return new Square();}return null;}
}// 测试类
public class SimpleFactoryDemo {public static void main(String[] args) {ShapeFactory shapeFactory = new ShapeFactory();// 获取Circle对象并调用它的draw方法Shape shape1 = shapeFactory.getShape("CIRCLE");shape1.draw();// 获取Rectangle对象并调用它的draw方法Shape shape2 = shapeFactory.getShape("RECTANGLE");shape2.draw();// 获取Square对象并调用它的draw方法Shape shape3 = shapeFactory.getShape("SQUARE");shape3.draw();}
}

2. 工厂方法模式

java

// 产品接口
interface Button {void render();void onClick();
}// 具体产品类
class HtmlButton implements Button {@Overridepublic void render() {System.out.println("<button>HTML按钮</button>");onClick();}@Overridepublic void onClick() {System.out.println("点击HTML按钮! 浏览器跳转");}
}class WindowsButton implements Button {@Overridepublic void render() {System.out.println("绘制Windows风格按钮");onClick();}@Overridepublic void onClick() {System.out.println("点击Windows按钮! 执行命令");}
}// 创建者抽象类
abstract class Dialog {public void renderWindow() {Button okButton = createButton();okButton.render();}// 工厂方法public abstract Button createButton();
}// 具体创建者类
class HtmlDialog extends Dialog {@Overridepublic Button createButton() {return new HtmlButton();}
}class WindowsDialog extends Dialog {@Overridepublic Button createButton() {return new WindowsButton();}
}// 测试类
public class FactoryMethodDemo {private static Dialog dialog;public static void main(String[] args) {configure();runBusinessLogic();}static void configure() {if (System.getProperty("os.name").equals("Windows 10")) {dialog = new WindowsDialog();} else {dialog = new HtmlDialog();}}static void runBusinessLogic() {dialog.renderWindow();}
}

3. 抽象工厂模式

java

// 抽象产品接口
interface Checkbox {void paint();
}interface GUIFactory {Button createButton();Checkbox createCheckbox();
}// 具体产品类
class MacOSButton implements Button {@Overridepublic void render() {System.out.println("渲染MacOS风格按钮");}@Overridepublic void onClick() {System.out.println("MacOS按钮点击事件");}
}class MacOSCheckbox implements Checkbox {@Overridepublic void paint() {System.out.println("渲染MacOS风格复选框");}
}class WindowsButton implements Button {@Overridepublic void render() {System.out.println("渲染Windows风格按钮");}@Overridepublic void onClick() {System.out.println("Windows按钮点击事件");}
}class WindowsCheckbox implements Checkbox {@Overridepublic void paint() {System.out.println("渲染Windows风格复选框");}
}// 具体工厂类
class MacOSFactory implements GUIFactory {@Overridepublic Button createButton() {return new MacOSButton();}@Overridepublic Checkbox createCheckbox() {return new MacOSCheckbox();}
}class WindowsFactory implements GUIFactory {@Overridepublic Button createButton() {return new WindowsButton();}@Overridepublic Checkbox createCheckbox() {return new WindowsCheckbox();}
}// 客户端代码
class Application {private Button button;private Checkbox checkbox;public Application(GUIFactory factory) {button = factory.createButton();checkbox = factory.createCheckbox();}public void paint() {button.render();checkbox.paint();}
}// 测试类
public class AbstractFactoryDemo {private static Application configureApplication() {Application app;GUIFactory factory;String osName = System.getProperty("os.name").toLowerCase();if (osName.contains("mac")) {factory = new MacOSFactory();} else {factory = new WindowsFactory();}app = new Application(factory);return app;}public static void main(String[] args) {Application app = configureApplication();app.paint();}
}

总结

  1. 简单工厂模式

    • 一个工厂类根据传入的参数决定创建哪种产品

    • 适用于产品较少且不经常变化的情况

  2. 工厂方法模式

    • 定义一个创建对象的接口,但让子类决定实例化哪个类

    • 适用于需要扩展性强、产品种类可能增加的情况

  3. 抽象工厂模式

    • 提供一个创建一系列相关或相互依赖对象的接口

    • 适用于产品族的情况,需要创建多个相关产品对象

每种模式都有其适用场景,选择哪种模式取决于具体的需求和设计目标。

关键字:惠州百度seo排名_软件网站开发公司_seo网站培训班_网页设计模板

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: