package methodOfFactory;
public class People
{
private String name;
private String sex;
public People(){}
public People(String name,String sex)
{
this.name = name;
this.sex = sex;
}
public void show(){}
public String toString()
{
return "Name: "+name+" Sex: "+sex;
}
}
package methodOfFactory;
public class Boy extends People {
public Boy(){}
public Boy(String name)
{
super(name,"male");
}
public void show(){}
}
package methodOfFactory;
public class ZhangSan extends Boy {
public ZhangSan()
{
super("zhang san");
}
public void show()
{
System.out.println("zhang san is my dota friend!");
}
}
package methodOfFactory;
public class Girl extends People {
public Girl(){}
public Girl(String name)
{
super(name,"male");
}
public void show(){}
}
package methodOfFactory;
public class LiSi extends Girl {
public LiSi()
{
super("li si");
}
public void show()
{
System.out.println("li si is my girl friend!");
}
}
package methodOfFactory;
public class WangWu extends Girl
{
public WangWu()
{
super("wang wu");
}
public void show()
{
System.out.println("wang wu is my wife!");
}
}
package methodOfFactory;
public interface Factory {
public abstract People getPeople(String name);
}
package methodOfFactory;
public class BoyFactory implements Factory {
@Override
public People getPeople(String name) {
if(name.equalsIgnoreCase("zhangsan"))
{
return new ZhangSan();
}
return null;
}
}
package methodOfFactory;
public class GirlFactory implements Factory
{
@Override
public People getPeople(String name)
{
if(name.equalsIgnoreCase("lisi"))
{
return new LiSi();
}else if(name.equalsIgnoreCase("wangwu"))
{
return new WangWu();
}
return null;
}
}
package methodOfFactory;
public class Main
{
public static void main(String[] args)
{
Factory f = new BoyFactory();
Boy b = (Boy) f.getPeople("zhangsan");
System.out.println(b);
b.show();
Factory fa = new GirlFactory();
Girl g = (Girl) fa.getPeople("lisi");
System.out.println(g);
g.show();
Girl gg = (Girl) fa.getPeople("wangwu");
System.out.println(gg);
gg.show();
}
}
/*
*工厂方法模型就要解决的是有多个工厂,每一个工厂生产特定产品,都是在工厂里面产生对象
*以后凡是要增加其他类型的产品的时候就可以新建一个工厂继承父工厂,然后在新建产品类就
*行,这样很好的体现了Java的一个设计原则,开闭原则
* */
分享到:
相关推荐
Factory Method(工厂方法)模式是GoF(Gang of Four)设计模式之一,属于创建型模式。此模式的核心在于提供了一种创建对象的方式,而不必将具体的类名硬编码到程序中,从而增强了程序的灵活性和可维护性。 #### ...
Factory Method Abstract Factory Builder Prototype Singleton 结构型 Adapter Bridge Composite Decorator Facade Flyweight Proxy 行为型 Interpreter Template Method Chain of Responsibility ...
在本作业中,我们面临的是一个使用工厂方法模式来设计汽车保险信息系统的任务。工厂方法模式是一种设计模式,它提供了一种将实例化过程推迟到子类中的方式,从而允许在运行时创建对象的特定类型,而不必在编译时就...
如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...
★第1章至第11章陆续介绍了设计模式:Strategy、Observer、Decorator、Abstract Factory、Factory Method、Singleton、Command、Adapter、Facade、TemplatMethod、Iterator、Composite、State、Proxy。 ★第12章介绍...
Factory Method Creates an instance of several derived classes Prototype A fully initialized instance to be copied or cloned Singleton A class of which only a single instance can exist ...
It is a process whereby objects define their dependencies, that is, the other objects they work with, only through constructor arguments, arguments to a factory method, or properties that are set on ...
如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...
如:Singleton、AbstractFactory、Factory Method、Builder、Prototype。 行为型模式涉及到类和对象如何交互及分配职责。如:Template Method、Command、Iterator、Observer、State、Strategy、Mediator、Visitor、...
客户端代码通过调用 `Creator` 类的 `AnOperation` 方法间接地创建了 `ConcreateProduct` 对象,而具体的创建逻辑则由 `ConcreateCreator` 类的 `FactoryMethod` 方法完成。这种方式使得系统更加灵活,可以轻松地...
在Spring框架中,`org.springframework.beans.factory.InitializingBean`接口是一个非常重要的概念,它用于标记那些需要在初始化完成后执行特定逻辑的bean。这个接口只包含一个方法:`afterPropertiesSet()`,当bean...
<bean id="exampleBean" factory-bean="myFactory" factory-method="createInstance"/> ``` 在这个例子中,"myFactory"是工厂bean的ID,而`createInstance`是工厂方法的名称。Spring会调用这个方法来创建目标bean。...
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.transaction.interceptor.TransactionInterceptor#0': Error setting property values; nested ...
Chapter 4: Factory Method Chapter 5: Abstract Factory Chapter 6: Builder Chapter 7: Singleton Chapter 8: Adapter Chapter 9: Bridge Chapter 9: Bridge Chapter 11: Mediator Chapter 12: Observer Chapter ...
It starts with a general introduction to all types of programming patterns and goes on to describe 10 of the most popular design patterns in detail: Singleton, Iterator, Adapter, Decorator, State, ...