mercredi 1 avril 2015

How to avoid duplicating code and processing on an selection method and on a constructor?

I have a situation where I need to classify a bunch of files according to several parameters (file name, content, specific features of the content if it is one type of file, other features if it is another) and after identifying the right type of file, I'll create an object passing that file to a constructor. This part is computationally expensive, and i already have it working adequately.


The thing is, the constructor of each object will basically do the same processing that the classifier did, because most of the things the classifier checks for will be repeated when I'm creating the object. But this leads to an awful lot of code duplication and processing duplication. All the objects I'm creating have inherit from the same base class.


Is there a cleaner way to achieve this and avoid the duplication (ie some standard design pattern)?


I have thought about simply throwing the files at all the different possible object constructors and bail out of the constructors with exceptions if it's not the right file for that constructor, but i don't specially enjoy using exceptions for code branching and flow (i don't have anything specific against it, i just think it has an unpleasant smell). Another thing I've thought about was moving the processing part out of the object constructors and use the values i got from the classifier to populate the object after creating it "empty", but that way I'd be moving behavior out of the class, and that doesn't feel right either, even if it does work.


Here is an example of what I'm trying to achieve:



file A.abc
file B.abc
(potentially hundreds of files on each run)

Classifier.Scan(file A.abc)
<heavy lifting operations>
<decides file A is of type alpha>
var a= new alpha(file A.abc)
<now constructor of alpha will do most of the same heavy lifting operations again to create the object>

Classifier.Scan(file B.abc)
<heavy lifting operations>
<decides file B is of type beta>
var b= new beta(file B.abc)
<now constructor of beta will do most of the same heavy lifting operations again to create the object>

so on for other files.


I'm coding it in c#, but i don't think the problem nor the solution are language specific. Thank you very much for your time.


missing return statement in factory pattern c#

i have the following code for my code but it always says the return statement is missing but i have put them in a switch list? please help!?


public IMap Map(string oldtheme) { switch (oldtheme) { case "archer": return new Archer(); case "craftyblue": return new CraftyBlue(); case "minimal": return new Minimal(); case "mintalicious": return new Mintalicious(); case "misfit": return new Misfit(); case "peach": return new Peach(); case "queen": return new Queen(); case "sketch": return new Sketch(); case "takeaway": return new TakeAwayLemonFresh(); case "lemonfresh": return new TakeAwayLemonFresh(); case "vanilla": return new Vanilla(); case "velvet": return new Velvet(); case "victoriana": return new Victoriana(); case "writer": return new Writer();

}



} // this is where the error is
}

How to calculate a shortest distance form town to town in java?

I was asked at an interview a question on how would I write code which would calculate a shortest route from one town to another. What would a Road class look like if there were lets suppose Classes of a type road and how would the main method work with that?


Anyone can come here with some solution?


This redundant Readonly prefix


internal abstract class ReadonlyCoefs
{
public const Boolean Allowed = true;
public const Boolean Disallowed = false;

public abstract Boolean GetCoef();
}

internal class Coefs : ReadonlyCoefs
{
public override Boolean GetCoef() { ... }
public void SetCoef() { ... }
}


Now, suppose i want to use it somewhere like this



if (variable == ReadonlyCoefs.Allowed)
...


I don't think i want to have Readonly prefix.


In this case i can just add instance methods IsAllowed and SetAllowed, SetDisallowed, but if there are a lot of consts, how to be in such a case?


Mixing prototypes with singletons

I've got some application in Spring, which on every request creates prototype bean X (from factory). This bean X has some DAO singletons e.g. Y, Z. and SomeObject on which I work (set values etc.)



@Scope(value="prototype")
public class X{
@Autowired
private X x;
@Autowired
private Z z;

private SomeObject obj;

public void someMethod(){
obj.setProperty();
}
}


Is it right approach? I mean, in every request Spring container must search for those singleton beans and inject them into prototype, is it effective? Or maybe it's better to create stateless bean and create SomeObject instance in appropriate method, and then pass it in function arguments whenever I want to use it (it's not so comfortable)?


Hierarchies of Visitors modifying the behavior of parent. Is it fine with Liskov?

There is a class IUser. It has a function which takes a Visitor and allows changes to the public properties.



public IUser
{
public PermissionMatrix Permissions { get; set; }

public void Authorizations(IAuthManager manager)
{
manager.SetRoles(this);
}
}


Now, it can be visited by class hierarchies of IAuthManager



public IAuthManager
{
public void SetRoles(IUser user);
}

public InternalAuthManager : IAuthManager
{
public virtual void SetRoles(IUser user)
{
// sets permissions in user for internal security
// according to a complex logic
}
}

public RestrictInternalAuthManager : InternalAuthManager
{
public override void SetRoles(IUser user)
{
base.SetRoles(user); // need to use complex logic of parent
// then reverts few permissions based on conditions
}
}


I want to evaluate if class RestrictInternalAuthManager is violating Liskov Substitution Principle. I have been arguing for both yes and no,


No : There is no check for type of IAuthManager.


Yes : RestrictInternalAuthManager is changing the post-conditions of InternalAuthManager.


Can this be left as it is, or the classes require refactoring? Any help is appreciated.


Redesigning a search engine, what design patterns to apply

I'm really stuck here. I'm trying to do add a new database in addition to a current one. I'm going to use a factory pattern to select the right database control like this:



private AssetStoresFactory assetStoresFactory;

AssetStore assetStore = assetStoreFactory.getAssetStore(query);
assetStore.search(search);

public AssetStore getAssetStore(String query){
AssetStore assetStore;
if (qualifiesForStoreA(query))
assetStore = new AssetStoreA();
else
assetStore = new AssetStoreB();
return assetStore;
}


AssetStore A
.....
Results search(AssetSearch search) {
AssetSearchOperation o = new AssetSearchOperationA(search);
}
.....


AssetStore B
.....
Results search(AssetSearch search) {
AssetSearchOperation o = new AssetSearchOperationB(search);
}
.....


Now the real problem lies here. The current approach to perform searches is like this:



_______________________
| BaseSearchOperation |
| ___________________ |
| abstract search() |
-----------------------

|
____________________
| StreamingSupport |
|__________________|

|
______________________
|AssetSearchOperation|
|____________________|


Now there is code to query the current database in all of the above classes and this needs to be removed and isolated. I can't get my head around how this can be done. Note that anonymous implementations of AssetSearchOperation override StreamingSupport. Any help is appreciated! If you need more information, please do ask!