java中的设计模式 工厂 和抽象工厂模式 的例子
2009-11-26 15:58
831 查看
工厂模式:
package com.wepull.note1125;
public interface Factory {
public Com Create();
}
package com.wepull.note1125;
public interface Com {
public void money();
}
package com.wepull.note1125;
public class HbComFactory implements Factory {
public Com Create() {
// TODO Auto-generated method stub
return new HbCom();
}
}
package com.wepull.note1125;
public class HnComFactoy implements Factory {
public Com Create() {
// TODO Auto-generated method stub
return new HnCom();
}
}
package com.wepull.note1125;
public class HbCom implements Com{
public void money() {
System.out.println("Hbcom工资的计算");
}
}
package com.wepull.note1125;
public class HnCom implements Com {
public void money() {
System.out.println("Hncom工资的计算");
}
}
抽象工厂:
package com.wepull.lesson1126;
public interface Factory {
Jeep Createjeep();
Bike CreateBike();
}
package com.wepull.lesson1126;
public class LmFactory implements Factory{
public Bike CreateBike() {
// TODO Auto-generated method stub
return new LmBike();
}
public Jeep Createjeep() {
// TODO Auto-generated method stub
return new LmJeep();
}
}
package com.wepull.lesson1126;
public class LsFactory implements Factory {
public Bike CreateBike() {
// TODO Auto-generated method stub
return new LsBike();
}
public Jeep Createjeep() {
// TODO Auto-generated method stub
return new Lsjeep();
}
}
package com.wepull.lesson1126;
public class LmJeep extends Jeep {
public void Sc(){
System.out.println("生产汽车螺帽");
}
}
package com.wepull.lesson1126;
public class LmBike extends Bike{
public void Sc(){
System.out.println("生产自行车螺帽");
}
}
package com.wepull.lesson1126;
public interface Car {
void Sc();
}
package com.wepull.lesson1126;
public class Jeep implements Car {
public void Sc() {
// TODO Auto-generated method stub
}
}
package com.wepull.lesson1126;
public class Bike implements Car{
public void Sc() {
// TODO Auto-generated method stub
}
}
package com.wepull.lesson1126;
public class TestClent {
public static void main(String args[]){
Factory f = new LmFactory();
Bike bike=f.CreateBike();
Jeep jeep = f.Createjeep();
bike.Sc();
jeep.Sc();
f= new LsFactory();
bike =f.CreateBike();
jeep= f.Createjeep();
bike.Sc();
jeep.Sc();
}
}
package com.wepull.note1125;
public interface Factory {
public Com Create();
}
package com.wepull.note1125;
public interface Com {
public void money();
}
package com.wepull.note1125;
public class HbComFactory implements Factory {
public Com Create() {
// TODO Auto-generated method stub
return new HbCom();
}
}
package com.wepull.note1125;
public class HnComFactoy implements Factory {
public Com Create() {
// TODO Auto-generated method stub
return new HnCom();
}
}
package com.wepull.note1125;
public class HbCom implements Com{
public void money() {
System.out.println("Hbcom工资的计算");
}
}
package com.wepull.note1125;
public class HnCom implements Com {
public void money() {
System.out.println("Hncom工资的计算");
}
}
抽象工厂:
package com.wepull.lesson1126;
public interface Factory {
Jeep Createjeep();
Bike CreateBike();
}
package com.wepull.lesson1126;
public class LmFactory implements Factory{
public Bike CreateBike() {
// TODO Auto-generated method stub
return new LmBike();
}
public Jeep Createjeep() {
// TODO Auto-generated method stub
return new LmJeep();
}
}
package com.wepull.lesson1126;
public class LsFactory implements Factory {
public Bike CreateBike() {
// TODO Auto-generated method stub
return new LsBike();
}
public Jeep Createjeep() {
// TODO Auto-generated method stub
return new Lsjeep();
}
}
package com.wepull.lesson1126;
public class LmJeep extends Jeep {
public void Sc(){
System.out.println("生产汽车螺帽");
}
}
package com.wepull.lesson1126;
public class LmBike extends Bike{
public void Sc(){
System.out.println("生产自行车螺帽");
}
}
package com.wepull.lesson1126;
public interface Car {
void Sc();
}
package com.wepull.lesson1126;
public class Jeep implements Car {
public void Sc() {
// TODO Auto-generated method stub
}
}
package com.wepull.lesson1126;
public class Bike implements Car{
public void Sc() {
// TODO Auto-generated method stub
}
}
package com.wepull.lesson1126;
public class TestClent {
public static void main(String args[]){
Factory f = new LmFactory();
Bike bike=f.CreateBike();
Jeep jeep = f.Createjeep();
bike.Sc();
jeep.Sc();
f= new LsFactory();
bike =f.CreateBike();
jeep= f.Createjeep();
bike.Sc();
jeep.Sc();
}
}
相关文章推荐
- Java[Android]设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- java之设计模式工厂三兄弟之抽象工厂模式
- Java设计模式之工厂模式、抽象工厂模式
- Java 可复用的面向对象的设计模式之 工厂模式Factory详解 :简单工厂模式 工厂方法模式 抽象工厂模式
- JAVA设计模式之工厂模式之抽象工厂模式
- java设计模式学习之工厂模式和抽象工厂模式
- Java设计模式之工厂方法、抽象工厂模式 程序 总结
- java设计模式---工厂方法和抽象工厂模式
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)
- java va中工厂模式( 静态工厂模式、工厂方法模式、抽象工厂模式)的介绍以及简单例子
- java设计模式之工厂模式(简单工厂模式、工厂方法模式、抽象工厂模式)
- Java设计模式编程中简单工厂与抽象工厂模式的使用实例
- java之设计模式工厂三兄弟之抽象工厂模式
- JAVA设计模式之:工厂模式(下)--抽象工厂模式
- java工厂设计模式小例子
- java 设计模式之工厂模式和抽象工厂模式
- Java-马士兵设计模式学习笔记-工厂模式-抽象工厂模式
- Java设计模式(2)创建型:工厂模式(工厂方法模式与抽象工厂模式)
- JAVA设计模式之工厂模式(简单工厂模式+工厂方法模式+抽象工厂模式)