askari.khosravani
یک شنبه 05 دی 1389, 21:10 عصر
سلام
من پترن جاوا رو گرفتم ولی نمی دونم چه طوری اجرا می شه کسی هست که بتونه به ما کمک کنه؟؟؟؟؟؟؟؟؟؟؟
ممنون میشم به خاطره شبه امتحانه...............دیگه
package accounting;
import java.lang.reflect.Constructor;
import java.util.HashMap;
public class ServicesFactory {
private static IObject interfaceObjectAdaptor;
private static String interfaceClassAdaptorString;
private static ServicesFactory factory;
private static HashMap<String,Constructor<?>> serviceAdapters;
private static HashMap<String,IObject> serviceObjects = new HashMap<String, IObject>();;
private ServicesFactory( ){}
//ExampleFactory (a singleton)
public synchronized static ServicesFactory getServiceFactory(HashMap<String,Constructor<?>> factoryServices){
if( factory == null ){
serviceAdapters = factoryServices;
factory = new ServicesFactory();
}
return factory;
}
public void setAdapterName(String cls ) {
this.interfaceClassAdaptorString = cls;
}
public IObject getAdaptor(){
String adapterName = this.interfaceClassAdaptorString;
try{
if( this.interfaceObjectAdaptor == null){
Object obj = null;
Constructor<?> con = null;
Class<?> cl = System.class;
adapterName = System.getProperty(adapterName);
this.interfaceObjectAdaptor = (IObject)Class.forName(adapterName).newInstance();
}
} catch(Exception e) {
}
return this.interfaceObjectAdaptor;
}
public boolean factoryItemExists(Constructor<?> cls){
return ServicesFactory.serviceAdapters.containsValue(cls) ;
}
public IObject getAdapter(String adapter){
//create a Service object,
try{
Object obj = null;
Constructor<?> cl = null;
Class cls;
String str = null;
cl = ServicesFactory.serviceAdapters.get(adapter);
obj = cl.getClass();
obj = serviceObjects.get(obj);
if( obj == null)
{
obj = cl.newInstance();
serviceObjects.put(adapter,(IObject) obj);
}
return (IObject) obj;
} catch(Exception e) {
return null;
}
}
}
package accounting;
import accounting.*;
import java.lang.reflect.Constructor;
import java.util.HashMap;
public class AccountingServices {
private HashMap<String,Constructor<?>> services;
private ServicesFactory servicesFactory;
public AccountingServices(){
try{
services = new HashMap<String, Constructor<?>>();
Class cls = accounting.SAPAdapter.class;
Constructor<?> con = cls.getConstructor(null);
con.setAccessible(true);
services.put("SAPAdapter",con);
cls = accounting.NorthernAccountingAdapter.class;
con = cls.getConstructor(null);
con.setAccessible(true);
services.put("NorthernAccountingAdapter",con);
servicesFactory = ServicesFactory.getServiceFactory(services);
servicesFactory.setAdapterName("accountingfirm.class.name");
}catch(Exception e){}
}
public IAccountingAdaptor getAccountingAdaptor(String cls){
return (IAccountingAdaptor)servicesFactory.getAdapter(cls );
}
public IAccountingAdaptor getAccountingAdaptor(){
return (IAccountingAdaptor)servicesFactory.getAdaptor();
}
public static void main(String args[]) {
IAccountingAdaptor myIface = null;
String adapter1 = "SAPAdapter";
String adapter2 = "NorthernAccountingAdapter";
AccountingServices sb = new AccountingServices();
myIface = sb.getAccountingAdaptor(adapter1);
myIface.postReceivable("Factory Made");
myIface.postSale("Factory Made");
myIface = sb.getAccountingAdaptor(adapter2);
myIface.postReceivable("Factory Made");
myIface.postSale("Factory Made");
myIface = sb.getAccountingAdaptor();
myIface.postReceivable("Factory Made");
myIface.postSale("Factory Made");
}
}
من پترن جاوا رو گرفتم ولی نمی دونم چه طوری اجرا می شه کسی هست که بتونه به ما کمک کنه؟؟؟؟؟؟؟؟؟؟؟
ممنون میشم به خاطره شبه امتحانه...............دیگه
package accounting;
import java.lang.reflect.Constructor;
import java.util.HashMap;
public class ServicesFactory {
private static IObject interfaceObjectAdaptor;
private static String interfaceClassAdaptorString;
private static ServicesFactory factory;
private static HashMap<String,Constructor<?>> serviceAdapters;
private static HashMap<String,IObject> serviceObjects = new HashMap<String, IObject>();;
private ServicesFactory( ){}
//ExampleFactory (a singleton)
public synchronized static ServicesFactory getServiceFactory(HashMap<String,Constructor<?>> factoryServices){
if( factory == null ){
serviceAdapters = factoryServices;
factory = new ServicesFactory();
}
return factory;
}
public void setAdapterName(String cls ) {
this.interfaceClassAdaptorString = cls;
}
public IObject getAdaptor(){
String adapterName = this.interfaceClassAdaptorString;
try{
if( this.interfaceObjectAdaptor == null){
Object obj = null;
Constructor<?> con = null;
Class<?> cl = System.class;
adapterName = System.getProperty(adapterName);
this.interfaceObjectAdaptor = (IObject)Class.forName(adapterName).newInstance();
}
} catch(Exception e) {
}
return this.interfaceObjectAdaptor;
}
public boolean factoryItemExists(Constructor<?> cls){
return ServicesFactory.serviceAdapters.containsValue(cls) ;
}
public IObject getAdapter(String adapter){
//create a Service object,
try{
Object obj = null;
Constructor<?> cl = null;
Class cls;
String str = null;
cl = ServicesFactory.serviceAdapters.get(adapter);
obj = cl.getClass();
obj = serviceObjects.get(obj);
if( obj == null)
{
obj = cl.newInstance();
serviceObjects.put(adapter,(IObject) obj);
}
return (IObject) obj;
} catch(Exception e) {
return null;
}
}
}
package accounting;
import accounting.*;
import java.lang.reflect.Constructor;
import java.util.HashMap;
public class AccountingServices {
private HashMap<String,Constructor<?>> services;
private ServicesFactory servicesFactory;
public AccountingServices(){
try{
services = new HashMap<String, Constructor<?>>();
Class cls = accounting.SAPAdapter.class;
Constructor<?> con = cls.getConstructor(null);
con.setAccessible(true);
services.put("SAPAdapter",con);
cls = accounting.NorthernAccountingAdapter.class;
con = cls.getConstructor(null);
con.setAccessible(true);
services.put("NorthernAccountingAdapter",con);
servicesFactory = ServicesFactory.getServiceFactory(services);
servicesFactory.setAdapterName("accountingfirm.class.name");
}catch(Exception e){}
}
public IAccountingAdaptor getAccountingAdaptor(String cls){
return (IAccountingAdaptor)servicesFactory.getAdapter(cls );
}
public IAccountingAdaptor getAccountingAdaptor(){
return (IAccountingAdaptor)servicesFactory.getAdaptor();
}
public static void main(String args[]) {
IAccountingAdaptor myIface = null;
String adapter1 = "SAPAdapter";
String adapter2 = "NorthernAccountingAdapter";
AccountingServices sb = new AccountingServices();
myIface = sb.getAccountingAdaptor(adapter1);
myIface.postReceivable("Factory Made");
myIface.postSale("Factory Made");
myIface = sb.getAccountingAdaptor(adapter2);
myIface.postReceivable("Factory Made");
myIface.postSale("Factory Made");
myIface = sb.getAccountingAdaptor();
myIface.postReceivable("Factory Made");
myIface.postSale("Factory Made");
}
}